[clug-progsig] Perl array disorder

Shawn sgrover at open2space.com
Fri Aug 15 13:55:59 PDT 2008


while I'm not familiar with perl, I have seen this same issue come up in 
other languages.

A hash is not the same as an array.  They are fundamentally different. 
Even if they do have some similarities in appearance/usage.

An array is basically just a list of items - identified by an offset 
(aka index) from the start of the array.

A hash is a key/value pair, where the key is used as the seed for a 
hashing function to determine where in memory the value is stored. 
Which means you cannot guarantee the order of a hash.

Disclaimer:  My descriptions above are very basic.  The details could be 
different - especially as you move between languages.

I suspect you know this Royce, so I'm just posting this in hopes that it 
might one day help someone else who is dealing with the issue.

Shawn

Royce Souther wrote:
> I do understand the difference between between a hash and an array. The
> example I gave was wrong. I quickly typed that up because I did not want to
> post a large block of  object code. My thinking is that a hash feature would
> build upon the programming that provides the array feature but it looks like
> that is not the case in Perl.
> 
> The code in the Perl object looks more like this.
> this->{TestArray}->{'1'} = "one";
> this->{TestArray}->{'2'} = "two";
> this->{TestArray}->{'3'}->{'cars'}->{'4door'} = "out of stock";
> this->{TestArray}->{'3'}->{'cars'}->{'2door'}->{'blue'} = "3";
> this->{TestArray}->{'3'}->{'cars'}->{'2door'}->{'red'} = "1";
> this->{TestArray}->{'3'}->{'trucks'} = "on order";
> this->{TestArray}->{'4'} = "four";
> 
> This is why I did not use push.
> 
> 
> 
> 
> On Fri, Aug 15, 2008 at 2:16 PM, William Astle <lost at l-w.ca> wrote:
> 
>> Royce Souther wrote:
>>> I don't think I would call this a bug but more just another joke Larry
>> Wall
>>> is try to play on Perl programmers.
>>>
>>> I am working with a custom config file that looks kind of like badly
>>> formated XML in that each field may contain any number of entries and
>>> sub-entries. I stack the data into an array and when I go to pull the
>> data
>>> back out is is in a different order then when I put it in. This is not a
>>> problem because the oder of the data is not important as long as the
>>> hierarchy remains intact. I would like to know if there is a reason for
>>> this. At some point in the future I may want an array in order and I
>> should
>>> know what needs to be done to make that way.
>>>
>>> root at QtCompiler[~] #cat ./PerlArrayDisorder.pl
>>> #!/usr/bin/perl
>>> #
>>>
>>> my @TestArray;
>> This makes @TestArray, an array.
>>
>>> $TestArray->{'1'} = "one";
>>> $TestArray->{'2'} = "two";
>>> $TestArray->{'3'} = "three";
>>> $TestArray->{'4'} = "four";
>> The above adds entries to a hash that is referenced by the scalar
>> $TestArray.
>>
>>> foreach $key (keys %{$TestArray})
>>> {
>>>         print "key=$key, value = ".$TestArray->{$key}."\n";
>>> }
>> The above iterates over the keys in the hash referenced by the scalar
>> $TestArray.
>>
>>> root at QtCompiler[~] #./PerlArrayDisorder.pl
>>> key=4, value = four
>>> key=1, value = one
>>> key=3, value = three
>>> key=2, value = two
>>>
>>>
>> Your problem is that you are using *hashes*, not *arrays*. Arrays are
>> indexed by [] while hashes are indexed by {}.
>>
>> Your code actually has three things in it: an array that is declared
>> local but never used, a scalar called $TestArray, and an anonymous hash
>> (which $TestArray contains a reference to).
>>
>> The following is approximately what you thought you were doing:
>>
>> #!/usr/bin/perl
>> my @TestArray;
>> push (@TestArray, "one");
>> push (@TestArray, "two");
>> push (@TestArray, "three");
>> push (@TestArray, "four");
>> foreach $value (@TestArray)
>> {
>>        print "$value\n";
>> }
>>
>> Note that arrays are indexed from 0.
>>
>> push() adds an element to the end of the array.
>>
>> $#TestArray would return the highest index in the array. That would be 3
>> in the above example. (Indexes that exist would be 0, 1, 2, 3).
>>
>> _______________________________________________
>> clug-progsig mailing list
>> clug-progsig at clug.ca
>> http://clug.ca/mailman/listinfo/clug-progsig_clug.ca
>>
> 
> 
> 



More information about the clug-progsig mailing list