in reply to Grep a word and print the filename

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,

Replies are listed 'Best First'.
Re^2: Grep a word and print the filename
by alokranjan (Acolyte) on Aug 15, 2015 at 10:37 UTC
    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
Re^2: Grep a word and print the filename
by alokranjan (Acolyte) on Aug 15, 2015 at 21:57 UTC
    No problem mate. I am also new here. I just wanted to correct you nothing else. cheers and thanks :)