MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:
Suppose I have a text file in the following format:
<item><key1>someValue</key1><key2>someValue</key2><key3>someValue</key +3><key4>someValue</key4></item><item><key1>someValue</key1><key2>some +Value</key2><key3>someValue</key3><key4>someValue</key4></item><item> +<key1>someValue</key1><key2>someValue</key2><key3>someValue</key3><ke +y4>someValue</key4></item> ... etc.
I need to run reports on this data (do a running total on some of the values; output some of these values to the screen as a table, etc.), and I assume the best way to do that is to turn it into an array of hashes: one item for each element of the array, and then the hashes will be the key names/values of each individual item. What is the best way to do this? I can read the data in like so:
my $filename = "/path/to/my/inputfile.txt"; my $filehandle; if (not open $filehandle, '<', $filename) { print "ERROR: Could not open file $filename\n"; return 0; } while (my $line = $filehandle) { chomp($line); # lots of stuff goes here!!! }
I guess my first step is to create an array by splitting on "</item><item>" right at the "><" point (so "</item>" goes to element n and "<item>" goes to n + 1). How do I do that?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I create an array of hashes from an input text file?
by Anonymous Monk on Nov 10, 2011 at 22:42 UTC | |
|
Re: How do I create an array of hashes from an input text file?
by Kc12349 (Monk) on Nov 10, 2011 at 23:30 UTC | |
|
Re: How do I create an array of hashes from an input text file?
by mrstlee (Beadle) on Nov 11, 2011 at 08:05 UTC | |
by MrSnrub (Beadle) on Nov 11, 2011 at 19:14 UTC | |
by Kc12349 (Monk) on Nov 11, 2011 at 23:48 UTC | |
by MrSnrub (Beadle) on Nov 12, 2011 at 00:44 UTC | |
by Kc12349 (Monk) on Nov 16, 2011 at 17:46 UTC |