in reply to Re: Re: Making a hash of making a hash of an array
in thread Making a hash of making a hash of an array

Actually, that's exactly what it was doing. The way your code was written, it would have inserted all the lines from the file into the hash(granted the nested while was reinserting that same hash values 10 times each). It was the line immediately after the foreach loop, as kabel already pointed out, which was resetting your your hash to just one key/value pair:

# This is the culprit! %member = ( $timestamp => [$current_time, $funds, $action, $current_fu +nds]);

That line was resetting the %member hash to the last values of $timestamp, et al. Removing that line would leave you with a hash with every line split and inserted. Now since you only wanted the last 10 lines, the for loop code I posted simply took an array slice from 0-9 and iterated over those, effectively letting the loop parse only the last 10 lines of the file.

Hope that clears things up a bit.

Replies are listed 'Best First'.
Re: Re: Re: Re: Making a hash of making a hash of an array
by jonnyfolk (Vicar) on Jun 04, 2003 at 10:52 UTC

    Once again, I appreciate your help.

    I only put the "culprit" line in because what I had before was only giving the last line read out and I attempted to 'reformat' the hash as per the example on the html-template tutorial page. How wrong I was...!

    Your advice has made me realise that instead of the while ($count < 10){ #loop } I had used:

    if ($count < 10){ $member{$timestamp} = [$current_time, $funds, $action, $current_fu +nds]; $count += 1; }
    all would have worked okay and I would have unwittingly missed out on a great deal of good advice...:)

    Thanks once again.

      Consider this instead:
      my @array = (1..20); my $i = 0; for (@array) { print "$_\n"; last if ++$i >= 10; }
      This is one of the jobs that last was built for. Now consider this one:
      my @array = (1..20); print "$_\n" for @array[0..9];
      That's called an array slice, another tool that seperates Perl from C. Play around with these two snippets. You'll learn something. ;)

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)