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

Hiii

is there any way to make perl read 2 lines at a time and compare fields with in them

Replies are listed 'Best First'.
Re: reading 2 lines at a time using perl
by toolic (Bishop) on Jun 26, 2012 at 16:36 UTC
    Here is one way:
    use warnings; use strict; while (<>) { my $line1 = $_; my $line2 = <>; # compare 1 and 2 }
Re: reading 2 lines at a time using perl
by cavac (Prior) on Jun 26, 2012 at 16:42 UTC

    Something like this, perhaps?

    #!/usr/bin/env perl use strict; use warnings; my ($first, $second); while(($first = <DATA>) && ($second = <DATA>)) { print "1: $first", "2: $second\n"; if($first eq $second) { print STDERR "Duplicate line $first"; } } __DATA__ Hello World Hallo Welt Servas Woit foo foo
    "You have reached the Monastery. All our helpdesk monks are busy at the moment. Please press "1" to instantly donate 10 currency units for a good cause or press "2" to hang up. Or you can dial "12" to get connected directly to second level support."
Re: reading 2 lines at a time using perl
by tobyink (Canon) on Jun 26, 2012 at 20:01 UTC

    Assuming your filehandle is called $fh, then this is quite nice:

    use strict; use Data::Dumper; my $count = 0; while (my @both = grep length, ~~<$fh>, ~~<$fh>) { chomp @both; print Dumper \@both; }

    It might be even nicer if the grep length weren't necessary, but unfortunately it is.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: reading 2 lines at a time using perl
by johngg (Canon) on Jun 26, 2012 at 23:29 UTC

    Another way using eof, a scalar readline and a ternary in a map.

    knoppix@Microknoppix:~$ perl -Mstrict -Mwarnings -E ' > open my $fh, q{<}, \ <<EOD or die $!; > Line 1 > Line 2 > Line 3 > Line 4 > Line 5 > EOD > > while ( not eof $fh ) > { > my @two = map { eof $fh ? () : scalar <$fh> } 1 .. 2; > chomp @two; > say qq{@two}; > }' Line 1 Line 2 Line 3 Line 4 Line 5 knoppix@Microknoppix:~$

    Cheers,

    JohnGG