Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

word with some number in between

by Anonymous Monk
on Jun 29, 2013 at 10:30 UTC ( #1041443=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hey i have array of words say like @array=("aqw","asd8dsa","sd8sd","89","aws"); and I wish to seperate out only those words which have atleat one number in it , like asd8dsa,sd8sd89 . so how can i do it ?

Comment on word with some number in between
Re: word with some number in between
by Happy-the-monk (Monsignor) on Jun 29, 2013 at 10:36 UTC

    I wish to seperate out only those words which have atleat one number in it

    Have a look at regular expressions and grep!

    Cheers, Sören

    (hooked on the Perl Programming language)

      hey i did this , but there is some issue with the command , the first $ is not a part of regex as per perl . with this regex what i tried doing is that some $ before num and some $ after it . please tell what is the mistake

      if ($array_A[$i] =~ /^$+[0-9]+$/)

        The /^$+ part makes no sense. If you you just want to test for the presence of digits then a simple /[0-9]/ will work splendidly.

        " ... what i tried doing is that some $ before num and some $ after it "

        Do you, by any chance, mean you tried to deal with data that included a "$" sign... such as:  abc12$ or  $98abc or  $$zyx5abc or something similar?

        If so, you'll have to escape the "$" sign (with a preceding backslash, "\") in your regex.

        perl -E "my $s='$abc123'; if ($s =~ /\$/ ) { say 'Found a dollar-sign';}

        which outputs:

        Found a dollar-sign

        Otherwise, (slightly oversimplified), the regex engine uses the "$" sign is only to mark an EOL.

        Updated by quoting a possibly mis-understood part of OP's latest question.


        If you didn't program your executable by toggling in binary, it wasn't really programming!

Re: word with some number in between
by Laurent_R (Parson) on Jun 29, 2013 at 12:00 UTC

    Hi

    Consider the following example under the Perl debugger:

    DB<1> @array=("aqw","asd8dsa","sd8sd","89","aws"); DB<2> @c = grep {/\d/} @array; DB<3> x @c 0 'asd8dsa' 1 'sd8sd' 2 89
Re: word with some number in between
by sundialsvc4 (Abbot) on Jun 29, 2013 at 13:41 UTC

    What they’re all trying to say here is this:

    1. A regular expression /[0-9]/ will do nicely:   you want an instance of a character in the inclusive range 0..9, anywhere in the string.
    2. The function grep() is a handy shortcut that, given a list, will return another list containing those entries in the first list that match the regex ... in this case, “your answer.”

Re: word with some number in between
by ww (Bishop) on Jun 29, 2013 at 14:51 UTC
    The spec-by-example is imprecise. Does "89" fit your use of "words" or should it be excluded for lack of an alpha character?

    If the latter (which is the way I read "words") you'll need slightly more complex code:

    #!/usr/bin/perl use 5.016; use strict; use warnings; # 1041443 my @array=("aqw","asd8dsa","sd8sd","89","aws", "17A", "bc24", ); for my $elem (@array) { if ( $elem =~ /\d/ ) { if ( $elem !~ /[\D]/ ) { say "No non-digits in *| $elem |*"; next; } say $elem; } else { say "No numerals in *| $elem |*"; } }

    OUTPUT:

    No numerals in *| aqw |* asd8dsa sd8sd No non-digits in *| 89 |* No numerals in *| aws |* 17A bc24

    If you didn't program your executable by toggling in binary, it wasn't really programming!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1041443]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-12-19 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls