Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Perl hashes are not associative arraysassociation lists, though I can see how one might think that way if one comes from a Lisp background. Please take a careful look at perldata and perldsc for a better understanding of Perl hashes and how to work with them.

How did you set up @data_list? The code above would only print out a value if you had set up @data_list something like this:

my @data_list = ( { correct => 42 } );

Or maybe you did something like this: my %data_list =(correct=>42)? That does not set up a collection of data pairs even though the fat comma (=>) may make it seem that way. To iterate through the elements of %data_list using a for loop one would need to use keys and do something like this:

foreach my $k (keys %data_list) { my $v = $data_list->{$k}; print "The value for $k is $v\n"; }

@data_list=(correct => 42) doesn't set up discrete pairs either. It merely sets up a flat array with alternating keys and values. To iterate through key value pairs for @data_list one would need to do something like this:

for (my $i=0; $i < $#data_list; $i+=2) { my $k = $data_list[$i]; my $v = $data_list[$i+1]; print "The value for $k is $v\n"; }

Note that in Perl, %data_list and @data_list are two entirely different variables. If the preceding discussion does not clarify things, I think you will need to post more code so we can see how @data_list and/or %data_list is set up.

Best, beth

Update - struck out "associative array" and replaced with "association list" - see Re^5: Associative array for explanation.


In reply to Re: Associative array by ELISHEVA
in thread Associative array by gem555

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found