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

Thanks, The code does actually what I want, I just want to learn to make it more neat. it seems and it feels like I'm writing to much codes for what it does. I tried using regex to try to make the code more neat but I guess I'm going the wrong way.

Replies are listed 'Best First'.
Re^3: Perl: Directoriesand files
by jethro (Monsignor) on Mar 30, 2011 at 10:44 UTC

    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

      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