Gossamer Forum
Home : General : Perl Programming :

help with split please

Quote Reply
help with split please
Hi All

Apologies in advance if this is a dumb question.

I'm trying to separate out digits from an alphanumeric string and then load these into an array using the following.

E.g $string = 1test123

@stringdigits = split(/\D/, $string);

What I wanted to happen was that only the 1,2,3,4 were read and they were separately loaded into the array as:

$stringdigits[0], $stringdigits[1], $stringdigits[2], $stringdigits[3]

What seems to be happening is that the letters are being loaded into the array as empty elements and the numbers are not being separated as I expected. I know strings can be separated into individual characters using:

@indivchars = split(//, $string);

Do I need to somehow combine the two? What am I doing wrong?

Any help would be much appreciated.
Thanks
J

Unsure
Quote Reply
Re: [juletools] help with split please In reply to
one way

Code:
my $string = '1test123';
my @stringdigits = $string =~ /\d/g;

Last edited by:

Mark Badolato: Mar 6, 2003, 7:58 PM
Quote Reply
Re: [Mark Badolato] help with split please In reply to
Heh this question was asked by the same user at perlguru.com the other day and I gave the exact same answer. Not sure why it needed asking again.