Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^4: Perl delete function

by annel (Novice)
on Dec 23, 2013 at 07:03 UTC ( #1068148=note: print w/replies, xml ) Need Help??

in reply to Re^3: Perl delete function
in thread Perl delete function

Yes, I would like to compare the arrays one by one by position. I have attempted following:
use strict; use warnings; open (FILE, "a") or die "Unable to open ref file.\n"; my @data1 =<FILE> ; close(FILE); open (FILE, "b") or die "Unable to open new file.\n"; my @data2 =<FILE> ; close(FILE); my $i; for( $i = 0; $i < 2; $i++){ if ($data1[$i] ne $data2[$i]){ print "Mismatch for $data1[$i] and $data2[$i]\n"; exit; } }
But the error occurs when file 1 contains one line and file 2 contains 2 lines.

Replies are listed 'Best First'.
Re^5: Perl delete function
by hdb (Monsignor) on Dec 23, 2013 at 07:19 UTC

    You need to determine the size of the smaller array and restrict the loop accordingly.

    my $size = @data1 < @data2 ? @data1 : @data2;
      Thanks for the tips. It works.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1068148]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2023-03-31 21:47 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (76 votes). Check out past polls.