Popcorn Dave has asked for the wisdom of the Perl Monks concerning the following question:
I've got 2 files containing customer names and year to date purchases. The files should each contain the same customers, although the second file may have new entries or have lost entries. My thought was to put this in to an anonymous hash so that I could match up customers with their YTD purchases. So far so good. (The data is comma delimited with quotes around the customer name, hence the regex.)
The problem arose when I tried to use the following code:
open FH, $file1; while (chomp($line = <FH>)){ $line =~ s/"//g; ($key,$ytd{$key}->{oct01}) = split(',',$line); } close FH;
My results were that I would get the first line's YTD value out, but not the customer. Next line would have the first line's customer and the second line's YTD value. Very odd.
However when I changed to this:
open FH, $file1; while (chomp($line = <FH>)){ $line =~ s/"//g; ($key,$temp) = split(',',$line); $ytd{$key}->{oct01} = $temp; } close FH;
all worked like a champ.
So this brings me to my quest for some understanding and instruction. Is it possible to split file data in to an anonymous hash? I really don't understand why it didn't work assigning the value to the anon hash directly. I'd rather do it in the one line rather than have to use the temp variable.
There is no emoticon for what I'm feeling now.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting file data in to anonymous hashes
by jdporter (Paladin) on Dec 13, 2002 at 18:38 UTC | |
|
Re: Splitting file data in to anonymous hashes
by MarkM (Curate) on Dec 13, 2002 at 17:55 UTC | |
|
Re: Splitting file data in to anonymous hashes
by Mr. Muskrat (Canon) on Dec 13, 2002 at 17:55 UTC | |
|
Re: Splitting file data in to anonymous hashes
by Eimi Metamorphoumai (Deacon) on Dec 13, 2002 at 17:55 UTC | |
by Popcorn Dave (Abbot) on Dec 13, 2002 at 18:23 UTC | |
|
Re: Splitting file data in to anonymous hashes
by CountZero (Bishop) on Dec 13, 2002 at 18:42 UTC |