Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Filehandle problem

by graff (Chancellor)
on May 07, 2013 at 23:22 UTC ( #1032541=note: print w/ replies, xml ) Need Help??


in reply to Filehandle problem

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); .................................


Comment on Re: Filehandle problem
Select or Download Code
Re^2: Filehandle problem
by jnyman (Acolyte) on May 08, 2013 at 07:49 UTC
    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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2014-10-26 02:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (149 votes), past polls