in reply to Sorting with perl
Use a Hash!
--#!/usr/bin/perl use strict; use warnings; my $guidefile = './guide'; my $flatfile = './flat'; my $delimiter = qr{\s*\|\s*}; my (%guide,@flat); open GUIDE,'<',$guidefile or die "E: open($guidefile): $!"; {my $i = 0; while(<GUIDE>){ chomp; $guide{$_}=$i++ } } close GUIDE; open FLAT,'<',$flatfile or die "E: open($flatfile): $!"; while( <FLAT> ){ chomp; push @flat,[split /$delimiter/]; } close FLAT; foreach( sort { ($guide{ $b->[0] }||0) <=> ($guide{ $a->[0] }||0) } @ +flat ){ print join("\t", exists($guide{$_->[0]}) ? $guide{$_->[0]} : '-', @$_),"\n"; }
|
|---|