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

how can I create this program more concise. I'm new to PEARL so I'm still learning the tricks. Any assistance would be really appreciated. The code works, I just want to make it more neat and short. I feel like I'm writing too much code for what it does. I keep experimenting with regex but I can't seem to make it work using regex.

#!/usr/bin/perl opendir(CURRENT,"."); @list = readdir(CURRENT); closedir(CURRENT); my $string = "messages"; my $x = 0; foreach $item(@list){ if($item =~ /^$string\.*\d*/){ open(FILE,"$string"); open(FILE1,"$string.1"); open(FILE2,"$string.2"); open(FILE3,"$string.3"); open(FILE4,"$string.4"); $content = <FILE>; $content1 = <FILE1>; $content2 = <FILE2>; $content3 = <FILE3>; $content4 = <FILE4>; while($content == $x){ print ($content, $content1, $content2, $content3, $content4); $x++; close(/FILE\d*/); } } }

Replies are listed 'Best First'.
Re: Perl: Directoriesand files
by Anonymous Monk on Mar 30, 2011 at 10:19 UTC
      I'd first write down what I want, then worry about details like what boilerplate to use. It's like getting from A to B, first worry about the type of transportation to use, before considering what colour paint the vehicle should have.
        It's like getting from A to B, first worry about the type of transportation to use, before considering what colour paint the vehicle should have.

        Actually, first you need a drivers license, or new drivers need a learners permit, so the boilerplate is good

        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.
Re: Perl: Directoriesand files
by JavaFan (Canon) on Mar 30, 2011 at 10:21 UTC
    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.

      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.

      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