Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: How do I cut single characters out of a string

by Not_a_Number (Prior)
on Jan 13, 2012 at 20:36 UTC ( #947825=note: print w/replies, xml ) Need Help??

in reply to How do I cut single characters out of a string

Ok, you've had three replies* (at the time of writing), all showing you how to remove '1' from a string.

Now, I might be wrong here (in which case read no further), but your use of a variable $getchar suggests that it might not be a known character that you want to remove from the string (and apparently save to a variable), but a character at a given position in the string.

However, you say:

now I need to only cut number 1 from the middle

Which rather militates against my analysis:

1. Your example string is 14 characters long, and therefore has no single middle character.

2. The middle two characters are 'ng', but you say you want 'number 1 from the middle'.

Whatever your real requirement, the following code removes and saves the middle character of a string if its length is odd, or the first of the two middle characters if its length is even:

my $string = 'mystring123456'; # or whatever my $getchar = substr $string, length( $string ) / 2, 1, ''; print "Character is $getchar and string is now $string\n";

* One canonical, one rather needlessly complicated, and one that removes all '1's willy nilly.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://947825]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2018-06-21 11:29 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (118 votes). Check out past polls.