in reply to searching and printing what is wanted
I've been humming and hawing about 179544 in my head all day. It just felt clumsy. So, after watching some excellent commentary in the CB, I was inspired to rewrite this using map and grep. I gotta say I am much happier with this.
#! /usr/bin/perl use strict; use warnings; open SIG, 'signal.txt' ; my @sig = <SIG> ; close SIG ; chomp @sig ; open ORD, 'orderednames.txt' ; my @ord = <ORD> ; close ORD ; my @names = map { split } @ord ; my %names_hash ; $names_hash{$_} = 1 foreach @names ; my @results = grep { !$names_hash{$_} } @sig ; print join( ':', @results ), "\n" ;
Update: Fixed closing of wrong file handle. Thanks theorbtwo!
Update 2: Yep, it keeps getting shorter (Thanks again theorbtwo.)
#! /usr/bin/perl use strict; use warnings; open SIG, 'signal.txt' ; chomp( my @sig = <SIG> ) ; close SIG ; open ORD, 'orderednames.txt' ; my %names_hash ; $names_hash{$_} = 1 foreach map { split } <ORD> ; close ORD ; my @results = grep { !$names_hash{$_} } @sig ; print join( ':', @results ), "\n" ;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: searching and printing what is wanted
by theorbtwo (Prior) on Jul 05, 2002 at 07:19 UTC |