Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Split with numbers

by BrowserUk (Pope)
on Nov 24, 2015 at 18:31 UTC ( #1148523=note: print w/replies, xml ) Need Help??


in reply to Split with numbers

Like this?

#! perl -slw use strict; my @examples = qw[ AB23C ABC23 23BC ABC ]; m[([A-Z]*)([0-9]*)([A-Z]*)] and printf "%s: '%s', '%s', '%s'\n", $_, $1//'', $2//'', $3//'' for @examples; __END__ C:\test>1148522 AB23C: 'AB', '23', 'C' ABC23: 'ABC', '23', '' 23BC: '', '23', 'BC' ABC: 'ABC', '', ''

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Split with numbers
by muba (Priest) on Nov 24, 2015 at 19:26 UTC

    Neat.

    Using your code as inspiration, I wrote something that creates the data structure instead of only printing out the results.

    use strict; use warnings; use Data::Dump 'pp'; my @examples = qw[ AB23C ABC23 23BC ABC ]; my @out; push @out, [ m/([A-Z]*)([0-9]*)([A-Z]*)/ ] for @examples; pp @out; __END__ ( ["AB", 23, "C"], ["ABC", 23, ""], ["", 23, "BC"], ["ABC", "", ""], )

      Nice. Though rather than pre-declaring @out and then pushing to it, I'd use map to initialise @out directly:

      @out = map[ m[([A-Z]*)([0-9]*)([A-Z]*)] ], qw[ AB23C ABC23 23BC ABC ]; +; pp \@out;; [ ["AB", 23, "C"], ["ABC", 23, ""], ["", 23, "BC"], ["ABC", "", ""], ]

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Yes. I don't know why I didn't do that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1148523]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2020-07-14 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?