Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Somebody asked me a question about comparing arrays with == and comparing array slices with ==. It's not as straightforward as you might think. So what is an array slice anyway?
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @x = (2,3); my @y = (7,8); my @z = (9,8,7,6,5,4,3,2,1); # probably everybody would agree that @x == @y, because they have # the same number of elements if (@x == @y) { print "x == y\n"; } if (@x == @z) { print "x == z\n"; } if (@y == @z) { print "y == z\n"; } # But how about slices? They have the same number of elements, # right? if (@x[0,1] == @y[0,1]) { print "x slice == y slice\n"; } if (@x[0,1] == @z[0,1]) { print "x slice == z slice\n"; } if (@y[0,1] == @z[0,1]) { print "y slice == z slice\n"; } print "x array is: ", Dumper(@x), "\n"; print "y array is: ", Dumper(@y), "\n"; print "z array is: ", Dumper(@z), "\n"; print "x slice is: ", Dumper(@x[0,1]), "\n"; print "y slice is: ", Dumper(@y[0,1]), "\n"; print "z slice is: ", Dumper(@z[0,1]), "\n"; print "scalar x slice is: ", scalar(@x[0,1]), "\n"; print "scalar y slice is: ", scalar(@y[0,1]), "\n"; print "scalar z slice is: ", scalar(@z[0,1]), "\n"; # try assigning my @q = @x[0,1]; my @r = @y[0,1]; if (@q == @r) { print "q == r\n"; } # okay how about this? if (@q == @x[0,1]) { print "q == x slice\n"; } __END__

In reply to 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 pondering the Monastery: (4)
As of 2024-04-25 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found