Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Blank File

by ImpalaSS (Monk)
on Jan 25, 2001 at 22:10 UTC ( [id://54309]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,
This is a little bit of code in the script I am currently working on. Here is my problem, when i print it using just print and the info, it prints on the command line perfectly, like this,
5213aud|5210|PPA0200ScrubOak|5213 5223aud|5222|PPA0366HighKnobMtn|5223 5224aud|5222|PPA0366HighKnobMtn|5224 5225aud|5222|PPA0366HighKnobMtn|5225 5227aud|5226|PPA0101Greentown|5227 5228aud|5226|PPA0101Greentown|5228 5229aud|5226|PPA0101Greentown|5229 5231aud|5230|PPA0221MtYeager|5231 5232aud|5230|PPA0221MtYeager|5232 5233aud|5230|PPA0221MtYeager|5233 5235aud|5234|PPA0231Freeland|5235 5236aud|5234|PPA0231Freeland|5236 5237aud|5234|PPA0231Freeland|5237 5239aud|5238|PPA0034TylerCorner|5239 5240aud|5238|PPA0034TylerCorner|5240 5241aud|5238|PPA0034TylerCorner|5241 5243aud|5242|PPA0209Scranton|5243 5244aud|5242|PPA0209Scranton|5244

only about 1200 of them. But, when i try and print to a file, the file it creats comes up blank? Any suggestions?
@omcees = ("pasys","audsys"); foreach $omc(@omcees){ if ($omc eq "pasys"){$id = "pa"} else{$id = "aud";} $filename = "combine".".txt"; ## May as well test them all now open(FIRST, "/mnt/$omc/ne_data/unload_stats/$currenttime/cel_ntid +.unl") or die "Could not open first\n"; open(SECOND, "/mnt/$omc/ne_data/unload_stats/$currenttime/sit_ntid +.unl") or die "Could not open second\n"; if ($omc eq "pasys"){ open(THIRD, ">/tmp/$filename" ) or die "Could not write third +\n"; } else{ open(THIRD, ">>/tmp/$filename" ) or die "Could not write thir +d\n"; } my (%first, %found); while(<FIRST>) { my ($key, @cols) = split(m#\|# => $_, -1); push(@{$first{$key}}, \@cols); $found{$key} = $.; } close(FIRST); while(<SECOND>) { my ($one, $key, @cols) = split(m#\|# => $_, -1); if (exists $first{$key}) { delete $found{$key}; for (@{$first{$key}}) { print THIRD $_->[0].$id."|$key|$cols[0]"; print THIRD "|".$_->[0]; print THIRD "\n"; } } } close(SECOND); close(THIRD); }


Thanks :)

Dipul

Replies are listed 'Best First'.
Re: Blank File
by tadman (Prior) on Jan 25, 2001 at 23:44 UTC
    Maybe it's just me, but your use of split() is a little unusual:      ... = split(m#\|# => $_, -1); It's not entirely clear why you're using the arrow '=>' operator in there when a comma should do just fine. Also, including a negative number as a parameter seems to request the default behaviour, so it seems redundant and misleading.      ... = split(m#\|#); Since you're using $_ anyway, and the default behaviour, you should be able to reference split as such.

    With respect to your curious problem, it's not clear why reorganizing the statements would cause the program to start "working" all of a sudden, but you could certainly avoid using concatenation and use the list method of print instead:     print THIRD $_->[0],$id,"|",$key,"|",$cols[0],"|",$_->[0],"\n"; That would seem to be functionally equivalent, and more presentable.

      Specifying -1 as the third argument to split causes it to not discard trailing empty results, which is not the default behavior.

              - tye (but my friends call me "Tye")
        True, but the presence or absence of '-1' as a paremeter as used in that program would have no effect, correct? If I understand the documentation correctly, the '-1' is important in very specific circumstances, such as using pop():
        my ($a) = "the|sneaky|camel||"; my (@b, $nil); # Try different parameters with split and report for my $p (undef, -1) { @b = split (/\|/, $a, $p); ($nil) = pop (@b); print "split() with '$p': '$nil' should be empty, ", (length($nil)?" but isn't":"and is"),".\n"; }
        You learn something new every day, it seems.
Re: Blank File
by ImpalaSS (Monk) on Jan 25, 2001 at 22:18 UTC
    Oh yes,
    One more thing. When I had the print statements in this order:
    print THIRD "$_->[0]|$key|$cols[0]"; print THIRD $_->[0].$id; print THIRD "\n";

    the code worked flawlessly.
    I hope this helps somewhat.

    Thanks
    Dipul

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://54309]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 12:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found