Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

How can I access/change the first N letters of a string?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:20 UTC ( #598=perlfaq nodetype: print w/replies, xml ) Need Help??

Current Perl documentation can be found at perldoc.perl.org.

Here is our local, out-dated (pre-5.6) version:

There are many ways. If you just want to grab a copy, use substr():

    $first_byte = substr($a, 0, 1);

If you want to modify part of a string, the simplest way is often to use substr() as an lvalue:

    substr($a, 0, 3) = "Tom";

Although those with a pattern matching kind of thought process will likely prefer:

    $a =~ s/^.../Tom/;

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
[shmem]: oh the whitespace in the regex got condensed, meh
[shmem]: should be /^ {7}(\w+)/ or such
[shmem]: ...at least for my perldoc on Linux debian 8
[Discipulus]: mmh.. at themomemt i just an old 5.8 and outputs just GLOBAL::
[Discipulus]: anyway thanks! i've posted my font size suggestion anyway

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2017-05-23 10:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?