• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Split a string?

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
4099aol
Asked:
4099aol
  • 4
  • 4
  • 4
  • +2
1 Solution
 
ozoCommented:
($First4,$Next4,$Last4) = ($PromoNum =~ /(\d+\D+\d+\D+\d+\D+\d+)\D+(\d+(?:\D+\d+){3}).*?(\d+(?:\D+\d+){3})\D*$/);

0
 
burnotteCommented:
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
 
ozoCommented:
Did you mean Place the first 4 digits in $First4 ?
What is a "number" and what is in $PromoNum besides "numbers"?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
b2piCommented:
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
 
4099aolAuthor Commented:
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
 
4099aolAuthor Commented:
ozo, I was unable to get your codes to work.  Please check them out.  

thanks
0
 
b2piCommented:
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
 
bertvermeerbergenCommented:
If the length of the string is fixed (3x4digits, how about:
  ($l, $m, $r) = unpack "a4 a4 a4", $PromoNum;
0
 
4099aolAuthor Commented:
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
 
b2piCommented:
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
 
b2piCommented:
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
 
ozoCommented:
#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
 
4099aolAuthor Commented:
Thanks ozo, your codes are now working.  Please answer the question so that I could give you the points...
0
 
ozoCommented:
# 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now