in reply to Hashes and Arrays - Selecting a memory structure

I'd go for a HoH, although a HoA would work. Consider:

use strict; use warnings; use Text::CSV; my %lookup; my $csv = Text::CSV->new (); my $filename = 'DATA'; while (<DATA>) { next unless $csv->parse ($_); my @row = $csv->fields (); next unless @row >= 7; my $key = $row[0]; next if exists $lookup{$key} && $row[7] <= $lookup{$key}{max}; $lookup{$key} = {max => $row[7], line => $., file => $filename}; } print "Following maximums found:\n"; print "$_ = $lookup{$_}{max} in file $lookup{$_}{file}, line $lookup{$ +_}{line}\n" for sort keys %lookup; __DATA__ DOW,Sep,18,09:31:16,440,29,142,10148,4 Russell2000,Sep,18,09:31:16,440,29,142,10148,4 RussellComposite,Sep,18,09:31:16,440,29,142,10148,4 Russell1000,Sep,18,09:31:16,440,29,142,10148,4 SP500,Sep,18,09:31:16,440,29,142,10148,4 Russell2000,Sep,18,09:31:16,440,29,142,10120,4 SP500,Sep,18,09:31:16,440,29,142,10160,4

Prints:

Following maximums found: DOW = 10148 in file DATA, line 1 Russell1000 = 10148 in file DATA, line 4 Russell2000 = 10148 in file DATA, line 2 RussellComposite = 10148 in file DATA, line 3 SP500 = 10160 in file DATA, line 7

Perl is environmentally friendly - it saves trees

Replies are listed 'Best First'.
Re^2: Hashes and Arrays - Selecting a memory structure
by tdudgeon (Initiate) on Sep 20, 2007 at 18:01 UTC
    My apology for being a bit unclear on the requirements..

    I'm better with examples: I need to process multiple log files containing thousands of 'DOW' related records each. (same data structure as shown in my original post). My goal is to determine if any record in the new log files contain a value in the 7th element that is greater than the current maximum value stored in the 'DOW' record of an index maximum CSV file. That CSV file will contain only 1 record per financial index. (ie DOW, SP500)

      The while loop doesn't care if the lines come from one file or many, but the sample code is much more concise if I use a single 'file' with duplicated lines. You could:

      @ARGV = @filenames; while (<>) { $filename = shift @filenames if $. == 1; ... }

      to process a swag of files.


      Perl is environmentally friendly - it saves trees