in reply to need help: substitute n-th word in a line
use warnings; use strict; my $N = 3; my $new = "NEW"; while (<DATA>) { my @words = split /\s+/; $words[$N-1] = $new if scalar @words >= $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
output:
hi there NEW I am looking to substitute lots of NEW for new ones, but only the third word per NEW on each line! one one two one two NEW one two NEW four
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: need help: substitute n-th word in a line
by Anonymous Monk on Sep 16, 2003 at 17:01 UTC |