perl197 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I have a file that I've overtly replaced certain text with a | to assist in loading the file into a table. As the last field could contain actual |'s as part of a command sequence, I want to replace any |'s after the 9th occurrence with the word pipe to load into a varchar field. It works for the 10th occurrence but somehow skips over the 11th and treats the 12th as the 11th. I.e, "|some command with the 10th | along with the 11th | along with the 12th |" results in "|some command with the 10th pipe along with the 11th | along with the 12th pipe " Is there something inherent with my approach that is unable to accurately determine the 11th occurrence of a |?...a humble novice.
open($fh, '<:encoding(UTF-8)', $outfilename) or die "Could not open fi +le '$outfilename' $!"; # open the file that had 9 |'s overtly set. open(OUT, ">$outfileload") or die "Unable to open $outfilename for wri +ting: $!\n"; # replace the 10th, 11th, and 12th |'s with pipe so the field can be +loaded into an expanded varchar field without treating the data as mo +re than one field. while ($row = <$fh>) { my $pos1 = 10; $row =~ s/(\|)/!--$pos1 ? ' pipe ' : $1/ge; my $pos2 = 11; $row =~ s/(\|)/!--$pos2 ? ' pipe ' : $1/ge; my $pos3 = 12; $row =~ s/(\|)/!--$pos3 ? ' pipe ' : $1/ge; chomp $row; print OUT "$row\n"; } print "doneagain\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: replace nth occurrence of |
by toolic (Bishop) on Sep 24, 2014 at 14:42 UTC | |
by perl197 (Novice) on Sep 24, 2014 at 17:29 UTC | |
|
Re: replace nth occurrence of |
by LanX (Saint) on Sep 24, 2014 at 14:31 UTC | |
|
Re: replace nth occurrence of |
by AnomalousMonk (Archbishop) on Sep 24, 2014 at 16:06 UTC | |
|
Re: replace nth occurrence of |
by GotToBTru (Prior) on Sep 24, 2014 at 14:45 UTC | |
by perl197 (Novice) on Sep 24, 2014 at 17:36 UTC | |
|
Re: replace nth occurrence of |
by clueless newbie (Curate) on Sep 24, 2014 at 19:05 UTC |