in reply to Re: Matching Text
in thread Matching Text

my %dict = map {split /\s/,$_,2} <$d>; close $d; while (defined (my $line=<$w>)){ for (split /\s/,$line){ if (my $meaning = $dict{$_}){ print "$_: $meaning\n"; next; } print "$_ is not in the dictionary\n"; } }

I would write that as:

my %dict = map split( ' ', $_, 2 ), <$d>; close $d; while ( <$w> ) { for my $word ( split ) { if ( exists $dict{ $word } ) { print "$word: $dict{ $word }"; next; } print "$word is not in the dictionary\n"; } }

The use of /\s/ with split will screw up the dictionary if there are any leading whitespace.    (Anything that can go wrong, will go wrong. -- Murphy)

Replies are listed 'Best First'.
Re^3: Matching Text
by NetWallah (Canon) on Apr 03, 2012 at 05:23 UTC
    agreed. (++). my split-fu is not the greatest.

                 All great truths begin as blasphemies.
                       ― George Bernard Shaw, writer, Nobel laureate (1856-1950)

Re^3: Matching Text
by Chad C (Initiate) on Apr 03, 2012 at 16:22 UTC

    Hey guys thanks this seems to be working real well with printing to the screen. I just need to figure out how to get it to output to an external file