All,

I have a file named... uhh... lets call it desired_file.txt and there is a file with this same name in a directory path that is specific to the day of year it was created on. (yes i know this is a "hill-billy database i'm working with but it's what i'm stuck with so i have to deal with it......)

i.e. the path to a few of these are:

/home/myfiles/day_of_year/255/data/JPF_374362/desired_file.txt

/home/myfiles/day_of_year/256/data/EJF_264827/desired_file.txt

/home/myfiles/day_of_year/257/data/FFE_387392/desired_file.txt

...

/home/myfiles/day_of_year/###/data/desired_file.txt

respectively for days of year 255, 256, 257.

I have a foreach loop that is accessing an array where each element of that array contains the number for the day of year it wishes to access data for (and replaces ### in the path above to find the desired_file.txt for that day).

in the foreach, my script pulls the day of year from the array element in question, then attempts to open the "desired_file.txt" in the path associated with that day of year. the code is something like:

EDIT: putting in the actual code

<p>Okay, [actual code]:</p> <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

end edit

so what ends up happening is, the first time through, the script does exactly what i want. it find the file, opens it, munches on it and then goes back to the foreach for the next day of year value in the array. Thing is, the next time through, it die's saying it can't find the file.

Even if the first and second elements of the @days_of_year_array are THE SAME DAY OF YEAR, it works the first time but then the second time the print statement after the glob shows that $info_desired_filepath is empty.....

oh, and one last thing, i am using v5.8.3 built for sun4-solaris

HALP!

thanks folks

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.