Because this is not a CSV file but plain tab delimited and thus no quotes, I'm not getting out the big guns and solve the problem with plain Perl, just to show how simple it can be.

Step 1: pull out the header.

$_ = <IN>; chomp; my @column = split /\t/;

Step 2: read each line and convert to a hash wit hthe column names as keys:

my @data; while(<IN>) { chomp; my %row; @row{@column} = split /\t/; push @data, \%row; }

That's it: the whole file is read into @data as an array of hashes. I think you probably need more code when using Text::CSV_xs.

As for your final request, the filtering: it depends on whether you want to use the same source for something else as well, and whether the data is huge (pretty meaningless nowadays, as several MB of data is now considered "small"), you can either filter from @data using grep, or test before pushing the current row onto @data.

Assuming the condition can be written as:

$row{'sex'} eq 'F' and $row{'body mass index'} > 40 and $row{blood pre +ssure'} > 135
you can do:
push @data, \%row if $row{'sex'} eq 'F' and $row{'body mass index'} > +40 and $row{blood pressure'} > 135;
or
@filtered = grep { $_->{'sex'} eq 'F' and $_->{'body mass index'} > 4 +0 and $_->{blood pressure'} > 135 } @data;
Note that for the latter a row is a hash ref in $_, while in the former, it's a plain hash in %row.

Perl is one of the very few languages that makes a distinction between the two in syntax, and although it has its advantages (flattening lists is very easy in Perl), the different syntax in both cases is rather annoying, IMHO.


In reply to Re: how to extract data from an array using a condition by bart
in thread how to extract data from an array using a condition by kayj

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.