?
Solved

spliting strings by index

Posted on 2012-09-11
12
Medium Priority
?
391 Views
Last Modified: 2012-09-13
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
0
Comment
Question by:trevor1940
  • 6
  • 5
12 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 38386495
my $string = "1234567890123AB67890123-567890";
/^\d/ && (length)%10==0 && s/(.{10})(?=.)/$1 /sg for $string
0
 
LVL 1

Author Comment

by:trevor1940
ID: 38386704
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
 
LVL 85

Expert Comment

by:ozo
ID: 38386729
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:trevor1940
ID: 38386822
$len = length($string);

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

=

[30] 1234567890123AB67890123-567890

so yes i'm sure
0
 
LVL 85

Expert Comment

by:ozo
ID: 38387259
Exactly what code did you run?
0
 
LVL 12

Expert Comment

by:tel2
ID: 38389094
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
 
LVL 1

Author Comment

by:trevor1940
ID: 38390377
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
 
LVL 85

Expert Comment

by:ozo
ID: 38390399
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
 
LVL 85

Expert Comment

by:ozo
ID: 38390471
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
 
LVL 1

Author Comment

by:trevor1940
ID: 38391191
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
 
LVL 85

Assisted Solution

by:ozo
ozo earned 2000 total points
ID: 38393237
$_='Ignor butKeepthisBit0123AB678901234567890123-56789AndThis with bit';
s/(0\d{3}(?:[A-Z]{2}|[-\d]\d)\d{4})/ $1 /g;s/  0/ 0/g;
print;
0
 
LVL 1

Author Closing Comment

by:trevor1940
ID: 38394362
Thanks Very much
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans
Suggested Courses

850 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