Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

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
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,
$len = length($string);

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

=

[30] 1234567890123AB67890123-567890

so yes i'm sure
Exactly what code did you run?
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.
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
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
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])
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Very much