Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by stevieb (Canon)
on Jun 29, 2015 at 13:48 UTC ( [id://1132456]=note: print w/replies, xml ) Need Help??


in reply to find a substring of unknow lenght if it is in a hash

What you've done in your code is create an array with each element only one character in length, so when you use ~~ smartmatch against the array, you're only testing against a substring of a single character.

If I understand what you're attempting to do, this code may help get you on your way:

#!/usr/bin/perl use warnings; use strict; my $str = 'abcdefghijkl'; my %match = (ab => 0, abcd => 0, xyz => 0,); for my $key (keys %match){ if ($str =~ /$key/){ $match{$key}++; } else { print "$key not found in $str\n"; } } for my $k (keys %match){ print "key: $k value: $match{$k}\n"; }

-stevieb

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-24 20:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found