Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1032541]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2018-06-21 06:52 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.