Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

is this an array or a hash or what ?

Status
Not open for further replies.

iza

Programmer
Apr 4, 2000
1,804
FR
hi :)

this is a hash :
%hash1=("key1"=>"value1","key2"=>"value2");

this is an array :
@a=(1,2,3);

but what is THIS ???
????=["key1"=>"value1","key2"=>"value2"]; ????
and how can i build it, if i have key1, value1, key2 and value2 ???

thanx for any answer :))
 
iza

Named hashes and arrays are defined like:

Code:
%hash = ( key1 => 'value1', key2 => 'value2' );
@array = ( 1, 2, 3 );

Anonymous hashes and arrays are defined like:

Code:
{ key1 => 'value1', key2 => 'value2' };
[ 1, 2, 3 ];

Anonymous structures are used to create more powerful nested data structures, for example:

Code:
my %data = ( valid   => [ "good", "ok", "yes" ],
             meaning => { ASAP => "As soon as possible",
                          RSVP => "Respondez s'il vous plait",
                        },
           );

In this example,
Code:
$data{'valid'}
points to an anonymous array, and
Code:
$data{'meaning'}
points to an anonymous hash. You would access this information like:

Code:
foreach ( @{ $data{ 'valid' } } ) {
    print "valid string: $_\n";
}

foreach ( sort keys %{ $data{ 'meaning' } } ) {
    print "$_ means $data{ 'meaning' }->{$_}\n";
}

For your example, you will need change the []'s to {}'s, to change from anonymous array to anonymous hash.

Hope this helps, s-) NEIL

 
thank you neil :))
but i had to use a function that requires a structure like this [value => key, another_value => another_key] - and i wondered what kind of a ?hash? ?array? it could be .... still don't know, but i managed to pass a string and then eval it, and it works fine
thanks anyway :)))
 
To learn more about data structures in Perl, check out "perldoc perldsc".
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top