in reply to Perl: Directoriesand files

I've no idea what your program is supposed to do, but, if there aren't any failures (which you aren't checking for), then, for each file in the directory that looks like 'messages.\d*', you read the first lines of the files 'messages', 'messages.1', 'messages.2', 'messages.3', 'messages.4', and then if the first line of 'messages' equals $x (numerically), you print the first lines, increment $x, and try to close a handle you've never opened.

Is that really what you want? Doubtful. But I do not know what you want to do.

Replies are listed 'Best First'.
Re^2: Perl: Directoriesand files
by JavaFan (Canon) on Mar 30, 2011 at 11:37 UTC
    Since the content of the files doesn't change, why not read the first line of 'message', and if that's numerically 0, (so, it could be "foo", or "0 moon", but not "123 strawberry"), read the first lines of 'message.1', 'message.2', 'message.3', 'message.4', and then print the five lines?

    There doesn't seem to be a need for either loop.

Re^2: Perl: Directoriesand files
by craziestfire73 (Initiate) on Mar 30, 2011 at 10:32 UTC
    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.

      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