in reply to Can you set a character in a string at a given index directly?

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

  • Comment on Re: Can you set a character in a string at a given index directly?
  • Download Code

Replies are listed 'Best First'.
Re^2: Can you set a character in a string at a given index directly?
by tkguifan (Scribe) on Feb 03, 2015 at 16:08 UTC
    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