Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi i am a beginner in perl.I have the following code to replace the 2nd comma in a line with "|".

$line="1,2,3"; my $ft_count = 0; $ft_count++ while($line =~ m/[,]/g); if($ft_count==2) { my $n = 2; my $r = $n - 1; $line =~ s/((?:,.*?){$r}),/${1}|/; } print $line;

Output will be 1,2|3 I need to reuse the code in such a way that the input is "1|2|3" and i need to replace the 2nd "|" with comma.I tried changing the regular expression in the above code.But it didnt work.Can anyone plz help

??

Replies are listed 'Best First'.
Re: Replacing with comma
by Athanasius (Archbishop) on Dec 27, 2012 at 06:51 UTC

    The {$r} in the regex is acting as a quantifier, not an index. It would be better to use split, splice, and join, like this:

    #! perl use Modern::Perl; my $separator = '|'; my $replacement = ','; my $target_count = 2; my $line = '1|2|3'; my $ft_count = 0; ++$ft_count while $line =~ /\Q$separator\E/g; if ($ft_count == $target_count) { my @data = split /\Q$separator\E/, $line; splice(@data, $target_count - 1, 2, "$data[$target_count - 1]$repl +acement$data[$target_count]"); $line = join($separator, @data); } print $line, "\n";

    Output:

    16:38 >perl 453_SoPW.pl 1|2,3 16:46 >

    See perlre for \Q...\E.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Replacing with comma
by muba (Priest) on Dec 27, 2012 at 06:57 UTC
    use strict; use warnings; use 5.010; my $foo = "1,2,3"; substr($foo, rindex($foo,","), 1, "|"); say $foo; # 1,2|3 $foo = "1|2|3"; substr($foo, rindex($foo,"|"), 1, ","); say $foo; # 1|2,3
Re: Replacing with comma
by 2teez (Vicar) on Dec 27, 2012 at 07:37 UTC

    You have been given great answers, but If I may add just this.
    Using a subroutine with scalar, reverse functions with substitution s///. One can hack a solution like so:

    use warnings; use strict; my $foo = "1,2,3"; print modify( $foo, ',', '|' ); ## prints 1,2|3 $foo = "1|2|3"; print modify( $foo, '|', ',' ); ## prints 1|2,3 sub modify { my ( $val, $sep, $rep ) = @_; $val = scalar reverse $val; $val =~ s/\Q$sep\E/$rep/; return scalar reverse $val; }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Replacing with comma
by 7stud (Deacon) on Dec 27, 2012 at 10:33 UTC

    I need to reuse the code

    First, you need to learn proper formatting. Use this code as a guide:

    my $line = "1,2,3"; my $ft_count = 0; $ft_count++ while($line =~ m/[,]/g); if ($ft_count == 2) { my $n = 2; my $r = $n - 1; $line =~ s/((?:,.*?){}),/${1}|/; } say $line;

    I need to reuse the code

    1) Replace all occurrences of | in the code with a comma.

    2) Replace all occurrences of a comma in the code with a |. However, the pipe has a special meaning in a regex, so if you literally want to match a pipe in a regex, then you have to escape it with a backslash. Or, you can put the pipe in a character class:

    [|]

    ... which will nullify its special meaning. The character class notation is easier to figure out than the hieroglyphics "\|", so I suggest you do that.

    3) The substitution operator, s///, uses the syntax:

    s/regex/string/

    That means if you want to match a pipe character in the regex part of the s///, you must escape it or use it in a character class.

Re: Replacing with comma
by Anonymous Monk on Dec 27, 2012 at 12:19 UTC
    See also: Replace the nth occurence - 1004836