in reply to Re^3: find a substring of unknow lenght if it is in a hash
in thread find a substring of unknow lenght if it is in a hash
index my $tring,$art
A variable declaration like my returns a value:
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.c:\@Work\Perl\monks>perl -wMstrict -le "my $x = my $y = my $z = 42; print $x; " 42
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: <%-(-(-(-<
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: find a substring of unknow lenght if it is in a hash
by perlynewby (Scribe) on Jun 30, 2015 at 02:12 UTC | |
by AnomalousMonk (Archbishop) on Jun 30, 2015 at 06:58 UTC |