Problem with split function

D

David Morel

Hi all,

I am having a problem with the split function.
Here is my code:

$_ = "123 a 456 b 789 c";
my @words = split /[^a-z]+/, $_;
print $words[0], $words[1], $words[2];

I want the output to be: abc
However, the actual output of this code is: ab
($words[0] is equal to "" not "a").

What am I doing wrong? Why isn't the output abc?
 
T

Ted Zlatanov

I am having a problem with the split function.
Here is my code:

$_ = "123 a 456 b 789 c";
my @words = split /[^a-z]+/, $_;
print $words[0], $words[1], $words[2];

I want the output to be: abc
However, the actual output of this code is: ab
($words[0] is equal to "" not "a").

What am I doing wrong? Why isn't the output abc?

See "perldoc -f split" - you have an empty leading field which went
into $words[0], and 'c' is in $words[3].

Ted
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top