in reply to Re^4: 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

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; }

Replies are listed 'Best First'.
Re^6: find a substring of unknow lenght if it is in a hash
by AnomalousMonk (Archbishop) on Jun 30, 2015 at 06:58 UTC
    ... 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:  <%-(-(-(-<