Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: find words around a word in a file.

by happy.barney (Friar)
on Jul 28, 2011 at 09:35 UTC ( [id://917226]=note: print w/replies, xml ) Need Help??


in reply to find words around a word in a file.

next time please use <code> tag and also add expected result.
following code will do something similar to your spec :-)
my %hash; while (<>) { $hash{$1} = $2 while m/(\w+)\W+going\W+(\w+)/g; } __END__ %hash = ( 'am' => 'home', 'are' => 'school', 'is' => 'to', );

Replies are listed 'Best First'.
Re^2: find words around a word in a file.
by The Perlman (Scribe) on Jul 29, 2011 at 17:13 UTC
    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://917226]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2025-07-14 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.