Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by Not_a_Number (Parson)
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.


Comment on Re: How do I cut single characters out of a string
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (8)
As of 2015-07-07 08:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (87 votes), past polls