index my $tring,$art

A variable declaration like my returns a value:

c:\@Work\Perl\monks>perl -wMstrict -le "my $x = my $y = my $z = 42; print $x; " 42
In the case of your index expression, the  my $tring sub-expression defines a scalar variable and default-initializes it with the (very well defined) undefined value undef. This value is passed to the index built-in and "promoted" to  '' (the empty string), but because you have wisely asked Perl to do so, Perl warns you about this rather suspicious transaction (update: and also the business with my). index then searches for an occurrence of the string in  $art within the empty string, and returns its success/failure. The string in  $art is never found, so index always returns -1, which is a true if-condition, and so everything gets printed.

Update: Here's an experiment: Is it possible that  '' (the empty string) is a valid hash key? If so, if you add a new artist's name to your hash as the value of the key empty-string, will this artist's name be printed in the for-loop? In other words, can index identify the empty-string of the key within the empty-string of the promoted undef? If it can, what value will index return?


Give a man a fish:  <%-(-(-(-<


In reply to Re^4: find a substring of unknow lenght if it is in a hash by AnomalousMonk
in thread find a substring of unknow lenght if it is in a hash by perlynewby

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.