Solved

split

Posted on 2000-04-03
5
154 Views
Last Modified: 2010-03-05
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
Comment
Question by:mmcw
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:maneshr
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

by:clockwatcher
ID: 2681195
or

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

and for the second example:

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

Author Comment

by:mmcw
ID: 2681212
clockwatcher:

Your second answer does not work!!!
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 50 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

by:mmcw
ID: 2681250
Thank you.
I made a mistype
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question