Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Extraction of numbers in an string

by Not_a_Number (Prior)
on Nov 25, 2016 at 20:20 UTC ( [id://1176552]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Extraction of numbers in an string
in thread Extraction of numbers in an string

Even simpler: no need for grep.

my @x = split /\D+/, $string;

Update: tybalt89 and AnomalousMonk are both right, of course. My bad :(

Replies are listed 'Best First'.
Re^5: Extraction of numbers in an string
by tybalt89 (Monsignor) on Nov 25, 2016 at 20:47 UTC

    Nope

    #!/usr/bin/perl -l # http://perlmonks.org/?node_id=1176478 use strict; use warnings; my $string = '<1,22>:<5,7>:<333,0>'; my @x = split /\D+/, $string; print /\d/ ? $_ : 'empty string' for @x;

    prints

    empty string 1 22 5 7 333 0
Re^5: Extraction of numbers in an string
by AnomalousMonk (Archbishop) on Nov 25, 2016 at 20:50 UTC

    But without some sort of grep step, that produces a spurious leading empty string in the output list:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $string = '<1,22>:<5,7>:<333,0>'; my @x = split /\D+/, $string; dd \@x; " ["", 1, 22, 5, 7, 333, 0]


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-24 17:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found