Re: How do I cut single characters out of a string
by toolic (Bishop) on Jan 13, 2012 at 19:17 UTC
|
use warnings;
use strict;
my $string = "mystring123456";
$string =~ s/1//;
print "$string\n";
__END__
mystring23456
| [reply] [d/l] |
Re: How do I cut single characters out of a string
by johngg (Canon) on Jan 13, 2012 at 19:21 UTC
|
You can use index to find the position of the "1" and the four-argument form of substr to replace it.
knoppix@Microknoppix:~$ perl -E '
> $str = q{mystring123456};
> substr $str, index( $str, q{1} ), 1, q{};
> say $str;'
mystring23456
knoppix@Microknoppix:~$
I hope this is helpful.
| [reply] [d/l] |
Re: How do I cut single characters out of a string
by Not_a_Number (Prior) on Jan 13, 2012 at 20:36 UTC
|
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. | [reply] [d/l] [select] |
Re: How do I cut single characters out of a string
by CountZero (Bishop) on Jan 13, 2012 at 20:59 UTC
|
use List::MoreUtils qw/before after/;
my $string = 'mystring123456';
$string = join '', (before {$_ eq 1} split //, $string), after {$_ eq
+1} split //, $string;
andmy $string = 'mystring123456';
$string = join '', split /1/, $string, 2;
This last one is particularly nice: it allows you to delete the first one, first two, first three, ... characters just by changing the last number. To delete the first n occurrences, use n + 1 as the last argument of split.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] [select] |
Re: How do I cut single characters out of a string
by pileofrogs (Priest) on Jan 13, 2012 at 22:07 UTC
|
Aaand just because everyone else is posting to this:
It all hinges on how you define what you are trying to remove. Is it the 1st instance of the number '1'? Is it the 1st digit in the string? Is it the 9th char from the beginning, the 6th from the end?
Some possible solutions:
$string =~ tr/1//d;
Will kill all the '1's w/out the regex engine.
$string =~ s/\d//;
Will kill the very first digit found in a string
$char = substr($string,-6,1,'');
Will remove the 6th from the last char. substr treats negative numbers as offset from the end of the string, so you don't need to do (strlen -6).
etc... etc...
| [reply] [d/l] [select] |
Re: How do I cut single characters out of a string
by CountZero (Bishop) on Jan 13, 2012 at 20:00 UTC
|
A most silly way to do it (but in the spirit of TIMTOWTDI): my $string = 'mystring123456';
$string = join '', grep {$_ ne 1} split //, $string;
And yes, this will cut all '1's out of the string.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
| [reply] [d/l] |
Re: How do I cut single characters out of a string
by tobyink (Canon) on Jan 13, 2012 at 22:10 UTC
|
I'm surprised that nobody has mentioned that substr returns an lvalue. In other words, you can assign to it.
my $string = 'mystring123456';
substr($string, 8, 1) = '';
print "$string\n";
| [reply] [d/l] [select] |
|
knoppix@Microknoppix:~$ perl -E '
> $str = q{abcdefghijk};
> say $str;
> substr $str, 5, 2, substr $str, 3, 2, substr $str, 5, 2;
> say $str;'
abcdefghijk
abcfgdehijk
knoppix@Microknoppix:~$ perl -E '
Of course, that particular example can be done more simply with a regex substitution but it can be a useful technique sometimes.
knoppix@Microknoppix:~$ perl -E '
> $str = q{abcdefghijk};
> say $str;
> $str =~ s{^...\K(..)(..)}{$2$1};
> say $str;'
abcdefghijk
abcfgdehijk
knoppix@Microknoppix:~$
I hope this is of interest.
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
Noone has mentioned that, but already 2 people before you used 4-arg substr, which in this case does the same thing.
| [reply] |
Re: How do I cut single characters out of a string
by akechnie (Initiate) on Jan 16, 2012 at 00:28 UTC
|
Thank you all for your assistance so far. I've been working with this and have a temporary solution.
To clarify my script takes in a different value every time ie.
$string = "mystring654321";
I should have been more clear that the input will always have six numeric characters at the end. I need to find what the first characters value is. In this case it will be a "6" but could change the next time.
My script then takes that value in and processes it further.
| [reply] |
|
if ($string =~ s/(\d)//) {
my $digit = $1;
...
} else {
print "No digit in '$string'\n";
}
which finds and removed the first digit from $string. Putting the substitution inside an if allows you to deal with not finding a digit.
True laziness is hard work
| [reply] [d/l] |
|
Ok I see what you did there
Now say I want the ability to grab any character out of the hole string given the position of it.
Such as mystring123456 so when I find the length of it with length(), so 14 characters. Is there a way I can tell it I want the 6th and 7th characters. Or then just grab the 9th character.
#not actual code more just a logical walk through
if(the string has 20 characters){
Get the 9th character;
Get the characters 6-8;
}elsif(the string has 14 characters){
Get the 6th character from the end;
Get the last 4 characters from the end;
}
Sorry if this looks ugly but sometimes it helps me think through things like this.
| [reply] |
|
|