[lug] perl hash and array assignments in a loop

karl horlen horlenkarl at yahoo.com
Fri Nov 16 16:01:44 MST 2007


> perl -e '$x{Karl} = ["Marx", "Horlen", "Rove"];
> print "@{$x{Karl}}\n"; print
> "$x{Karl}[1]\n"; print "$x{Karl}->[1]\n"'


I think I'm already using one of your constructs as
described in initial code sample but it doesn't work. 
This just returns the array index count.

print "dateinfo is :" .  @{$report{$id}{$dateinfo}} .
"\n";

>From your example and more searching, I realize I can
use the specific indices to access a given indice, but
there can be different number of indices for each
record 'dateinfo' field.  I'd like to just be able to
do a quick debug print to see what's in each record
rather than have to create a loop, check the indice
count and burn through the array elements one at a
time.

Hope that makes sense.  I'm starting to gather the
whole scalar only concept of perl.  It not intuitive
and it's rather hard to read programs with these
complex data structures.

> I find the last easier to read than the one before
> it, but, since it's Perl,
> they'll both work.
> 
> (Working this stuff out on the command line, in
> one-liners, is also easier
> for me than trying to debug programs I have to edit
> and run.)
> 
> Hope this helps!
> 
> On Nov 16, 2007 2:15 AM, karl horlen
> <horenkarl at yahoo.com> wrote:
> 
> > I haven't programmed perl in awhile and can't find
> the
> > answer to what should be a pretty simple
> operation.
> >
> > I read file input line by line in a loop.  I split
> the
> > line fields out to an array called @tokens.  I
> know
> > that array is being set correctly via a test print
> > statement.
> >
> > The problem comes in when I try to assign the
> array to
> > a hash key/variable.  After searching for a long
> time
> > it looks like perl requires that I assign an array
> as
> > a
> > reference to the hash key.  I believe that part is
> > working by debug print statements.
> >
> > However when it comes time to print the array
> VALUES
> > as output, I either print the array value count or
> the
> > array reference address and never the values
> > contained in the array.
> >
> > 1) How do I output the array values contained in
> the
> > hash key in a single print statement?  Similar to
> how
> > you use 'print @tokens' for a standard array.
> >
> > 2) I know this will be soon on deck. How do I
> > individually print each hash key array value
> > separately using a loop?  I can handle the loop, I
> > just
> > need to know the convoluted syntax  to access the
> hash
> > key array values one by one.
> >
> > My very simplified code snippet that isn't working
> > (stuff left out)
> >
> > LOOP
> >
> >  my @tokens = split(' ');
> >
> >  # this works!
> >  #print "@tokens";
> >
> >  # assign by reference
> >  $report{$id}{$dateinfo} = \@tokens;
> >
> >  # TRY TO PRINT as an array
> >  # WRONG! this prints the count of array
> >  # values but not the values themselves!
> >  print "dateinfo is :" .
> @{$report{$id}{$dateinfo}} .
> > "\n";
> >
> >  # TRY TO PRINT as a scalar
> >  # WRONG!  this prints the array ref not the
> > values!!! -> dateinfo is :ARRAY(0x8f865fc)
> >  print "dateinfo is :" . $report{$id}{$dateinfo} .
> > "\n";
> >
> > END LOOP
> >
> > Also.  How would I add a scalar variable that
> keeps a
> > running count of records that have the same date?
> >
> > This actually works:
> >
> >  $record{$id}{$dateinfo}++;
> >
> > But if I assign an array to hold the field values
> to
> > that $dateinfo key as described above and then
> > increment a count on it, I will overwrite the
> array
> > stored in the hash key.
> >
> > That means I need to go with another hash level
> but
> > I'm getting more confused with the perl syntax.  I
> > imagine it would look something like this:
> >
> >  $record{$id}{$dateinfo}$count++;
> >  $record{$id}{$dateinfo}@fields;
> >
> > Any Perl pros out there?
> >
> >
> >
> >
> > 
>
____________________________________________________________________________________
> > Never miss a thing.  Make Yahoo your home page.
> > http://www.yahoo.com/r/hs
> > _______________________________________________
> > Web Page:  http://lug.boulder.co.us
> > Mailing List:
> http://lists.lug.boulder.co.us/mailman/listinfo/lug
> > Join us on IRC: lug.boulder.co.us port=6667
> channel=#colug
> >
> 
> 
> 
> -- 
> Jeffrey Haemer <jeffrey.haemer at gmail.com>
> 720-837-8908 [cell]
> http://goyishekop.blogspot.com
> > _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List:
> http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667
channel=#colug



      ____________________________________________________________________________________
Be a better pen pal. 
Text or chat with friends inside Yahoo! Mail. See how.  http://overview.mail.yahoo.com/



More information about the LUG mailing list