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

I need to split xe-2/2/0 to (xe 2 2 0) . Here is my code and is not getting split

my @intf_list = ("xe-1/2/0", "xe-2/2/0"); foreach my $myint (@intf_list) { my @outputlist = split /-\d+/, $myint; print "$outputlist[1]\n"; }

Its not matching any pattern . Is there a way to split 2 different patterns using a single command

Replies are listed 'Best First'.
Re: Splitting 2 different patterns
by choroba (Cardinal) on Mar 17, 2015 at 11:24 UTC
    If your delimiters are / and -, use them in the regex:
    my @outputlist = split m([-/]), $myint;

    You made \d+ part of the delimiter, and as it wasn't captured, it was removed from the returned values. See split.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Splitting 2 different patterns
by Corion (Patriarch) on Mar 17, 2015 at 11:25 UTC

    Can you explain in words what your split code is supposed to do?

    split /-\d+/, $myint;

    Maybe you want to read perlre?

    In practice, I found that it's often much easier to match what I want to keep instead of splitting away the things I don't want:

    my @outputlist = ( $myint =~ m!([^-/]+)!g);
Re: Splitting 2 different patterns
by karlgoethebier (Abbot) on Mar 17, 2015 at 11:37 UTC

    Try  my @outputlist = split /-|\//, $myint;

    Update: Consider also @outputlist = map {[split /-|\//]}  @intf_list;

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re: Splitting 2 different patterns
by Tux (Canon) on Mar 17, 2015 at 14:06 UTC

    Just because I can't resist. Here's some perl6 ways ...

    $ perl6 -e'("xe-1/2/0","xe-2/2/0").split("-")».split("/").say' xe 1 2 0 xe 2 2 0 $ perl6 -e'.split("-")».split("/").say for ("xe-1/2/0","xe-2/2/0")' xe 1 2 0 xe 2 2 0 $ perl6 -e'.split("-")».split("/")».perl.say for ("xe-1/2/0","xe-2/2/0 +")' "xe" "1" "2" "0" "xe" "2" "2" "0" $ perl6 -e'.split(/<[-/]>/)».Str.say for ("xe-1/2/0","xe-2/2/0")' xe 1 2 0 xe 2 2 0 $ perl6 -e'.split(/<[-/]>/)».Str.perl.say for ("xe-1/2/0","xe-2/2/0")' ("xe", "1", "2", "0") ("xe", "2", "2", "0")

    Enjoy, Have FUN! H.Merijn
Re: Splitting 2 different patterns
by jeffa (Bishop) on Mar 17, 2015 at 17:52 UTC

    It appears that you simply want to capture all things that can be matched by \w+:

    perl -le'$_="xe-2/2/0";print for $_ =~ /(\w+)/g'

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Splitting 2 different patterns
by Anonymous Monk on Mar 17, 2015 at 11:53 UTC

    Yet another alternative, the "defensive programming" style:

    use Data::Dumper; my @intf_list = ("xe-1/2/0", "xe-2/2/0", "xf-3/2/a"); foreach my $myint (@intf_list) { my @outputlist = $myint=~/^(\w+)-(\d+)\/(\d+)\/(\d+)$/ or die "Couldn't match \"$myint\""; print Dumper(\@outputlist); } __END__ $VAR1 = ['xe', '1', '2', '0']; $VAR1 = ['xe', '2', '2', '0']; Couldn't match "xf-3/2/a" at - line 4.

      Perhaps a bit more "defensive": my @outputlist = $myint =~ m|(.+)-(.+)/(.+)/(.+)|;

      Regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        But  m|(.+)-(.+)/(.+)/(.+)| will also match  'xf-3/2/a' or even  '@-&-*&^-@#$/_)(/&^/%-$#?!' which seems less, not more, defensive.


        Give a man a fish:  <%-(-(-(-<