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

sub set_piece_at_pos { my $self=shift; my $pos=shift; my $piece=shift; my @rep=split //,$self->{rep}; $rep[$pos]=$piece; $self->{rep}=join('',@rep); }

Replies are listed 'Best First'.
Re: Can you set a character in a string at a given index directly?
by blindluke (Hermit) on Feb 03, 2015 at 15:52 UTC

    Yes, you can. Read the documentation for the substr function.

    UPDATE: a simple example

    #!/usr/bin/perl use v5.14; my $string = '0123456789'; my $index = 1; # second character my $replacement = 'x'; substr( $string, $index, # starting from 0 1, # how many characters $replacement, ); say $string; # will print out '0x23456789' as expected

    - Luke

      This one I like. I mean the example. I could not figure this out that quickly even reading the documentation. I have always thougt that substr returns something. I never thought of it as something that can set something :).

        The return value of substr is an lvalue (assuming the original string is an lvalue), which is a fancy way of saying that it can be assigned to. So, the chunk of the data returned by substr can be assigned to. The four argument version of this call is (basically) equivalent.

        substr( $string, $index, # starting from 0 1, # how many characters ) = $replacement;

        --MidLifeXis

Re: Can you set a character in a string at a given index directly?
by Corion (Patriarch) on Feb 03, 2015 at 15:53 UTC

    See substr. Also, maybe review your course materials that likely cover that function too.

Re: Can you set a character in a string at a given index directly?
by Athanasius (Archbishop) on Feb 03, 2015 at 16:26 UTC

    Hello tkguifan,

    For your particular problem, substr is the best solution, as others have shown. However, you might find it useful to see how your original approach can be made to work. The missing piece of the puzzle is splice:

    #! perl use strict; use warnings; while (<DATA>) { my @f = split /\s+/; my $s = insert_string(@f); printf "%9s: %s\n", $f[0], $s; } sub insert_string { my ($string, $pos, $insert) = @_; my @chars = split //, $string; splice @chars, $pos, 0, $insert; return join '', @chars; } __DATA__ advark 1 a aaacccddd 3 bbb xyz 3 w qrst 0 p

    Output:

    2:23 >perl 1144_SoPW.pl advark: aadvark aaacccddd: aaabbbcccddd xyz: xyzw qrst: pqrst 2:23 >

    Hope that helps,

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

      How come the LENGTH parameter is 0? The doc says "removes the elements designated by OFFSET and LENGTH from an array". I'm removing 1 element.

        Oh, OK, I thought you were inserting a string at the given position. To replace N characters, of course you set the LENGTH parameter to N. For example, to remove 1 element, set LENGTH to 1.

        And (just for the sake of completeness), if you want to overwrite the string, you can do this:

        splice @chars, $pos, length $insert, $insert;

        which will, for example, take inputs of aaacccddd 3 bbb and thequickbrownfox 8 white, and produce outputs of aaabbbddd and thequickwhitefox, respectively.

        Hope that helps,

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