Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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 ?

Replies are listed 'Best First'.
Re: simple question
by moritz (Cardinal) on May 05, 2012 at 21:12 UTC
Re: simple question
by JavaFan (Canon) on May 05, 2012 at 22:04 UTC
Re: simple question
by Kenosis (Priest) on May 05, 2012 at 21:53 UTC

    It's not a pathetic question! And moritz is right: " 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 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 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.

Re: simple question
by BillKSmith (Priest) 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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2016-10-01 20:19 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (7 votes). Check out past polls.