in reply to Globbing for file in an unkown directory path only works first time in foreach loop?

prove it :) show that your story is true with code and output from that code
#!/usr/bin/perl -- use strict; use warnings; use Path::Tiny qw/ path /; use File::Find::Rule qw/ find rule /; path( "goner/day_of_year/$_/data/desired_file.txt" )->touchpath for 1 +.. 3; print "$_\n" for find( file => name => 'desired_file.txt', in => 'gone +r' ); path( "goner" )->remove_tree; print "$_\n" for find( file => name => 'desired_file.txt', in => 'gone +r' ); __END__ goner/day_of_year/1/data/desired_file.txt goner/day_of_year/2/data/desired_file.txt goner/day_of_year/3/data/desired_file.txt
  • Comment on Re: Globbing for file in an unkown directory path only works first time in foreach loop?
  • Download Code

Replies are listed 'Best First'.
Re^2: Globbing for file in an unkown directory path only works first time in foreach loop?
by MarsRover (Novice) on Jan 29, 2016 at 19:53 UTC

    Okay, actual code:

    #!/tps/bin/perl -s my @day_of_year = (4257,4257,4258,4259); foreach (@day_of_year) { print"\nskimming for the day of year number:\n"; if ($_ =~ /(\d{4})/){ $day_of_year = $1; print "looking for things in $day folder /home/myfiles +/day_of_year/$day_of_year/*/desired_file.txt\n"; my $info_desired_filepath = glob('/home/myfiles/day_of +_year/'.$day_of_year.'/*/desired_file.txt'); print "$info_desired_filepath \t\t is the file name \n +\n"; <STDIN>; open ($info_FH, '<', $info_desired_filepath) or die "cannot open $info_desired_filepath.... +......\n\n"; print "found a file!!!!!!\n"; chomp(@lines = <$info_FH>); close($info_FH); } }

    actual output:

    skimming for the day of year number:

    looking for things in 4257 folder /home/myfiles/day_of_year/4257/*/desired_file.txt

    /home/myfiles/day_of_year/4257/JKY_63521/desired_file.txt is the file name

    found a file!!!!!!

    skimming for the day of year number:

    looking for things in 4257 folder /home/myfiles/day_of_year/4257/*/desired_file.txt is the file name

    cannot open ..........

    end output

      Okay, actual code:

      close but but not quite

      See how I write code that first generates the requird files for the rest of the code to run?

      You don't actually want me to manually have to go create files do you?

      All that run tells me is that day 4257 doesn't have a file -- you say that is a problem, but how can I tell if the file really exists or not, other than you saying it does, but your code saying it doesn't?

      I don't think you have a file there

        look at the output! it access the same file both runs, just the first time it finds it and the second time through the foreach loop it doesn't. I'll try and write it your way while you look

        notice the array it is scanning through has 4257 as it's first element and it's second. It finds the 4257 file through the foreach the first time, but then one the foreach move to the second element in the array, which happens to be the exact same as the first element, it can't find the same file it just found.

        i tried to mimic your code and i don't have the Tiny module. I'm working with some reallllly antiquated systems here