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

Re^2: Filtering unwanted chars from input field

by johngg (Abbot)
on Dec 17, 2012 at 21:02 UTC ( #1009230=note: print w/replies, xml ) Need Help??


in reply to Re: Filtering unwanted chars from input field
in thread Filtering unwanted chars from input field

Instead of spliting and greping it might be simpler to use tr (see Quote and Quote like Operators).

$ perl -E ' > @data = ( > q{Hello, world!}, > q{te-st%$/*.}, > q{All_of_this_is_OK.}, > q{!@#$%^&*()12345}, > ); > say for map { tr{A-Za-z0-9_.}{}cd; $_ } @data;' Helloworld test. All_of_this_is_OK. 12345 $

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^3: Filtering unwanted chars from input field
by Kenosis (Priest) on Dec 17, 2012 at 21:10 UTC

    Thank you, johngg. Not sure why I complicated it. Yours is, indeed, a more elegant and readable solution (++). Was brought back to this node after the following should-have-done-this-in-the-first-place solution occurred to me:

    $str =~ s/[^$acceptable]//gi;

    Edited my original comment to reflect this...

Re^3: Filtering unwanted chars from input field
by Anonymous Monk on Dec 18, 2012 at 14:01 UTC
    Would I be breaking the law by allowing a "-" and updating the code to:
    sub filter { my $str = shift; defined $str or return ''; $str =~tr{A-Za-z0-9_.-}{}cd; return $str; }

      That will work ok.

      $ perl -E ' > @data = ( > q{Hello, world!}, > q{te-st%$/*.}, > q{All_of_this_is_OK.}, > q{!@#$%^&*()12345}, > ); > say for map { tr{A-Za-z0-9_.-}{}cd; $_ } @data;' Helloworld te-st. All_of_this_is_OK. 12345 $

      Cheers,

      JohnGG

        What is I want to allow spaces as well? \s+ does not work.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009230]
help
Chatterbox?
[1nickt]: my $x = $aohoaoh->[0]->{' foo'}->[0]->{'bar' }; should work
[thepkd]: not indexing
[thepkd]: getting data out of the ds
[choroba]: it depends. Give more details, please
[thepkd]: i used a series of {}'s but it dont work
[choroba]: You need square brackets for arrays
[GotToBTru]: you can certainly construct a single expression to access any part of the data structure, without using temp variables. but you might make it easier on yourself and any other poor soul who has to understand your code if you do
[thepkd]: to dereference i mean
LanX .oO( oh tempz, oh moretz)
[thepkd]: @GotToBTru Sure. But.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (13)
As of 2016-12-06 13:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (104 votes). Check out past polls.