alokranjan has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

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
Alok

Replies are listed 'Best First'.
Re: Grep a word and print the filename
by Athanasius (Archbishop) on Aug 15, 2015 at 13:50 UTC

    Hello again alokranjan,

    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. (Emphasis added.)

    I notice that you use strict but not warnings. When I run your code with perl -wc I get:

    23:34 >perl -wc 1342_SoPW.pl Variable "$name" will not stay shared at 1342_SoPW.pl line 39. 1342_SoPW.pl syntax OK 23:35 >

    And with diagnostics:

    23:35 >perl -Mdiagnostics -wc 1342_SoPW.pl Variable "$name" will not stay shared at 1342_SoPW.pl line 39 (#1) (W closure) An inner (nested) named subroutine is referencing a lexical variable defined in an outer named subroutine. When the inner subroutine is called, it will see the value of the outer subroutine's variable as it was before and during the *f +irst* call to the outer subroutine; in this case, after the first call t +o the outer subroutine is complete, the inner and outer subroutines will + no longer share a common value for the variable. In other words, the variable will no longer be shared. This problem can usually be solved by making the inner subroutine anonymous, using the sub {} syntax. When inner anonymous subs tha +t reference variables in outer subroutines are created, they are automatically rebound to the current values of such variables. 1342_SoPW.pl syntax OK 23:36 >

    This explains why the value is not changing: on the first call to getCountry the variable $name is set to “NY” in both getCountry and txtsearch. But on the second call, $name becomes “London” in getCountry but remains “NY” within txtsearch. This is easily confirmed by adding print statements at appropriate points within these subs.

    Moral: always use warnings !

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Grep a word and print the filename
by poj (Abbot) on Aug 15, 2015 at 12:37 UTC

    Take a look at this node Why I hate File::Find and how I (hope I) fixed it and try this

    #!/usr/bin/perl use File::Find; use strict; my $country; sub getCountry{ my $locn = shift; my $dir ='c:/temp/countries/'; find(sub{ txtsearch($locn) }, $dir); } sub txtsearch { my $locn = shift; if (-f $_) { open my $fh, "<", $_ or do { warn qq(Unable to open "$File::Find::name": $!); #checks permiss +ions return; }; while(<$fh>) { if (/\Q$locn\E/) { my $country = (split '/', $File::Find::name)[-1]; print "$locn found in $country\n"; last ; #stops searching once found } } } }; getCountry("NY"); getCountry("London");
    poj
      Thanks...
Re: Grep a word and print the filename
by Athanasius (Archbishop) on Aug 15, 2015 at 09:40 UTC

    Hello alokranjan,

    my $cname = "$File::Find::name"; #prints location of file my @chunks = split '/', $cname; $country = $chunks[4];

    Update 1: Added the code snippet quotation.

    If the full path+filename is /home/alok/countries/London, then the call to split produce a 4-member list. But the fourth element (London) of the array @chunks is $chunks[3], because array indices start at zero.

    Update 2: alokranjan is correct, of course: the first element is the empty string. Note to self: always test before posting! Sorry for the noise. :-(

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Hi,

      Actually field separator is / and the first element would be blank. hence the position of London will be 5th not 4th.

      Anyways thanks for your effort and reply. Regards,
      Alok
      No problem mate. I am also new here. I just wanted to correct you nothing else. cheers and thanks :)
Re: Grep a word and print the filename (File::Find::Rule)
by Anonymous Monk on Aug 15, 2015 at 22:08 UTC
    use File::Find::Rule qw/ find rule /; my @files = find( file => grep => qr{\Q$name\E}, in => $dir ); my $cname = $files[0];