We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello there! i have a .txt file composed by words separated by commas. I loaded it into Processing, and joined all the lines and i would really like my program to go through the text and only print the commas at the right position(for example, if i have something like this: "word 1,word 2,word 3,..." i would really like to be able to have this " , , ,..." where the blanks represent exactly the number of the words' chars, symbols and spaces (in this case there are 6 blanks and a comma, since "word 1" is formed by 6 chars). Now, the output i would like to have is a continuous horizontal text made of commas, but i am afraid i am unable to break down the workflow and to understand how can i achieve this.
can anybody help me understand the direction to take? I am not asking for code, but just for a hint regarding what to consider and where to watch.
Thank you very much!
Answers
show your code....
use split() with ','
then for-loop over it, use length() on each entry in the array and insert as many ' ' as length() returned
thank you for your pointers!(sorry for the late reply!) here is my code till now:
here i should have an array of words and a for loop iterating through the array. i am trying to draw a line at the end of every word, but i can't use length() on words, since words is an array. but if i run words.length the returned value will be the length of the array. i am now looking for a solution in the reference pages but my doubt remains.
[]
array access operator in order to get its content: https://Processing.org/reference/arrayaccess.htmlfor ( : )
loop:https://Processing.org/reference/for.html
for (String w : words) line(w.length(), 0, w.length(), height);
that's amazing! thank you both for helping me out! i guess what i missed (i am really blind) was the existence of the for ( : ) loop. i have always used the other version. thank you thank you! it worked! now i am on my way to understanding how to draw the lines not all starting from the same spot, but from the previous line! cheers!
Dunno what's the exactly layout you wish to achieve but here's a start:
And the same above w/ traditional
for ( ;; )
loop: :Dso, the two pieces of code you just posted really helped me out in figuring how to approach the problem! i tried to apply the second method you indicated and now i have something like this:
tell me if i am wrong: thanks to GAP i can scale up and down the entire visualisation, and the lines are drawn basically at the end of every word length. so if i have dog the line is drawn before the d and after the g. it looks like it is working, but i feel that something is still wrong. i don't know why but sometimes the lines don't reflect the real length of the word. i am going to test this with another text, in which i alternate the length of the words in a more evident way! all of this was just to say: thank you for the help! :)
edit: after some testing i can affirm that the length of the words is not represented. ._.
thank you for the code. i think this example explains very well what i mean. i spent a bit of time trying to understand as much as i could what you did. the red lines are representing the length of the different words, but i can't understand why the vertical lines don't reflect the same length. i mean, shouldn't they be where the red line ends? what i am trying to achieve with all this is to draw only the vertical lines after every word. i am feeling so obtuse right now for not getting there despite all the help you gave me sigh.
you might want to look a textWidth in the reference
https://www.processing.org/reference/textWidth_.html
I also have made the obeservation that it is vital for
textWidth()
to work to calltextSize()
before it (I have it indraw()
)remark
please note that with modern fonts the graphical length of the word (given by
textWidth()
) is not really connected to the number of chars (which we were talking about up to now).This is because the graphical width of letters is not the same in modern ttf fonts: m is much wider then i etc.
textWidth()
takes care of that.when we need to make a table (columns of numbers) in an e-mail or so, we use the font Courier or Lucida Console, e.g., because they have a fixed size
https://en.wikipedia.org/wiki/Typeface#Proportion
you wrote:
not really...
look at the old code I wrote
black line:
red line
As you see, the black lines x-value is
words[v].length()+ v*GAP + 32
and the red line goes to a x-value of
words[v].length()*10
(actually imho it doesn't make much sense to use
v*GAP
in an x-value formula but here you go. I think it's more for y-value)