Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

simple question

by sweepy838 (Acolyte)
on May 05, 2012 at 20:46 UTC ( #969082=perlquestion: print w/ replies, xml ) Need Help??
sweepy838 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, very much a pathetic question but i have a string containing special characters, letters and numbers.
$str = 'xxia5ujcjzbgdiaknqybacf1uhm?6nd9q0icmjloztk?k@i3d@6b'; my @numbers = $str =~ /(\d+)/g; #grab all numbers my @chars=split(//,$str); #convert to char array
now sinse i have all the numbers, i'd like to loop through the char array and print out only the non numberic characters. using replace would do the job but that seems like going the long way ?

Comment on simple question
Download Code
Re: simple question
by moritz (Cardinal) on May 05, 2012 at 21:12 UTC
Re: simple question
by tobyink (Abbot) on May 05, 2012 at 21:46 UTC
    print for grep { not /\d/ } @chars;
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: simple question
by Kenosis (Priest) on May 05, 2012 at 21:53 UTC

    It's not a pathetic question! And moritz is right: "...you could create the array of only non-numeric characters in the first place." If only there was a way to represent the following in such a regex: not 0-9.

    You're almost there...

Re: simple question
by JavaFan (Canon) on May 05, 2012 at 22:04 UTC
Re: simple question
by BillKSmith (Chaplain) on May 06, 2012 at 10:59 UTC

    In perl, a string is not a character array. Some of the replies assume you meant one, others the other. An example would have been a big help to us.

Re: simple question
by sundialsvc4 (Abbot) on May 06, 2012 at 14:19 UTC

    Have a read about the /g modifier in regular expressions:   it lets you apply a regex more than one time to the same string.   Now you can, in a loop, collect all of the character-strings which match “one or more not-a-digit” and concatenate them into a new string.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2014-09-20 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (158 votes), past polls