I have two files 1.) US 2.) UK containing the name of the cities of respective country. Now I would like to print the name of file itself (means country name) by passing the name of city to a subroutine. But value is not changing.
Below is my code.#!/usr/bin/perl use File::Find; use strict; my $country; sub getCountry{ my $name = shift; chomp($name); my $dir ='/home/alok/countries/'; find(\&txtsearch, $dir); sub txtsearch { if(-f $_) { open my $file, "<", $_ or do { warn qq(Unable to open "$File::Find::name": $!); #c +hecks permissions return; }; while(<$file>) { if (/\Q$name\E/) { my $cname = "$File::Find::name"; #prints locati +on of file my @chunks = split '/', $cname; $country = $chunks[4]; print "$country\n"; return ; #stops searching once found } } } } } getCountry("NY"); getCountry("London");
Any help/assistance would be highly appreciated.
ThanksIn reply to Grep a word and print the filename by alokranjan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |