Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: Exact string matching

by Anonymous Monk
on Oct 16, 2011 at 11:08 UTC ( [id://931756]=note: print w/replies, xml ) Need Help??


in reply to Re: Exact string matching
in thread Exact string matching

Dear Monk, I'm so sorry for being very immature, this is my first time posting a question, so plz forgive my immaturity

open(HD,"file") or die ("Cant open"); $text=<HD>; $text=~s/ //g; chomp $text; $pattern="word"; $offset = 0; $pos=index $text,$pattern,$offset; while ($pos != -1) { print "Found $pattern at $pos\n"; $offset = $pos + 1; $pos = index($text, $pattern, $offset); }

Replies are listed 'Best First'.
Re^3: Exact string matching
by ramprasad27 (Sexton) on Oct 16, 2011 at 11:41 UTC
    Looking at what you are trying to achieve, here is the code
    use Data::Dumper; open (HAN,'employee.pm'); my $cont = <HAN>; # assume $cont = 'package Employee df df'; my %hash = (); while ( $cont =~ m/(\w+)/g ) { $hash{$1}++; } print Dumper(\%hash); --------- output $VAR1 = { 'Employee' => 1, 'df' => 2, 'package' => 1 };
    it prints how many time each word occured ..

      Dear Ram, Thank you very much for your kind assistance but this works only if the words in the file is separated with a defined spacer such as a white space, what if the file contains only strings without any spacer (junk of characters or sequence of characters to be precise). That's where I am stuck. I need to find the number of occurrence of all possible substrings, that to in linear time (sry, that I was not clear). example: $text = 'howdoidoit' and the answer should be like, for substring of length 3 => how = 1 ; owd = 1 ; wdo = 1 ; doi = 2 ; oid = 1 ; ido = 1 ; oit = 1 ;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-20 10:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found