Dunno PHP, sorry. And Java, just like C/C++, is very strongly typed.
That is, in Java we gotta declare the type of each variable and each container.
Your $lists seems like an array of String[] type.
So it's something like: String[] lists = {}
Unfortunately, we can't dynamically create variables in Java.
That is, all variables gotta be explicitly written in the source.
We can't just pick or concatenate a String and state that is a new variable now.
Seems like in PHP, [] has multiple meanings.
While in ${$part}[$i] = the [] over there means array access,
in ${$part}[rand( ... )] apparently it means a char position within the String now.
In short, it's randomly picking up 1 char outta the String as value and associating it w/ the same String as the key.
So the {key : value} pair relationship is <String, Character> in Java.
For that, this time we're gonna use a HashMap<String, Character> in place of previous StringDict.
More specifically, an array of Map<String, Character>[]: :D https://processing.org/reference/HashMap.html
You see, If I got that right, the ${$part}[$i] is actually creating an array of Map[] in order to store 1 random()char.
Well, here it goes the probable solution for your mysterious PHP sample: #:-S
// forum.processing.org/two/discussion/10726/
// php-to-processing-variable-variables
final String[] lists = {
"interjections",
"determiners",
"adjectives",
"nouns",
"adverbs",
"verbs",
"prepositions",
"conjunctions",
"comparatives"
};
import java.util.Map;
final int capacity = ceil(lists.length/.75);
final Map<String, Character>[] chars = new Map[] {
new HashMap<String, Character>(capacity), new HashMap<String, Character>(capacity)
};
for (String part : lists) for (int i = 0; i != chars.length; ++i)
chars[i].put( part, part.charAt((int) random(part.length())) );
printArray(chars);
println();
println("verbs:", chars[0].get("verbs"), chars[1].get("verbs"));
exit();
This is the version I just did with string arrays.. It's not that elegant but works.. (the only part I haven't ported is the one between "/* */" --cause I don't get the foreach clause. Any ideas?
This is the version I just did with String arrays... It's not that elegant but works...
Well, I've posted many examples using Map containers already.
It's not easy to wrap around how to use them. And I didn't learn about them in 1 day either! b-(
Indeed separate variables are much easier & comfortable to get around... B-)
Answers
Dunno PHP, sorry. And Java, just like C/C++, is very strongly typed.
That is, in Java we gotta declare the type of each variable and each container.
Your $lists seems like an array of String[] type.
So it's something like:
String[] lists = {}
Unfortunately, we can't dynamically create variables in Java.
That is, all variables gotta be explicitly written in the source.
We can't just pick or concatenate a String and state that is a new variable now.
However, we can use a Map type container w/ {key : value} pairs.
I've chosen StringDict for this attempt: https://processing.org/reference/StringDict.html
But some HashMap<String, String> would be even faster...
thanks, really helpful
In this sense, how can I create a multidimensional array from this code? --original php script:
In this case $verb is different to $verb[1]``
In Java, the
[]
array access operator always means the index of some array:https://processing.org/reference/arrayaccess.html
Seems like in PHP,
[]
has multiple meanings.While in
${$part}[$i] =
the[]
over there means array access,in
${$part}[rand( ... )]
apparently it means achar
position within the String now.And translating the latter to Java's String, it corresponds to its method charAt():
https://processing.org/reference/String_charAt_.html
Plus the count() function would correspond to String's length() method:
https://processing.org/reference/String_length_.html
And obviously, rand() is Processing's random(): 3:-O
https://processing.org/reference/random_.html
In short, it's randomly picking up 1
char
outta the String as value and associating it w/ the same String as the key.So the {key : value} pair relationship is
<String, Character>
in Java.For that, this time we're gonna use a HashMap<String, Character> in place of previous StringDict.
More specifically, an array of Map<String, Character>[]: :D
https://processing.org/reference/HashMap.html
You see, If I got that right, the
${$part}[$i]
is actually creating an array of Map[] in order to store 1 random()char
.Well, here it goes the probable solution for your mysterious PHP sample: #:-S
how can i do an array of maps or dicts?
There should be a HasMap/StringDic tutorial, really powerful and useful..
Before anything, an alternative to previous example: :D
I'll just assume it's the latter. A simple loadStrings() will do the whole work then:
https://processing.org/reference/loadStrings_.html
In order to store the output from loadStrings(), we're gonna need Map<String, String[]>.
In this case it would be better to just assign to different String vars each .txt file isn't?
that's the Java's way after all. :P
Haven't you spotted these last lines in my example? $-)
It's doing exactly what you asked for! They can be synthesized this way too:
But it's faster & shorter to temporarily store
grammar.get("verbs")
in some variable than repeating it more than once! :-BThe Map approach would allow us to even choose a random() "grammar" category: ;;)
This is the version I just did with string arrays.. It's not that elegant but works.. (the only part I haven't ported is the one between "/* */" --cause I don't get the foreach clause. Any ideas?
Well, I've posted many examples using Map containers already.
It's not easy to wrap around how to use them. And I didn't learn about them in 1 day either! b-(
Indeed separate variables are much easier & comfortable to get around... B-)
There are 2 types of
for ()
loops in Java: regularfor ( ; ; )
& enhancedfor ( : )
:https://processing.org/reference/for.html
PHP's
foreach
equivalent in Java isfor ( : )
. In my code posts you'll find lotsa them:for (String entry : LIST) {}
for (Entry<String, String[]> pair : grammar.entrySet()) {}
P.S.: In PHP, the container to be traversed is the 1st argument before
as
.While in Java's
for ( : )
it's the opposite! It is placed after the:
! :-tReally helpful, man. Thanks a lot.