Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Small string problem

Reply
Thread Tools

Small string problem

 
 
PeterT
Guest
Posts: n/a
 
      09-04-2003
I'm trying to extract the numerical part and the letter part of a short
string.

i.e.

$mix = "6KL" --> $num = "6" and $let = "KL"

I can't find a way to do it with split, or substr

anybody can give me a clue?


--
petert


 
Reply With Quote
 
 
 
 
Shawn Corey
Guest
Posts: n/a
 
      09-04-2003
PeterT wrote:

> I'm trying to extract the numerical part and the letter part of a short
> string.
>
> i.e.
>
> $mix = "6KL" --> $num = "6" and $let = "KL"
>
> I can't find a way to do it with split, or substr
>
> anybody can give me a clue?
>
>


$mix =~ /(\d+)([A-Za-z]+)/;
$num = $1;
$let = $2;

 
Reply With Quote
 
 
 
 
Lao Coon
Guest
Posts: n/a
 
      09-04-2003
"PeterT" <(E-Mail Removed)> wrote in news:bj7ir8$gq$(E-Mail Removed):

> I'm trying to extract the numerical part and the letter part of a short
> string.
>
> i.e.
>
> $mix = "6KL" --> $num = "6" and $let = "KL"
>
> I can't find a way to do it with split, or substr
>
> anybody can give me a clue?


Read perldoc perlre

E.g.

my ($num,$let) = $mix =~ / ( \d+ ) ( [[:alpha:]]+ ) /x;

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-04-2003
PeterT wrote:

> I'm trying to extract the numerical part and the letter part of a short
> string.
>
> i.e.
>
> $mix = "6KL" --> $num = "6" and $let = "KL"
>
> I can't find a way to do it with split, or substr
>
> anybody can give me a clue?


my ($num, $let) = $mix =~ /^(\d+)([A-Z]+)$/;

note that this matches one or more digits followed by one or more
letters in caps. Use i if mixed case is ok.

If it always has this form: exactly one digit followed by exactly 2
letters you could use substr:

$num = substr($mix, 0, 1);
$let = substr($mix, 1);

IIRC

--
Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
virtual home: http://johnbokma.com/ ICQ: 218175426
John web site hints: http://johnbokma.com/websitedesign/

 
Reply With Quote
 
PeterT
Guest
Posts: n/a
 
      09-04-2003
"Christian Caron" <(E-Mail Removed)> wrote in message
news:bj7kdb$(E-Mail Removed)...
>
> "Christian Caron" <(E-Mail Removed)> wrote in message
> news:bj7jis$(E-Mail Removed)...
>
> > Easy solution:
> >
> > ($num = $mix) =~ s/[a-zA-Z]//g;
> > ($let = $mix) =~ s/[0-9]//g;
> >
> > Better solution: ?
> >

>
> Please note my solution will work with mixed numbers and letters ($mix =
> '6K8L'). You did not mention if you needed that capacity. Also, will it
> contain other characters? What to do with them?


Thanks a lot already, and a good point brought to my attention.

I didn't clarify my problem enough, even to myself.

Sometimes I have one or two digits following, which I don't want with $num
but with $let . The leading number, which is not always there, needs to be
seperated from the following string, which always starts with a letter but
can have numbers at the end.

$mix = "17HD21" --> $num = "17" , $let = "HD21"

possible examples are:

"HN" => " ","HN"

"7HN" => "7","HN"

"132HD3" => "132","HD3"

--
petert


 
Reply With Quote
 
PeterT
Guest
Posts: n/a
 
      09-04-2003
"Lao Coon"
> "PeterT"


> > I'm trying to extract the numerical part and the letter part of a short
> > string.
> >
> > i.e.
> >
> > $mix = "6KL" --> $num = "6" and $let = "KL"
> >
> > I can't find a way to do it with split, or substr
> >
> > anybody can give me a clue?

>
> Read perldoc perlre
>
> E.g.
>
> my ($num,$let) = $mix =~ / ( \d+ ) ( [[:alpha:]]+ ) /x;


Thanks, I assume that would do it fine, but as I expanded in my reply to
Christian, I haven't looked at all permutations of my problem.

--
petert


 
Reply With Quote
 
PeterT
Guest
Posts: n/a
 
      09-04-2003
"John Bokma"
> "PeterT"


> > I'm trying to extract the numerical part and the letter part of a short
> > string.
> >
> > i.e.
> >
> > $mix = "6KL" --> $num = "6" and $let = "KL"
> >
> > I can't find a way to do it with split, or substr
> >
> > anybody can give me a clue?

>
> my ($num, $let) = $mix =~ /^(\d+)([A-Z]+)$/;
>
> note that this matches one or more digits followed by one or more
> letters in caps. Use i if mixed case is ok.


Ta, similar to the suggestion Lao made, though my problem expanded as
I stated earlier.

--
petert


 
Reply With Quote
 
Ryan Shondell
Guest
Posts: n/a
 
      09-04-2003
"PeterT" <(E-Mail Removed)> writes:
(snip)

> Sometimes I have one or two digits following, which I don't want with $num
> but with $let . The leading number, which is not always there, needs to be
> seperated from the following string, which always starts with a letter but
> can have numbers at the end.
>
> $mix = "17HD21" --> $num = "17" , $let = "HD21"
>
> possible examples are:
>
> "HN" => " ","HN"
>
> "7HN" => "7","HN"
>
> "132HD3" => "132","HD3"


I think this seems to do what you're looking for:

my ($num, $let) = $mix =~ /^(\d+)?(.*)/;

It appears to work when $mix is any of your examples.


Ryan
--
perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
$;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
=shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
+shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
 
Reply With Quote
 
Ryan Shondell
Guest
Posts: n/a
 
      09-04-2003
"Christian Caron" <(E-Mail Removed)> writes:

> "Ryan Shondell" <(E-Mail Removed)-state.edu> wrote in message
> news(E-Mail Removed)-state.edu...
> > "PeterT" <(E-Mail Removed)> writes:
> > (snip)
> >
> > > Sometimes I have one or two digits following, which I don't want with

> $num
> > > but with $let . The leading number, which is not always there, needs to

> be
> > > seperated from the following string, which always starts with a letter

> but
> > > can have numbers at the end.

>
> >
> > I think this seems to do what you're looking for:
> >
> > my ($num, $let) = $mix =~ /^(\d+)?(.*)/;
> >
> > It appears to work when $mix is any of your examples.

>
> This would work:
>
> ($let = $mix) =~ s/^(\d+)//;
> $num = $1;
>
> It would catch any numbers (if they are at the beginning of the string) and
> put them in $1 (later assigned to $num), and also remove these numbers from
> $let.


First, why would you use the search and replace operator (s///), when
you really just want a match? But more importantly, this solution
could have some problems if you have previously put something in $1,
but there are no numbers in your string.

$mix = "132HD5";
($let = $mix) =~ s/^(\d+)//;
$num = $1;

# $num = 132 and $let = HD5

$mix = "YO";
($let = $mix) =~ s/^(\d+)//;
$num = $1;

# $num = 132 and $let = YO
# whoops...


Ryan
--
perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
$;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
=shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
+shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      09-04-2003
Ryan Shondell <(E-Mail Removed)-state.edu> wrote:
> "Christian Caron" <(E-Mail Removed)> writes:



>> This would work:
>>
>> ($let = $mix) =~ s/^(\d+)//;
>> $num = $1;
>>
>> It would catch any numbers (if they are at the beginning of the string) and
>> put them in $1

^^^^^^^^^^^^^^

But only when the s/// _succeeded_.


>> (later assigned to $num), and also remove these numbers from
>> $let.



> this solution
> could have some problems if you have previously put something in $1,



It has the same problem if you have _not_ previously put something
in $1, its value will just be undef.


> but there are no numbers in your string.



Right.

What matters is not what is in $1.

What matters is whether the match succeeded or not.

It it failed, $1 will have "stale" contents from some earlier
(successful) match, or undef.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Small cameras getting too small? GRL Digital Photography 50 02-03-2006 03:12 AM
Small Square with small red X Peter Coddington Computer Support 4 01-03-2006 06:58 AM
modifying small chunks from long string MackS Python 6 11-15-2005 03:02 AM
sizeof(std::string) seems to small jl_post@hotmail.com C++ 12 09-28-2005 10:10 PM
Small inconsistency between string.split and "".split Carlos Ribeiro Python 11 09-17-2004 05:57 PM



Advertisments