First, make sure you realize that lists and arrays are different things; they return different things in scalar context. A list returns its last element, while an array returns its size.
#!/usr/bin/perl -l
## list:
print scalar (4,5,6,7); ## 7
## array:
my @foo = (4,5,6,7);
print scalar @foo; ## 4
Next, understand that an array (or hash) slice is simply a list (see
here for one discussion). In other words, writing
@foo[1,2] is like writing
($foo[1], $foo[2]) explicitly. Yes, it's a bit confusing because the @ sigil is reminiscent of an array...
Now you can see why the array slices didn't compare with == like you thought. The == operator imposes scalar context, which causes the slices to return their last element and the arrays to return their size. If it helps, scatter some print scalar @foo[0,1] statements around to see what numbers really are being compared with == in those conditionals.
-
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.