in reply to Extracting lines starting with a pattern from an array
I had thought of reading the second file into a hash with the IDs as key and the rest of the fields as values but I can't find a way to do it due to the multiple fields per line.It might be possible, by creating a hash of arrays. But it seems easier to do it the other way round, to store the first file in a hash. Then iterate over the second file and check whether the given id exists in the hash. If you need the output sorted, you might store the line number ($.) from the first file as the value in the first hash, and sort by that at the end.
Update: Solution #2:
#!/usr/bin/perl use warnings; use strict; use Text::CSV; open my $LST, '<', 'ids.lst' or die $!; my %id; while (<$LST>) { chomp; $id{$_} = $.; } my @out; my $csv = 'Text::CSV'->new({ sep_char => "\t", eol => "\n", }); open my $CSV, '<', 'file.csv' or die $!; while (my $row = $csv->getline($CSV)) { push @out, [ $id{ $row->[0] }, $row ] if exists $id{ $row->[0] }; } $csv->print(*STDOUT, $_->[1]) for sort { $a->[0] <=> $b->[0] } @out;
Update #2: Solution #1:
#!/usr/bin/perl use warnings; use strict; use Text::CSV; my %record; my $csv = 'Text::CSV'->new({ sep_char => "\t", eol => "\n", }); open my $CSV, '<', 'file.csv' or die $!; while (my $row = $csv->getline($CSV)) { $record{ $row->[0] } = $row; } open my $LST, '<', 'ids.lst' or die $!; my %id; while (<$LST>) { chomp; print $record{$_} if exists $record{$_}; }
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
|
|---|