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-5 67890";
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
Thanx
in sudo code this is what i need to do
my $string = "1234567890123AB67890123-5
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;
}
}
Thanx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you sure you had
my $string = "1234567890123AB67890123-5 67890";
or was there an extra character?
If you had
my $string = "1234567890123AB67890123-5 67890\n";
then length($string) /10 would be 3.1, not a whole number,
my $string = "1234567890123AB67890123-5
or was there an extra character?
If you had
my $string = "1234567890123AB67890123-5
then length($string) /10 would be 3.1, not a whole number,
ASKER
$len = length($string);
print "[$len] $string\n";
=
[30] 1234567890123AB67890123-56 7890
so yes i'm sure
print "[$len] $string\n";
=
[30] 1234567890123AB67890123-56
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.
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.
ASKER
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 thisBit0123AB7890123456789 0123-56789 AndThis 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
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 thisBit0123AB7890123456789
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 thisBit0123AB7890123456789 0123-56789 AndThis bit
Instead of 10 characters each, it looks like the lengths of the sections in your example are
12, 9, 10, 10, 11
Need thisBit0123AB7890123456789
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])
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])
ASKER
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 butKeepthisBit0123AB678901 2345678901 23-56789An dThis 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
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 butKeepthisBit0123AB678901
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Very much
ASKER
added print"$string" after the two line of code and i get back
1234567890123AB67890123-56
what i'd hope to get is
1234567890 123AB67890 123-567890