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.