Solved

# split

Posted on 2000-04-03
Medium Priority
161 Views
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
Question by:mmcw
• 2
• 2

LVL 16

Expert Comment

ID: 2681122
try this

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

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

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

LVL 25

Expert Comment

ID: 2681195
or

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

and for the second example:

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

Author Comment

ID: 2681212
clockwatcher:

0

LVL 25

Accepted Solution

clockwatcher earned 200 total points
ID: 2681227
\$_ = "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 Comment

ID: 2681250
Thank you.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
###### Suggested Courses
Course of the Month16 days, 15 hours left to enroll