Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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

by Hellena (Initiate)
on Feb 19, 2013 at 13:06 UTC ( #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; }

Comment on How to add each line into single line in txt file PERL?
Select or Download Code
Re: How to add each line into single line in txt file PERL?
by Ratazong (Prior) 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
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? | Other CB clients
Other Users?
Others about the Monastery: (10)
As of 2014-08-22 18:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (163 votes), past polls