spliting strings by index

hi I have a string


in sudo code this is what i need to do

my $string = "1234567890123AB67890123-567890";

if $string =~ m/\d/ # starts with a Number
  if length($string) /10  = whole number # divide the length of the string by ten & test if it's an whole number
split $string ever 10 chariters with a space

how might i do this in perl

this is what i've got so far

my $string = "1234567890123AB67890123-567890";
if ($string =~ m/\d/ ) and length($string) >=10)
  {
    my $num = length($string) /10;
    
    if ($num != m/\./)
       {
         $string =~s/([a-zA-Z-])/$1 /g;
        }
  }

Open in new window



Thanx
LVL 1
trevor1940Asked:
Who is Participating?
 
ozoConnect With a Mentor Commented:
my $string = "1234567890123AB67890123-567890";
/^\d/ && (length)%10==0 && s/(.{10})(?=.)/$1 /sg for $string
0
 
trevor1940Author Commented:
ozo

added print"$string" after the two line of code and i get back

1234567890123AB67890123-567890

what i'd hope to get is

1234567890 123AB67890 123-567890
0
 
ozoCommented:
Are you sure you had
my $string = "1234567890123AB67890123-567890";
or was there an extra character?

If you had
my $string = "1234567890123AB67890123-567890\n";
then length($string) /10 would be 3.1, not a whole number,
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
trevor1940Author Commented:
$len = length($string);

print "[$len] $string\n";

=

[30] 1234567890123AB67890123-567890

so yes i'm sure
0
 
ozoCommented:
Exactly what code did you run?
0
 
tel2Commented:
On a side issue (i.e. English), Trevor:
  s/sudo code/pseudo code/

I think you might be getting mixed up with the UNIX/Linux "sudo" command, but a quick Google shows that you're not the only one on the planet who's made that mistake.

"Pseudo" means something like "not real".

http://en.wikipedia.org/wiki/Pseudo_code

Now you may proceed with the real issue, and I can sleep easy.
0
 
trevor1940Author Commented:
ozo
Finally got this working at home then bringing into work. Must have had a typo copying from IE6!

however i've now found examples where the wanted string dosn't always start with a number

Need thisBit0123AB78901234567890123-56789AndThis bit

should be

Need thisBit 0123AB789 0123456789 0123-56789 AndThis bit

so how might i addapt your code to grab

0\d[3][A-Z]\d[4]
or
0\d[3]-\d[5]
or
0\d[9]

Thanx

ps i'f required i'm happy to start a new question
0
 
ozoCommented:
How do you decide where the spaces should go in
Need thisBit0123AB78901234567890123-56789AndThis bit
Instead of 10 characters each, it looks like the lengths of the sections in your example are
12, 9, 10, 10, 11
0
 
ozoCommented:
Nor does
0123AB789
seem to match any of
0\d[3][A-Z]\d[4]
or
0\d[3]-\d[5]
or
0\d[9]
(I'm assuming you mean {3} where you say [3])
0
 
trevor1940Author Commented:
oops my bad

before & after the required pattern cauld be anything
10 chariters
always start with a 0 (zero) follwed by 3 numbers = 4

then either
all numbers                                      + 6                 = 10
or
two capital letters and the rest numbers  + 2 + 4  =10
or
-  (one dash) and the rest numbers            +1  + 5  = 10


Ignor butKeepthisBit0123AB678901234567890123-56789AndThis with bit
  anything                  |   4   2     4 |  4     6       |  4  1     5  |  Somthing
should be

Ignor butKeepthisBit 0123AB6789 0123456789 0123-56789 AndThis bit
  anything                  |   4   2     4  |  4     6        |  4   1     5  |  Somthing

I'm hopping this clarifies what i'm trying to do.

Thanx
0
 
ozoConnect With a Mentor Commented:
$_='Ignor butKeepthisBit0123AB678901234567890123-56789AndThis with bit';
s/(0\d{3}(?:[A-Z]{2}|[-\d]\d)\d{4})/ $1 /g;s/  0/ 0/g;
print;
0
 
trevor1940Author Commented:
Thanks Very much
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.

All Courses

From novice to tech pro — start learning today.