in reply to Re^2: Perl: Directoriesand files
in thread Perl: Directoriesand files

If that is really what you want to do then you can change the 'while' loop to a simple 'if', because that's what it effectively is. The while loop will never be executed more than once, because if $content is anything but $x the while loop will exit immediately

if ($content == $x){ print ($content, $content1, $content2, $content3, $content4); $x++; }

You don't need the close as the files are automatically closed on program exit or reopening

Replies are listed 'Best First'.
Re^4: Perl: Directoriesand files
by craziestfire73 (Initiate) on Mar 30, 2011 at 11:11 UTC
    Yes, I understand what you mean. Now is there a way I can read all the files in one code, instead of opening one by one? Thanks for the assistance

      Yes there is:

      my @content; foreach ('','.1','.2','.3','.4') { open(my $fh,'<',$string.$_) or die "Could not open $string.$_ : $!\n +"; push @content, scalar <$fh>; } if ($content[0] == $x){ print @content; $x++; }

      UPDATE: removed bug. UPDATE2: removed another bug found by anon monk

        Context matters :) use
        scalar <$fh>; scalar readline $fh;
        to read a SINGLE line from each file