in reply to Re: search and replace
in thread search and replace

the main purpose is to find all words that are seperated by space from my INFILE and replace them by their index. in your method its could work if it is one word per line.

Replies are listed 'Best First'.
Re^3: search and replace
by almut (Canon) on Apr 03, 2009 at 14:21 UTC
    ...all words that are seperated by space...

    Ah... something you didn't mention in your OP.

    In this case, you could do:

    while (my $line = <INFILE>) { $line =~ s/(\S+)/$dict{$1} || $1/eg; print $line; }

    In case zero is a valid index, too, you'd need

    $line =~ s/(\S+)/defined $dict{$1} ? $dict{$1} : $1/eg;

    Or, if you're using Perl 5.10, you could write instead

    $line =~ s|(\S+)|$dict{$1} // $1|eg;