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

How to add each line into single line in txt file PERL?

by Hellena (Initiate)
on Feb 19, 2013 at 13:06 UTC ( [id://1019549]=perlquestion: print w/replies, xml ) Need Help??

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

0 down vote favorite I have problem with my code cose of this part:
foreach my $line(keys %results){ print Dump($post) if $results{$line} == 1; }
I need it to be only one time printed 'print Dump($post)' Here is all that part
my $f1 = "/home/hellena/Desktop/data.txt"; my $f2 = "/home/hellena/Desktop/ini.txt"; my %results = (); open FILE1, "$f1" or die "Could not open file: $! \n"; while(my $line = <FILE1>){$results{$line}=1; } close(FILE1); open FILE2, "$f2" or die "Could not open file: $! \n"; while(my $line =<FILE2>) { $results{$line}++; } close(FILE2); foreach my $line(keys %results){ print Dump($post) if $results{$line} == 1; }

Replies are listed 'Best First'.
Re: How to add each line into single line in txt file PERL?
by Ratazong (Monsignor) on Feb 19, 2013 at 13:41 UTC

    Hi Hellena,

    I'm a bit guessing what you want to achieve. I assume you have the problem that Dump($post) is printed more than once in case there is more than one unique line in both files. You can evade this by leaving the loop once you printed the first time. That could be done the following way, using last:

    foreach my $line(keys %results){ if ($results{$line} == 1) { print Dump($post) ; last; } }
    HTH, Rata

      Yes thats it, thaks

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2024-04-23 16:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found