Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: find words around a word in a file.

by The Perlman (Scribe)
on Jul 29, 2011 at 17:13 UTC ( [id://917497]=note: print w/replies, xml ) Need Help??


in reply to Re: find words around a word in a file.
in thread find words around a word in a file.

IMHO using hashes like this is a bad idea, because different $2 for the same $1 will be lost.

Furthermore looking for non-whitespace and non-punctuation could help practically solving the "what is a word problem".

my %hash; $whitespace=" \n\t"; $punctuation=".,!?"; $non_delimiters="[^$whitespace$punctuation]"; while (<DATA>) { push @{$hash{$1}}, $2 while m/($non_delimiters+)\s+going\s+($non_del +imiters+)/g; } use Data::Dumper; print Dumper \%hash; __DATA__ I am going home. I am going to bed. What's going on?
Output:
$VAR1 = { 'What\'s' => [ 'on' ], 'am' => [ 'home', 'to' ] };
I'm still not sure if a hash should be used at all, IMHO an array of pairs (two elemnet arrays) is better.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-18 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found