Perl/PHP Hash array

Want to move your code and projects to LiveCode but don't know where to start?

Moderators: FourthWorld, heatherlaine, Klaus, robinmiller

Post Reply
Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Perl/PHP Hash array

Post by Philhold » Wed Oct 21, 2009 11:20 am

Is there any form of text that I can use to introduce the whole of an associative array into RunRev?

In Perl I can say something similar to using put in RunRev like this.

RunRev
put varValue into hashArray[tKey]

Perl
$hash{ $key } = $value;

In Perl if I want to write a whole a hash array I can use this:

%hash = (
key1 => 'value1',
key2 => 'value2',
key3 => 'value3',
key4 => 'value4',
key5 => 'value5',
key6 => 'value6',
key7 => 'value7',
);

I use these all of the time, sometimes with hundreds of key value pairs. Short of using chunk expressions and a loop to "put" each value into a key of the array is there a way that I can write a whole associative array and introduce it into a RunRev script?

Thanks

Phil

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Location: London, UK
Contact:

Post by Mark Smith » Wed Oct 21, 2009 11:56 am

Phil, I don't think it's all that different:

Code: Select all

put "value1" into tArray[key1]
put "value2" into tArray[key2]
...etc
which isn't all that much more verbose than the perl example you give.

On the other hand, if you already have, lets say, a comma and return delimited list, you can use the "split" command.

so if myVar contains:

key1,value1
key2,value2

you can do

Code: Select all

split myVar by cr and comma
and myVar is now an array.

Also, if you have a list of values in a variable myVar, eg.

val1,val2,val3,val4,etc

and are happy to have numeric keys, you can

Code: Select all

split myVar by comma
and myVar will be an array like so

myVar[1] = val1
myVar[2] = val2

and so on.

Best,

Mark Smith

Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Post by Philhold » Wed Oct 21, 2009 12:13 pm

Hi Mark,

Thanks for replying so promptly. I think that in my explanation I complicated matters. I didn't have a problem with the first bit and as you explained the notation for individual elements of is not much more verbose. It was the second part I was having problems with and as you explain that is so easy you find yourself thinking , it can't be that easy, can it.

I had a repeating loop doing stuff like this:

Code: Select all

put the second word of tVar into tArray[tKey][1]
Which is clearly the wrong way to go.

Many thanks for the hours of frustration saved.

I suppose you can do something similar but a bit more complicated with multidimensional arrays by splitting key value lists into existing keys.

Thanks again

Phil

Post Reply

Return to “Converting to LiveCode”