hello_its_me
hello_its_me

Reputation: 793

String Skipping characters in Perl

I have this string:

my $string = "Stuff1, stuff2, stuff3, stuff4";

I want to save the substring "stuff3" in another string. For an example, to save stuff1 I use the following:

$substring = substr($string, 0, index($string, ','));

Basically, this takes all the characters until there is a ',' char and that way, I have stuff1 saved in a different string. How can I save string 3?

Thank you,

Upvotes: 0

Views: 62

Answers (1)

Schwern
Schwern

Reputation: 164739

You could creep through it with index, but you're much better off splitting the whole thing into an array.

my @words = split /\s*,\s*/, "Stuff1, stuff2, stuff3, stuff4";
print $words[2];

Upvotes: 2

Related Questions