• Status: Solved
• Priority: Medium
• Security: Public
• Views: 163

# split

How to split the following:

"Quantity_381_04032000101718.x"

to

\$a = "381";
and
\$b = "04032000101718";

on a nice way???

or split it to:

\$a = "381_04032000101718";
0
mmcw
• 2
• 2
1 Solution

Commented:
try this

#!/usr/local/bin/perl
\$var="Quantity_381_04032000101718.x";

(\$junk,\$a,\$b)=split(/_/,\$var);
\$b=~ s/\..*//g;

print \$a," => ",\$b,"\n";
0

Commented:
or

(\$a, \$b) = /(\d+)_(\d+)/;

and for the second example:

(\$a) = /_([\d_]+)/;
0

Author Commented:
clockwatcher:

0

Commented:
\$_ = "Quantity_381_04032000101718.x";
(\$a, \$b) = /(\d+)_(\d+)/;
print "\$a\n\$b\n";

(\$a) = /_([\d_]+)/;
print "\$a\n";

--------------------
Output:
381
04032000101718
381_04032000101718

Which is what you said you wanted.

>> or split it to:
>>  \$a = "381_04032000101718";

0

Author Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.