Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^4: find a substring of unknow lenght if it is in a hash

by AnomalousMonk (Archbishop)
on Jun 29, 2015 at 23:15 UTC ( [id://1132541]=note: print w/replies, xml ) Need Help??


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:

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:  <%-(-(-(-<

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

    hmm, well, I see what it printed but I'm not sure if I defined the empty-string key in the hash as you have asked me, did I do it correctly?.

    If I add a space in between the tick marks then the hash returns

    this is INDEX =>bernini

    If no space between the ticks then match hash returns nothing.

    can you explained why even when the empty string(space in it) ' ' returns the key when there's not space in the $string that's being checked against??

    ok, I am have to read more on the index.

    I appreciate the well detailed explanations

    thank you

    my $string = 'abcdefghijk'; my %match = (' '=>"bernini",bc => "Michelangelo", efg => "Raffaello",d +e => "Caravaggio",ijk => "Tintoretto",hij=>"Leonardo",);
    foreach $art (keys %match){ print "this is INDEX $art =>",join(',',$match{$art}),"\n", if inde +x $string,$art; }
      ... I'm not sure if I defined the empty-string key in the hash as you have asked me, did I do it correctly?

      One way to see for yourself what a hash or array or arbitrarily complex structure of hashes and/or arrays contains is with the utility Data::Dumper, which IIRC is core and so should be supplied as part of any Perl installation, or with a similar utility such as Data::Dump, which I like. There are many other such utilities.

      Do consider the documentation for index. Also consider the true/false values of the values index can return: -1 if a substring is not found, an offset of 0 or more if a substring is found; some simple experiments may help here.

      Also perhaps consider if it's ever possible not to find the empty string in any other string, even in the empty string itself. Again, experimentation may be helpful.

      I will have more time tomorrow to respond to the questions in your reply if others have not already done so. (Update: Actually, I could not say much more nor better than kcott does in his reply below.)


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1132541]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found