Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How can i extract a number from an string

by Sombrerero_loco (Beadle)
on Jan 22, 2009 at 11:45 UTC ( #738117=perlquestion: print w/replies, xml ) Need Help??

Sombrerero_loco has asked for the wisdom of the Perl Monks concerning the following question:

Hi monkers! I need to do the next and really, i dont know how. I need to from the $variable that contains the next strings (from an array foreach) BS0-bleble, BS1-bleble, BS2-bleble, BS3-bleble, BS4-bleble and BS5-bleble.. Extract the number and compare (to do the comparison its done) but to split the number from the string, i dont know. I know i must to first of all an split to stract the BSx- from the bleble, im doing that like this:
foreach $var1 (@array) {
($hierarchie,$trash)= split('-',$var1);

}
But i dont know how i should do to split the number from the BSx (from 0 to 5) in another variable. Thanks monks!
  • Comment on How can i extract a number from an string

Replies are listed 'Best First'.
Re: How can i extract a number from an string
by Bloodnok (Vicar) on Jan 22, 2009 at 11:51 UTC
    foreach (@array) { /^BS(\d)/; my $num = $1; . . }

    A user level that continues to overstate my experience :-))
      /^BS(\d)/ or warn("Invalid frobniz id ($_)\n"), next;

      Or possibly die on bad input. Never ignore it.

      (And, it's better to be explicit about the loop variable IMHO, $_ is a missed opportunity to write self documented code).

      /J

      and this should capture only the number from the string "BS4-SuchaString" ???
        As the RE says, if the string begins with BS and is followed by a single digit, that digit will be captured - see perlre.

        A user level that continues to overstate my experience :-))
Re: How can i extract a number from an string
by gone2015 (Deacon) on Jan 22, 2009 at 11:56 UTC

    Consider a regular expression. There is also the Perl regular expressions quick start and the tutorial. If you are going to mess about with Perl, you ought to get to grips with regular expressions.

    In particular, consider a regular expression using a "capture". (Update: as demonstrated by some quicker monks.)

Re: How can i extract a number from an string
by JavaFan (Canon) on Jan 22, 2009 at 11:52 UTC
    my ($number) = $var1 =~ /([0-9]+)/; # Or my $number = substr $var1, 2, 1;
Re: How can i extract a number from an string
by setebos (Beadle) on Jan 22, 2009 at 12:25 UTC
    split will work only on one digit number:
    (split(//,"BS0-bleble"))[2]
      Not if you know split:
      $ perl -wE 'say +(split/([0-9]+)/,"BS123-bleble")[1]' 123

      An alternative to JavaFan's use of a capture in the split regexp could be to use look-arounds to do the split at points where the string changes from non-digit to digit and vice-versa. I think JavaFan's method is better in this case but the look-arounds can sometimes be a useful technique.

      $ perl -le ' > $_ = q{BS123-bleble}; > print +( split m{(?x) (?: (?<=\D)(?=\d) | (?<=\d)(?=\D) )} )[ 1 ];' 123 $

      I hope this is of interest.

      Cheers,

      JohnGG

        Wow....such complicate to extract the number. I never think its takes too many code to extract just a number, i thought using split the code must be easier. I'll try it today and keep you posted.. Thanks everyone for the replies!
        :::When you dream, there're no rules, ppl can fly...anything can happen!!!:::

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://738117]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2023-03-22 05:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which type of climate do you prefer to live in?






    Results (60 votes). Check out past polls.

    Notices?