http://www.perlmonks.org?node_id=1019549

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