Perl - search a word and print rest of line into variables -
i have easy question. how can make perl read file, search specific word, , if finds match, want print rest of line individual variables.
the ascii file (called "region_list" wish search contains has 3 lines:
hawaii 40 5 -140 -180
samoa -5 -25 -165 165
pacific 70 -65 290 110
here's code have far, doesn't seem working:
#!/usr/bin/perl -w # # required libraries use date::calc qw(:all); use date::manip; use math::trig; use warnings; use time::local; use posix 'strftime'; use lib '/usr/bin'; use cwd qw(); @region = ("hawaii", "samoa", "pacific"); open $listreg1, "$bin_dir/region_list" or die "could not open: $!"; ($reg2,$max_lat, $min_lat, $max_lon, $min_lon) = split(" ",$listreg1); if ($region eq $reg2) { print "lucreg $region $reg2 $max_lat, $min_lat, $max_lon, $min_lon \n"; } close $listreg1;
with perl 5.10 or higher, can use smart match ( ~~ ) operator match presence of value array
open $listreg1, '<', "$bin_dir/region_list" or die "could not open: $!"; while( $line = <$listreg1> ) { chomp $line; ($reg2, $max_lat, $min_lat, $max_lon, $min_lon) = split/ +/, $line; if( $reg2 ~~ \@region ) { print "lucreg $region $reg2 $max_lat, $min_lat, $max_lon, $min_lon\n"; } } close $listreg1;
note, @joel says, smartmatch operator marked experimental in 5.18, it's better choice use cpan module list::moreutils or grep.
Comments
Post a Comment