http://qs321.pair.com?node_id=120742

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

This works fine for outputing to file, however, still unable to assign to array without opening the output afterwards. Would be good if I could set overall match to array at the same time as sending to output.
#!perl.exe my $filename="e:\test.out.txt"; my $output="e:\out.txt"; sub body{ if (-r $filename) { open(FILE,"<$filename"); open(OUTPUT, "> $output"); while(<FILE>) { chomp; if (/Datasource:/) { print OUTPUT "\n", $_; } if (/Passed:/) { $section="passed"; next; } elsif (/Failed:/) { $section="failed"; print OUTPUT "\n",$_; next; + } elsif (/Exception:/) { $section="except"; print OUTPUT "\n",$_; ne +xt; } if ($section ne "passed") { if (/(test\.)|(Summary:)/) { print OUTPUT "\n",$_; } } } close(FILE); close(OUTPUT); } else { print("cant read file ",$filename,"\n"); } }

Edit: davorg - added code tags

Replies are listed 'Best First'.
Re: simple regex (again)
by MZSanford (Curate) on Oct 23, 2001 at 15:02 UTC
    You really need to use <code> tags around any code you post. If you did, it would look like this :
    #!perl.exe my $filename="e:\test.out.txt"; my $output="e:\out.txt"; sub body{ if (-r $filename) { open(FILE,"<$filename"); open(OUTPUT, "> $output"); while() { chomp; if (/Datasource:/) { print OUTPUT "\n", $_; } if (/Passed:/) { $section="passed"; next; } elsif (/Failed:/) { $section="failed"; print OUTPUT "\n",$_; next; + } elsif (/Exception:/) { $section="except"; print OUTPUT "\n",$_; ne +xt; } if ($section ne "passed") { if (/(test\.)|(Summary:)/) { print OUTPUT "\n",$_; } } } close(FILE); close(OUTPUT); } else { print("cant read file ",$filename,"\n"); } }
    I had trouble understanding the question, but the blank while () is probably not correct, i think maybe you want while (<FILE>).
    i had a memory leak once, and it ruined my favorite shirt.
Re: simple regex (again)
by davorg (Chancellor) on Oct 23, 2001 at 15:05 UTC

    When you posted that node, did you see some text that said:

    If something looked unlike you expected it to you might need to check out Writeup Formatting Tips

    Which bit of that didn't you understand? Or do you always write your code like that?

    It's much easier to help you if you surround your code in <code> ... </code> tags.

    Update: Yeah, OK. I mistyped "bit" as "did". Corrected :(

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

      That's not the all of it, I have reformatted the original question (see below) - and I still am feeling unsure what it is the original post actually wants...
      And there appears to be numerous other helpful suggestions to improve the code.

      I would suggest that the original poster tried SEARCHING the site to look for some relevant answers, and avoid a lot more RTFM answers...

       

      __REFORMATTED MESSAGE__

      This works fine for outputing to file, however, still unable to assign to array without opening the output afterwards.
      Would be good if I could set overall match to array at the same time as sending to output.

      #!perl.exe my $filename="e:\test.out.txt"; my $output="e:\out.txt"; sub body { if (-r $filename) { open(FILE,"<$filename"); open(OUTPUT, "> $output"); while() { chomp; if (/Datasource:/) { print OUTPUT "\n", $_; } if (/Passed:/) { $section="passed"; next; } elsif (/Failed:/) { $section="failed"; print OUTPUT "\n",$_; next; } elsif (/Exception:/) { $section="except"; print OUTPUT "\n",$_; next; } if ($section ne "passed") { if (/(test\.)|(Summary:)/) { print OUTPUT "\n",$_; } } } close(FILE); close(OUTPUT); } else { print("cant read file ",$filename,"\n"); } }

      -- Graq's Home Page

        Your reformatting has missed the fact that the AM actually had while (<FILE>). I've now added <code> tags to the original post which makes it clearer.

        But not necessarily very clear :)

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you don't talk about Perl club."

      "Which did of that" - sorry don't understand your question.
      Ok Ok. Apologies for not tagging and for not checking the paste (blank while).
Re: simple regex (again)
by Zecho (Hermit) on Oct 23, 2001 at 16:37 UTC
    have you given any thought to the fact that \t is a tab character in perl?

      Ooh. Good catch.

      Another good reason for not using doubled quoted strings when single quotes will do.

      --
      <http://www.dave.org.uk>

      "The first rule of Perl club is you don't talk about Perl club."

Re: simple regex (again)
by davorg (Chancellor) on Oct 23, 2001 at 17:24 UTC

    OK, I think I know what you're asking. You want the output to go into an array as well as the file. If that's right, then this will do what you want.

    #!perl.exe -w use strict; my $filename='e:\test.out.txt'; my $output='e:\out.txt'; sub body { if (-r $filename) { open(FILE,"<$filename") || die "$filename: $!\n"; my (@output, $section); while(<FILE>) { chomp; if (/Datasource:/) { push @output, "\n$_"; } if (/Passed:/) { $section="passed"; next; } elsif (/Failed:/) { $section="failed"; push @output, "\n$_"; next; } elsif (/Exception:/) { $section="except"; push @output, "\n$_"; next; } if ($section ne "passed") { if (/(test\.)|(Summary:)/) { push @output, "\n$_"; } } } close(FILE); open(OUTPUT, "> $output") || die "$output: $!\n"; print OUTPUT @output; close(OUTPUT); } else { print("cant read file $filename\n"); } }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."