use strict; use warnings; use Data::Dumper; my @fields; my @data; while (<DATA>) { chomp; if ( /^#/ ) { @fields = split / +- +/; shift @fields; next; } next if /^$/; my @values = split / +- +/; my $index = shift @values; for my $i (0..$#fields) { $data[$index-1]->{$fields[$i]} = $values[$i]; } } print Dumper (\@data); __DATA__ # - size - reads - writes - read IOPs - write IOPs 1 - 512 - 1000 - 0 - 2000 - 0 2 - 1024 - 0 - 1000 - 0 - 2000 3 - 2048 - 500 - 500 - 1000 - 1000 # - Time Stamp 1 - 2005-05-02 18:16:36:589 2 - 2005-05-03 18:16:36:589 3 - 2005-05-04 18:16:36:589 # - Comments 1 - This test failed 2 - This test passed 3 - This test needs more data.
Output:
$VAR1 = [ { 'Comments' => 'This test failed', 'read IOPs' => '2000', 'reads' => '1000', 'write IOPs' => '0', 'Time Stamp' => '2005-05-02 18:16:36:589', 'writes' => '0', 'size' => '512' }, { 'Comments' => 'This test passed', 'read IOPs' => '0', 'reads' => '0', 'write IOPs' => '2000', 'Time Stamp' => '2005-05-03 18:16:36:589', 'writes' => '1000', 'size' => '1024' }, { 'Comments' => 'This test needs more data.', 'read IOPs' => '1000', 'reads' => '500', 'write IOPs' => '1000', 'Time Stamp' => '2005-05-04 18:16:36:589', 'writes' => '500', 'size' => '2048' } ];


holli, /regexed monk/

In reply to Re: Parsing a csv file with multiple data records in it (generated from Window's IOMeter) by holli
in thread Parsing a csv file with multiple data records in it (generated from Window's IOMeter) by TASdvlper

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.