dusk wrote:
if ($input eq $matching[0..2]) {die "bastard\n"};
$code_or_die suggested:
if ($input eq $matching[0||1||2]) {die "bastard\n"};
merlyn wrote:
which means exactly the same as:
if ($input eq $matching[1]) {die "bastard\n"};
Were you expecting it to mean something different, especially since you said it "works"? In what manner did you mean "works"?

Coleoid said:
Schwartz, do you sometimes give the less-godly a break, or do you always present them with an insult which gives them no more useful knowledge than they began with? Where do you get your kicks from being an expert? Power-trip or education?

In this case, even though $code_or_die might hope the [0||1||2] would iterate over the values in the array, Perl uses short-circuiting logic to return a value, with the first true value in the set (in this case, the 1) being used for the subscript to the array. So I'm guessing you tested it with the middle ("duck") value, saw it work, and posted.

Lesson 1: Test more thoroughly--Success by coincidence sucks.

Lesson 2: Read up on short-circuiting logic--it's a sharp knife that can simplify problems for you...or slip in your hand and cost you a thumb.

But none of this bears on why dusk's original code didn't work:

if ($input eq $matching[0..2]) {die "bastard\n"};
In this case, the '0..2' resolved to three values, which means the 'matching[0..2]' subexpression is a 'slice'. The '$' in front of that all means that subexpression has to resolve to one scalar value--in this case, the first one in the list.

But after all this, dusk, I don't know why you get that paticular error message--I don't, in 'perl -de 0'...and I can't concoct in my head why that would happen. This is something I'd love to have Randall answer--I don't get it.


In reply to Re: Re: Re: Why does this happen? by Coleoid
in thread Why does this happen? by dusk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.