Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Report parsing

by AidanLee (Chaplain)
on Jan 24, 2002 at 20:22 UTC ( [id://141233]=note: print w/replies, xml ) Need Help??


in reply to Report parsing

Well, I'm not sure on the specifics of what your data looks like, but something like this ought to work:

my $currentRegion = ''; my %regions = (); while (<FILE>) { chomp; /REGION (\d+) (.+)/ and do { $currentRegion = $1; $regions{$currentRegion}{Name} = $2; next; }; /REGION TOTAL (.+)/ and do { $regions{$currentRegion}{Total} = $1; $currentRegion = ''; next; }; do { push @{$regions{$currentRegion}{Data}}, $_ if $currentRegion n +e ''; next; }; }

You now have a hash of the regions. you can then just call each region that belongs to each person like this:

# steve's regions (some added guessed region numbers for demonstration +): foreach my $region ( @regions{ '7A', '8A', '9A' } ) { #do stuff to each $region that belongs to steve... }

Replies are listed 'Best First'.
Re: Re: Report parsing
by Dalin (Sexton) on Jan 24, 2002 at 20:42 UTC
    I'm working with the example you gave, but I am having trouble getting it to work. I get nothing when I attempt to print any values. Where ever there is confusion to be had... I'll be there.

      Well, here are some debugging tips (note I did not test the code...). First, I only gave you a section of the code. I'm sure you can see that I didn't open the file yet, which obviously needs to be done. Also, it's a good idea to use strict at all times, and either use warnings if you're running perl 5.6 or later, or add the -w switch when running your script.

      My personal favorite for monitoring data structures is to use Data::Dumper to view what's going on:

      use Data::Dumper; print Dumper(\%regions);

      will give you output of what the %regions hash currently looks like.

      HTH

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://141233]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-25 08:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found