I'm just starting to play with references to create nested data structures. I've read through the Advanced Perl Programming (O'Reilly) on data structures and the Perl docs concerning the subject but I'm still a little shaky and/or missing something.

Basically I'm parsing a few log files to associate user name with given and last names. Not all entries have a last or given.

Sample log looks like this:

User: username1
   Last Name: last name1
   Given Name: given name1
User: username2
   Last Name: last name2
User: username3
   Given Name: given name3

I'm stepping through the log line by line and trying to create an array of hashes. Each hash has a username, givenname and lastname key. I can make the initial entry of a username but when I try to add a [last|given]name I can't seem to get the right syntax.

Specific problem line (#18 & #22):

$$user_list[$num_users -1]->{lastname} = $lastname;

Complete script:

00 #!/usr/bin/perl 01 02 # parse the user list dump from nlist into 1 tsv file 03 04 open (NLIST, $ARGV[0]) or die "Can't open nlist results\n $!"; 05 @nlist = <NLIST>; 06 close NLIST; 07 08 for $line (@nlist) { 09 # do not parse for the exceptions, all else do the following: 10 if ($line !~ /^[Object|Current|A total]/) { 11 # user | given | last name? 12 if ($line =~ /^User\: (\w+)\n/) { 13 $username = lc($1); 14 push @user_list, \{ "username" => $username, "lastname" + => "", "givenname" => ""}; 15 } elsif ($line =~ /^\tLast Name: (\w+)\n/) { 16 $lastname = lc($1); 17 $num_users = scalar @user_list; 18 $$user_list[$num_users -1]->{lastname} = $lastname; 19 } elsif ($line =~ /^\tGiven Name: (.)+\n/) { 20 $givenname = $1; 21 $num_users = scalar @user_list; 22 $$user_list[$num_users -1]->{givenname} = $givenname; 23 } 24 } 25 } 26 27 for $user (@user_list) { 28 print "User: $$user->{username}\n"; 29 print "\tLast: $$user->{lastname}\n"; 30 print "\tGiven: $$user->{givenname}\n"; 31 }

Any and all help is greatly appreciated. Thanks, Mark

Edit by tye


In reply to Dereferencing Array of Hashes by mxn_mark

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.