Instead of using an array for ids it may be better to use a hash, something like this:
#! perl -- use strict; use warnings; @ARGV == 3 or die "usage: $0 ID-file CSV-file CSV-output\n"; my $forextract = shift; # IDs to be extracted, 1 per row , further +info tab delimited #header[0] is matched against header of dtp_al +l my $dtp_all = shift; # csv CANCER60xxx.lis.... my $dtp_my = shift; # csv output print "Warning: The file with items to extract has to have a header - +the first column name will be matched.\n"; open my $IDS, '<', $forextract or die "Opening $forextract failed: $!" +; my ( $myID, %ids ); while ( <$IDS> ) { /^\s*([^\t]+?)\s*\t/ or next; if ( $. == 1 ) { $myID = $1; } else { $ids{ $1 } = 1; } } close $IDS; open my $ALL, '<', $dtp_all or die "Opening $dtp_all failed: $!"; open my $MY, '>', $dtp_my or die "Opening $dtp_my failed: $!"; # copy the header my $header = <$ALL>; print $MY $header; my @colnames = split /,/, $header, -1; my $colN; for my $col ( 0 .. $#colnames ) { if ( $colnames[ $col ] eq $myID ) { $colN = $col; last; } } unless ( defined $colN ) { die "Column $myID not found.\n"; } # parse the input line by line, when ids matched write the line into t +he output while ( my $line = <$ALL> ) { my $col = ( split /,/, $line, -1 )[ $colN ]; s/^\s+//, s/\s+$// for $col; if ( exists $ids{ $col } ) { print $MY $line; } } close $MY or die "Not completed $dtp_my\n"; close $ALL;

In reply to Re: Uninitialized value in string eq error but I initialized by jwkrahn
in thread Uninitialized value in string eq error but I initialized by karpatov

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.