http://qs321.pair.com?node_id=1148533


in reply to Re: Split with numbers
in thread Split with numbers

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", "", ""], )

Replies are listed 'Best First'.
Re^3: Split with numbers
by BrowserUk (Patriarch) on Nov 24, 2015 at 19:33 UTC

    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.