in reply to Re: need help: substitute n-th word in a line
in thread need help: substitute n-th word in a line

Just having fun with your code, however, I think this would be cool:

use warnings; use strict; my $N = -3; my $new = "NEW"; while (<DATA>) { local $[ = 1; my @words = split /\s+/; $words[$N] = $new if @words >= abs($N); print join " ", @words, "\n"; } __DATA__ hi there everyone I am looking to substitute lots of words for new ones, but only the third word per line on each line! one one two one two three one two three four __END__ outputs: hi there everyone I am NEW to substitute lots of words for new ones, but NEW the third word per line NEW each line! one one two NEW two three one NEW three four

Yay! Now we can specify from either end :)

Anonymously yours,
Anonymous Monk