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

Hi dear monks !

Does anyone have a short way (probably regexp) of spliting a string that holds digits separated by any number of spaces or commas, for example:
my $string = '4,5, ,, ,6,,,'; should give after spliting: @splited == (4, 5, 6)
without replacing all commas to spaces and split by spaces ?

Hotshot

Replies are listed 'Best First'.
Re: spliting a string
by joealba (Hermit) on Dec 04, 2001 at 19:02 UTC
    If you want to split something, use split! :)
    my $string = '4,5, ,, ,6,,,'; my @splited = split /[\s,]+/, $string; my @more_robust_split = split /\D+/, $string;
Re: spliting a string
by Rhose (Priest) on Dec 04, 2001 at 19:04 UTC
    Will splitting on non-digits work for ya?

    @splited=split(/\D+/,$string);
Re: spliting a string
by giulienk (Curate) on Dec 04, 2001 at 21:56 UTC
    Here is another way:
    @splited = $string =~ /\d+/g;

    gkinueliileunikg

Re: spliting a string
by hotshot (Prior) on Dec 04, 2001 at 19:25 UTC
    thanks guys, shame on me for not knowing this

    Hotshot
      *Smiles*

      I would not feel too badly -- there have been lots of times I have seen things posted on PM and slapped my head because it was so obvious and I missed it. Case in point -- I used to use the "if !" structure a LOT, then one day I posted a reply and someone commented that I should have used "unless" to make the code more readable.

      From that day on, my icky code has been improved (well, a little.)

      print if ! /bad line/; print unless /bad line/;