in reply to Better way to search an Array?

Sometimes it can be better to do it externally.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1130122 use strict; use warnings; my $want = undef; my @idxArray; # sort files together :) open my $fh, '-|', '/usr/bin/sort -n d.master d.index' or die "$! open +ing sort"; while(<$fh>) { if( /^(\d+)$/ ) { $want = $1; } elsif( $want && /^$want\s+(\S+)/ ) { push @idxArray, $1; $want = undef; } else { $want = undef; } } close $fh or die "$! on close of sort"; use YAML; print Dump \@idxArray; # for debugging __END__

I hope you're on an OS with a decent sort :)

Replies are listed 'Best First'.
Re^2: Better way to search an Array?
by doubleqq (Acolyte) on Jun 22, 2015 at 22:33 UTC
    I know it's a week late, but I just wanted to say; Thank you!! Your suggestion is amazing, and versatile too. I can't believe I have been limping without using even thinking about using sort.