Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

find all numeric values from a string and join them

by Anonymous Monk
on May 18, 2018 at 09:46 UTC ( [id://1214830]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, Im trying to have all numeric values from a sting and join them with pipe string is

EEH_ErrorCode=( 15, /* Component */ 65 /* Error */) and my output should be 15|65

while ($flag == 0 ){ if ($str =~ m/\)/ ) { $flag = 1; } else{ my @array=split(/' '/,$str); if(defined $array[0] ){ $array[0] =~ s/[^0-9]//g}else{$array[0] +=''}; if(defined $array[1] ){ $array[1] =~ s/[^0-9]//g}else{$array[1] +=''}; } }

Replies are listed 'Best First'.
Re: find all numeric values from a string and join them
by hippo (Bishop) on May 18, 2018 at 10:10 UTC

    SSCCE:

    use strict; use warnings; use Test::More tests => 1; my $str = 'EEH_ErrorCode=( 15, /* Component */ 65 /* Error */)'; my $want = '15|65'; my $have = join ('|', $str =~ /(\d+)/ag); is $have, $want;

    Meta-update: I've relocated your post into the correct section of this site: Seekers of Perl Wisdom. See Where should I post X? for a discussion of where to post.

Re: find all numeric values from a string and join them
by AnomalousMonk (Archbishop) on May 18, 2018 at 13:19 UTC
Re: find all numeric values from a string and join them
by james28909 (Deacon) on May 19, 2018 at 16:37 UTC
    use strict; use warnings; while(<DATA>){ print "$1|$2" if (/EEH_ErrorCode\=\( (\d+), \/\* Component \*\/ (\ +d+) \/\* Error \*\//); } __DATA__ EEH_ErrorCode=( 15, /* Component */ 65 /* Error */
    OR...
    use strict; use warnings; my @array; while( read( DATA, my $buf, 1 ) != 0 ){ push @array, $buf if $buf =~ /\d/; } print @array; __DATA__ a1b2c3d4

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1214830]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-26 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found