Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

how to split the string into two or three numbers based on certain value.

by venky4289 (Novice)
on Feb 11, 2013 at 19:43 UTC ( #1018228=perlquestion: print w/replies, xml ) Need Help??
venky4289 has asked for the wisdom of the Perl Monks concerning the following question:

hiw to split the string 79899920179 into 79 89 99 201 79. As i am splitting the string using regular expression /(\d\d)/ But i am getting 79 89 99 20 17 9. I want all the numbers to be greater than 32, if it is less than 32 We need to split into three numbers i.., 201 in this case. Thanks in advance.
  • Comment on how to split the string into two or three numbers based on certain value.

Replies are listed 'Best First'.
Re: how to split the string into two or three numbers based on certain value.
by moritz (Cardinal) on Feb 11, 2013 at 20:51 UTC

    If you really want a regex-solution (which isn't necessarily the best idea), here's one from the bottom of the poison cabinet:

    use strict; use warnings; use 5.010; $_ = '79899920179'; my $pass = qr/(?=)/; my $fail = qr/(*FAIL)/; while (/\G(\d{2,}?)(??{ $1 >= 32 ? $pass : $fail })/g) { say $1; }

    I'm sure that can be simplified somehow, but right now I don't know how.

    Be sure to read the warning about (??{...}) in perlre.

    And here's a nicer Perl 6 solution:

    $ perl6 -e 'say "79899920179".comb(/ (\d **? 2..*) <?{ $0 >= 32 }>/)' 79 89 99 201 79

      A slight simplification. I'm not sure the  \G is necessary: the given test string produces the same groups with/without it. (Update: And the same caveat about a regex solution possibly not being optimum.) See Extended Patterns in perlre.

      >perl -wMstrict -le "my $s = '79899920179'; ;; my @n = $s =~ m{ \G (\d{2,3}?) (?(?{ $^N < 32 }) (*FAIL)) }xmsg; printf qq{'$_' } for @n; " '79' '89' '99' '201' '79'
        Hi this works perfectly fine. Can you explain me the regulsr expression as i am nee to perl programming
Re: how to split the string into two or three numbers based on certain value.
by thundergnat (Deacon) on Feb 11, 2013 at 20:12 UTC

    It's not really clear what you want if you run out of digits and the last number is less than 32. Maybe something like this will work?

    use warnings; use strict; my $string = '79899920179'; my @nums; while (length $string){ no warnings qw/uninitialized/; my $num; $num .= substr($string, 0, 1, '') while (length $string and $num < + 32); push @nums, $num; } print join ', ', @nums;
    79, 89, 99, 201, 79
    
      You'll get the same results if you replace 'length $string' with '$string'.
        Except for cases where you will not. Try 9049589370.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: how to split the string into two or three numbers based on certain value.
by 7stud (Deacon) on Feb 12, 2013 at 19:18 UTC
    use strict; use warnings; use 5.012; my $string = '79899920179'; my @results; my $number; while ($string =~ /(\d)/g) { $number .= $1; next if $number < 32; push @results, $number; $number = ""; } say for @results; --output:-- 79 89 99 201 79

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1018228]
Approved by toolic
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2016-12-05 16:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (89 votes). Check out past polls.