Solved

Split a string?

Posted on 1998-09-23
14
189 Views
Last Modified: 2010-03-05
I have a string called $PromoNum and I want to spilt the value into 3 strings.

Place the first 4 numbers in $First4
Place the next 4 numbers in $Next4
and Place the last 4 numbers in $Last4

Is this possible?  Also I am using a UNIX system.

thakns
0
Comment
Question by:4099aol
  • 4
  • 4
  • 4
  • +2
14 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1204983
($First4,$Next4,$Last4) = ($PromoNum =~ /(\d+\D+\d+\D+\d+\D+\d+)\D+(\d+(?:\D+\d+){3}).*?(\d+(?:\D+\d+){3})\D*$/);

0
 
LVL 1

Expert Comment

by:burnotte
ID: 1204984
one way to this is ( there is certainly more efficient way to do this ) :
#!/usr/bin/perl
$PromoNum="1234567895678";
@tmp= split // , $PromoNum;
$first4=$tmp[0].$tmp[1].$tmp[2].$tmp[3];
$next4=$tmp[4].$tmp[5].$tmp[6].$tmp[7];
$last4=$tmp[8].$tmp[9].$tmp[10].$tmp[11];
print " $first4 $next4 $last4 \n";

0
 
LVL 84

Expert Comment

by:ozo
ID: 1204985
Did you mean Place the first 4 digits in $First4 ?
What is a "number" and what is in $PromoNum besides "numbers"?
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204986
burnotte wrote:
 > one way to this is ( there is certainly more efficient way to do
 this ) :


 Well, yes, that is one way to do it.  However, it's an exceedingly
 bad way, and not nearly as flexible as ozo's suggestion, which he was
 patiently waiting for acceptance of before submitting an actual
 answer.

 Perhaps burnotte can explain why he felt justified in proposing such
 an awful answer, on top of ozo's code?

 4099aol, please reject this answer.
0
 

Author Comment

by:4099aol
ID: 1204987
Sorry but ozo did give me the codes..

ozo.  I am sorry I should have said digits.  The digits that will be in $PromoNum are numbers (0-9).

thanks.
0
 

Author Comment

by:4099aol
ID: 1204988
ozo, I was unable to get your codes to work.  Please check them out.  

thanks
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204989
Amazingly enough, I see the problem in Ozo's code: The first
parenthetic term [ (\d+\D+\d+\D+\d+\D+\d+) ] will fail if the four
digits are abutting. (I think) Furthermore, that may grab more than
four digits if abutting.

A slightly different way (maybe more maintainable by a non-perl guru,
or, maybe not, but almost certainly _less_ efficient [sigh, I always
take the easy way out]):

($t = $PromoNum) =~ s/\D//g;
$t =~ /(.{4})(.{4}).*(.{4})/;
$First4 = $1;
$Next4 = $2;
$Last4 = $3;


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:bertvermeerbergen
ID: 1204990
If the length of the string is fixed (3x4digits, how about:
  ($l, $m, $r) = unpack "a4 a4 a4", $PromoNum;
0
 

Author Comment

by:4099aol
ID: 1204991
I was able to get burnotte's codes to work.  Sorry for rejecting you answer, if you want to reanswer it I will give you the 200 points.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204992
There's a reason I flamed burnotte's code (Other than a lack of coffee
and cigarettes).  If that's a user entered field, and the user happens
to enter a space somewhere, you'll fail.  It's also the _most_
inefficient way to do this.
0
 
LVL 5

Expert Comment

by:b2pi
ID: 1204993
There's a reason I flamed burnotte's code (Other than a lack of coffee
and cigarettes).  If that's a user entered field, and the user happens
to enter a space somewhere, you'll fail.  It's also the _most_
inefficient way to do this.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1204994
#if you want just 4 Digits, (with possible non-digits in between them)
($First4,$Next4,$Last4) = ($PromoNum =~/(\d\D*\d\D*\d\D*\d)\D*(\d(?:\D*\d){3}).*?(\d(?:\D*\d){3})\D*$/);
#If you just want 4 characters of any kind, burnotte's answer will work, although it may be easier to just say
$First4 = substr($PromoNum,0,4);
$Next4 = substr($PromoNum,4,4);
$Last4 = substr($PromoNum,-4);
0
 

Author Comment

by:4099aol
ID: 1204995
Thanks ozo, your codes are now working.  Please answer the question so that I could give you the points...
0
 
LVL 84

Accepted Solution

by:
ozo earned 200 total points
ID: 1204996
# if you want to strip any non digits, and if there are exactly 12 digits in $PromoNum, you could
($t = $PromoNum) =~ tr/0-9//cd;
($First4,$Next4,$Last4) = unpack "a4 a4 a4", $t;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now