alokranjan has asked for the wisdom of the Perl Monks concerning the following question:
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.
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Grep a word and print the filename
by Athanasius (Archbishop) on Aug 15, 2015 at 13:50 UTC | |
|
Re: Grep a word and print the filename
by poj (Abbot) on Aug 15, 2015 at 12:37 UTC | |
by alokranjan (Acolyte) on Aug 15, 2015 at 21:55 UTC | |
|
Re: Grep a word and print the filename
by Athanasius (Archbishop) on Aug 15, 2015 at 09:40 UTC | |
by alokranjan (Acolyte) on Aug 15, 2015 at 10:37 UTC | |
by alokranjan (Acolyte) on Aug 15, 2015 at 21:57 UTC | |
|
Re: Grep a word and print the filename (File::Find::Rule)
by Anonymous Monk on Aug 15, 2015 at 22:08 UTC |