Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

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 ( [id://1115398]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1115398]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2025-12-13 14:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (93 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.