Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Okay, here's what I'm going to give him. Hopefully it will make him think again about using == for this sort of thing. Hopefully it has enough examples and pointers to the documentation to show him what's up.
#!/usr/bin/perl use strict; use warnings; my @arr1 = (1, 2, 3, 4); my @arr2 = (4 .. 7); # from "perldoc perldata": # # If you evaluate an array in scalar context, it returns the length of # the array. (Note that this is not true of lists, which return the # last value, like the C comma operator) # note that an array slice is a list, not an array # see perldoc -q "difference between a list and an array" print "-" x 72, "\nbroken wrong way to do it:\n"; if (@arr1 == @arr2) { print "\@arr1 same length as \@arr2\n"; } if (@arr1[2,3] == @arr2[4,2,0]) { print "the two slice expressions each have the same number on the" +, " end of the list\n"; } if ((1,3,9) == (-10000,34,52,42,9)) { print "those two lists end with the same number\n"; } my @arr3 = @arr1[2,3]; my @arr4 = @arr2[4,2,0]; if (@arr3 != @arr4) { print "\@arr3 different length from \@arr4\n"; } print "-" x 72, "\na correct way to see if arrays are equal:\n"; print "two arrays\n"; if (! arr_comp(\@arr1, \@arr2)) { print "\@arr1 is not equal to \@arr2\n"; } # need to turn slices into arrays to compare them # see "perldoc -f scalar" print "two slices\n"; if (! arr_comp(\@{[ @arr1[2,3] ]}, \@{[ @arr2[3,2,0] ]})) { print "the two slices are not equal\n"; } print "two more slices\n"; if (arr_comp(\@{[ @arr3[0,1] ]}, \@{[ @arr1[2,3] ]})) { print "these two slices are equal\n"; } # compares two arrays, returns true if they are the same, false # if they aren't # pass in references to the arrays, please sub arr_comp { my $arr1 = shift; my $arr2 = shift; print "comparing @$arr1 and @$arr2\n"; my $same = 0; if (@$arr1 != @$arr2) { # arrays have different length return $same; } $same = 1; for (my $i = 0; $i <= $#$arr1; $i++) { # using ne so it works for strings too if ($arr1->[$i] ne $arr2->[$i]) { $same = 0; last; } } return $same; } __END__

Thanks for your help.


In reply to Re^3: So what is an array slice anyway? by beable
in thread So what is an array slice anyway? by beable

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-19 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found