Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Filehandle problem

by amma (Novice)
on May 07, 2013 at 11:09 UTC ( #1032459=perlquestion: print w/replies, xml ) Need Help??
amma has asked for the wisdom of the Perl Monks concerning the following question:

open (my $rfh,'<', $entry) or die "cannot open > $entry: $!"; open(my $new, '>', "$name\_new.txt") or die "cannot open: $!"; my @data = <$rfh>; foreach my $l (@data) { if ($l =~ m/$signal2\(\d+\).*\s*<=\s*$signal1\(\d+\).*/){ my $i; for($i=0;$i<7;$i++){ my $j = $i + 1; print $new "$signal2\_rrfa_$label($j) <= $signal1\_rrfa_$label($i);\n" +; } $required_line = $&; } else{ print $new $l; } } close($new); close($rfh2);

My new file does not contain the lines from the original file when the if condition fails ( I mean the else part is not executed). The new file has only lines when if conditioned passed.

Replies are listed 'Best First'.
Re: Filehandle problem
by Random_Walk (Prior) on May 07, 2013 at 11:18 UTC

    What is the variable $required_line for? What are in $signal[1|2] My guess is that this is a fragment of a larger script. Can you give an example of some input lines as well please?

    Why are you using $1 as the variable to write? What do you think happens to this variable during pattern match failure?


    Pereant, qui ante nos nostra dixerunt!

      Hi, I am storing the value whatever is matched in the if condition in the $required_line variable. The data file is too large of 40k lines.

        Just a couple of lines of the input demonstrating a match and a fail would help. Try replacing the use of $1 with something other than a special variable, I typically use $line. Then see how you get on.


        Pereant, qui ante nos nostra dixerunt!
Re: Filehandle problem
by jnyman (Acolyte) on May 07, 2013 at 11:43 UTC
    Are you sure you are not trying to read the new file before the file handle has been closed? (and autoflush $| has not been set for $new)
Re: Filehandle problem
by graff (Chancellor) on May 07, 2013 at 23:22 UTC
    After I applied appropriate indentation on the OP code (so I could read it), I noticed something odd about your print statement:
    print $new $l;
    Normally, the 'print' function takes a list, which means that there should be commas between its args. I didn't have your data files on hand, so I did a couple quick one-liner experiments:
    perl -le '$x="foo"; $y="bar"; print $x $y' perl -le '$x="foo"; $y="bar"; print $x, $y'
    The first of those (without a comma) didn't print anything at all. The second one printed "foobar", as expected.

    In the first try, without the comma, the 'print' function is trying to interpret $x as a file handle, and failing. In the absence of use warnings;' there wasn't even a hint about the problem, but a third experiment was more instructive:

    perl -wle '$x="foo"; $y="bar"; print $x $y' print() on unopened filehandle foo at -e line 1.
    Given that sort of condition, the default (prudent) behavior is for the print function to do nothing at all.

    Sorry - total brain fart on on the stuff above. I forgot that $new was supposed to be a file handle. Oh well.

    UPDATE: Random Walk has apparently solved your problem, but here's a slightly different version of the same solution anyway... (possibly a bit less different from the OP code, possibly a little simpler, and, well, at least this allows my reply to have some usable content):

    use strict; use warnings; my $name = "name"; # open( my $rfh,'<', $entry ) or die "cannot open > $entry: $!"; open( my $new, '>', "$name\_new.txt" ) or die "cannot open: $!"; my $signal1 = "s_abc_out"; my $signal2 = "s_abc_in"; my $label = "label"; while ( <DATA> ) { # would be <$rfh> for you if ( /$signal2\(\d+\).*\s*<=\s*$signal1\(\d+\).*/ ) { for my $i ( 0 .. 6 ) { my $j = $i + 1; print $new "$signal2\_rrfa_$label($j) <= $signal1\_rrfa_$l +abel($i);\n"; } } else{ print $new $_; } } __DATA__ ................................. s_abc_in(1450) <= s_abc_out(324); s_abc_in(1451 <= s_abc_out(1450); .................................
      That was not the solution. If you test the original code with some debug values on the variables, the lower branch is writing to the file as supposed.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032459]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-10-24 05:09 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (286 votes). Check out past polls.