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

The following snippet is not looping.
sub get_section { print join ':', @_ ; for (@_) { print ; /\.dat/ && do {$file = join '/', ($docdir, $_); $pubdate = substr ($_, 1, 4); print "$file \:\: $pubdate\n" ; @dat = get_file($file) ; sort_dat(@dat) ; print_sect() ; } ; } }
Looking at the above, and assuming that my dats are E0209.dat, F0210.dat, and H0211.dat, I get:
E0209.dat:F0210.dat:H0211.dat (First print statement)
E0209.dat (Second print statement)
/data/E0209.dat :: 0209 (End of loop)

Shouldn't the "for" take care of looking at every value in the array?? If not, I am without my Perl Refs and would appreciate a quick fix. Thanks, Hammy

Replies are listed 'Best First'.
(Guildenstern) Re: Not looping... Am I a moron?
by Guildenstern (Deacon) on Feb 21, 2001 at 00:21 UTC
    Cutting and pasting this code and calling get_section with your specifed parameters works for me. Of course, I had to comment out several lines since they refer to variables or subs that I don't have code for. My modified version looks like this:
    get_section("E0209.dat","F0210.dat","H0211.dat"); sub get_section { print join ':', @_ ; for (@_) { print ; /\.dat/ && do {my $file = $_; my $pubdate = substr ($_, 1, 4); print "$file \:\: $pubdate\n" ; } ; } }

    As I said above, this works fine for me, and prints out all three values. The only thing that I can think of is that somewhere in get_file, sort_dat,or print_sect is a problem that's either hanging the script or exiting. You didn't indicate that there was anything abnormal about the execution, so I would look first at anything that might be causing the script to exit, such as a die;.

    Of course, I could be way off in left field here, but it's a start.

    Guildenstern
    Negaterd character class uber alles!
      Thanks! that was it...

      The ol' open or die.... in the print sect....
      thanks

      And the other question is answered too... I am a moron. 8>)

(tye)Re: Not looping... Am I a moron?
by tye (Sage) on Feb 21, 2001 at 00:27 UTC

    Based on other replies, my guess is that since get_section() is working directly on @_, some other code is clobbering the values in the "variables" that you passed to get_section().

    Change the top of the function to:

    my @files= @_; for( @file ) {
    and see if that fixes things.

            - tye (but my friends call me "Tye")
Re: Not looping... Am I a moron?
by bliz (Acolyte) on Feb 21, 2001 at 00:23 UTC
    I took your code and removed the calls to the missing subs, since I don't know what they do (as they aren't posted), and it works fine for me... as follows:
    #!/usr/bin/perl -w get_section('E0209.dat','F0210.dat','H0211.dat'); sub get_section { $docdir = "./"; print join ':', @_ , "\n"; for (@_) { print ; /\.dat/ && do { $file = join '/', ($docdir, $_); $pubdate = substr ($_, 1, 4); print "$file \:\: $pubdate\n" ; } ; } }
    And I add back in your other calls, and add stub code, and it still works fine for me. ??
    --
    bliz
    
Re: Not looping... Am I a moron?
by Hot Pastrami (Monk) on Feb 21, 2001 at 00:16 UTC
    Perhaps your problem is because there is no "while" statement on your "do" block ... you may want this kind of structure, roughly:
    for (@_) { do { # stuff to do here } while (/\.dat/); }
    ...Or, if you aren't trying for a do/while loop, just testing for ".dat" in the string, maybe you want:
    for (@_) { if (/\.dat/) { # stuff to do here } }
    You may also want to replace /\.dat/ with /(\.dat)$/ to make sure that's the file extension, so "test.dat.old" wouldn't be a match.

    Update: I thumbed through the Camel and discovered there is a "do BLOCK" form I wasn't aware of, so my answer may be way off from what you're looking for.

    Hot Pastrami