Filtering a string

Discussion in 'Perl Misc' started by Bill H, Jun 20, 2008.

  1. Bill H

    Bill H Guest

    Can someone point me to some docs on how I would do this without
    iterating over the whole string (pattern matching?):

    $original = "a malformed%string/containi\"ng characters I don'~t
    want! ...";

    $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

    $new = &fix($original);

    $new would now equal:

    amalformedstringcontainingcharactersidontwant

    sub fix
    {
    my $o = shift;
    my $r = "";
    my $i = 0;
    for($i = 0;$i < length($o);$i++)
    {
    if(index($filter,uc(substr($o,$i,1))) != -1){$r .= substr($o,$i,1);}
    }
    return($r);
    }

    I just typed this in to give you the gist of what I want to do so if
    there are errors it is in my typing.

    Bill H
     
    Bill H, Jun 20, 2008
    #1
    1. Advertising

  2. Bill H

    Dave B Guest

    Bill H wrote:

    > Can someone point me to some docs on how I would do this without
    > iterating over the whole string (pattern matching?):
    >
    > $original = "a malformed%string/containi\"ng characters I don'~t
    > want! ...";
    >
    > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
    >
    > $new = &fix($original);
    >
    > $new would now equal:
    >
    > amalformedstringcontainingcharactersidontwant


    Is the following acceptable for you?

    $new = lc(join("",grep(m/[$filter]/i,split(//,$original))));

    --
    D.
     
    Dave B, Jun 20, 2008
    #2
    1. Advertising

  3. Bill H

    Ben Morrow Guest

    Quoth Bill H <>:
    > Can someone point me to some docs on how I would do this without
    > iterating over the whole string (pattern matching?):
    >
    > $original = "a malformed%string/containi\"ng characters I don'~t
    > want! ...";
    >
    > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
    >
    > $new = &fix($original);
    >
    > $new would now equal:
    >
    > amalformedstringcontainingcharactersidontwant


    $orginal =~ tr/a-zA-Z0-9_-//cd;

    See tr/// under 'Regexp Quote-Like Operators' in perlop.

    Note that '-' must come first or last, as otherwise it will be
    interpreted as part of an X-Y range.

    Ben

    --
    For far more marvellous is the truth than any artists of the past imagined!
    Why do the poets of the present not speak of it? What men are poets who can
    speak of Jupiter if he were like a man, but if he is an immense spinning
    sphere of methane and ammonia must be silent? [Feynmann]
     
    Ben Morrow, Jun 20, 2008
    #3
  4. Bill H wrote:
    > Can someone point me to some docs on how I would do this without
    > iterating over the whole string (pattern matching?):
    >
    > $original = "a malformed%string/containi\"ng characters I don'~t
    > want! ...";
    >
    > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
    >
    > $new = &fix($original);
    >
    > $new would now equal:
    >
    > amalformedstringcontainingcharactersidontwant


    $ perl -le'
    $original = "a malformed%string/containi\"ng characters I don\047~t
    want\041 ...";
    $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

    ( $new = $original ) =~ s/[^\Q$filter\E]+//ig;
    print for $original, $new;
    '
    a malformed%string/containi"ng characters I don'~t want! ...
    amalformedstringcontainingcharactersIdontwant



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
     
    John W. Krahn, Jun 20, 2008
    #4
  5. Bill H

    Bill H Guest

    On Jun 20, 1:48 pm, Ben Morrow <> wrote:
    > Quoth Bill H <>:
    >
    > > Can someone point me to some docs on how I would do this without
    > > iterating over the whole string (pattern matching?):

    >
    > > $original = "a malformed%string/containi\"ng characters I don'~t
    > > want! ...";

    >
    > > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

    >
    > > $new = &fix($original);

    >
    > > $new would now equal:

    >
    > > amalformedstringcontainingcharactersidontwant

    >
    > $orginal =~ tr/a-zA-Z0-9_-//cd;
    >
    > See tr/// under 'Regexp Quote-Like Operators' in perlop.
    >
    > Note that '-' must come first or last, as otherwise it will be
    > interpreted as part of an X-Y range.
    >
    > Ben
    >
    > --
    > For far more marvellous is the truth than any artists of the past imagined!
    > Why do the poets of the present not speak of it? What men are poets who can
    > speak of Jupiter if he were like a man, but if he is an immense spinning
    > sphere of methane and ammonia must be silent? [Feynmann]    


    Ben this one seems to work nice for me, only issue is I have to lc()
    the results (not really a problem). Is there a way to have that done
    in this ? Also thanks for pointing out the part in perlop that deals
    with it.

    Bill H
     
    Bill H, Jun 21, 2008
    #5
  6. Bill H

    Ben Morrow Guest

    Quoth Bill H <>:
    > On Jun 20, 1:48 pm, Ben Morrow <> wrote:
    > > Quoth Bill H <>:
    > >
    > > > Can someone point me to some docs on how I would do this without
    > > > iterating over the whole string (pattern matching?):

    > >
    > > > $original = "a malformed%string/containi\"ng characters I don'~t
    > > > want! ...";

    > >
    > > > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";

    > >
    > > > $new = &fix($original);

    > >
    > > > $new would now equal:

    > >
    > > > amalformedstringcontainingcharactersidontwant

    > >
    > > $orginal =~ tr/a-zA-Z0-9_-//cd;
    > >
    > > See tr/// under 'Regexp Quote-Like Operators' in perlop.
    > >
    > > Note that '-' must come first or last, as otherwise it will be
    > > interpreted as part of an X-Y range.

    >
    > Ben this one seems to work nice for me, only issue is I have to lc()
    > the results (not really a problem). Is there a way to have that done
    > in this ?


    Well, tr/-_a-zA-Z0-9\000-\177/-_a-za-z0-9/d will work, but personally
    I'd probably just use lc, and probably beforehand, to avoid two
    alphabetic ranges. Something like

    (my $new = lc $original) =~ tr/a-z0-9_-//cd;

    seems clearer than relying on the details of tr///, which is an operator
    a lot of Perl programmers don't know very well.

    Ben

    --
    Outside of a dog, a book is a man's best friend.
    Inside of a dog, it's too dark to read.
    Groucho Marx
     
    Ben Morrow, Jun 21, 2008
    #6
  7. Bill H

    Dave Weaver Guest

    On Fri, 20 Jun 2008 19:44:37 +0200, Dave B <> wrote:
    > Bill H wrote:
    >
    > > Can someone point me to some docs on how I would do this without
    > > iterating over the whole string (pattern matching?):
    > >
    > > $original = "a malformed%string/containi\"ng characters I don'~t
    > > want! ...";
    > >
    > > $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
    > >
    > > $new = &fix($original);
    > >
    > > $new would now equal:
    > >
    > > amalformedstringcontainingcharactersidontwant

    >
    > Is the following acceptable for you?
    >
    > $new = lc(join("",grep(m/[$filter]/i,split(//,$original))));
    >


    That won't do what the OP wants - try it with "=", "[" or "<" in
    $original, for example.
     
    Dave Weaver, Jun 23, 2008
    #7
  8. Bill H

    Dave B Guest

    Dave Weaver wrote:

    > On Fri, 20 Jun 2008 19:44:37 +0200, Dave B <> wrote:
    >> Bill H wrote:
    >>
    >>> Can someone point me to some docs on how I would do this without
    >>> iterating over the whole string (pattern matching?):
    >>>
    >>> $original = "a malformed%string/containi\"ng characters I don'~t
    >>> want! ...";
    >>>
    >>> $filter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_";
    >>>
    >>> $new = &fix($original);
    >>>
    >>> $new would now equal:
    >>>
    >>> amalformedstringcontainingcharactersidontwant

    >> Is the following acceptable for you?
    >>
    >> $new = lc(join("",grep(m/[$filter]/i,split(//,$original))));
    >>

    >
    > That won't do what the OP wants - try it with "=", "[" or "<" in
    > $original, for example.


    Yes, the "-" in the filter should be at the very beginning or end, I
    overlooked that. However, after seeing the solutions based on tr///, I
    realized that this is not by any means a good solution. I'm still learning...

    --
    D.
     
    Dave B, Jun 23, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas
    Replies:
    1
    Views:
    1,639
    Tuukka Toivonen
    Dec 2, 2003
  2. Jon Sequeira
    Replies:
    3
    Views:
    4,167
    gerry
    Jun 26, 2003
  3. Sam Vanderstraeten

    Adding textboxes for filtering in DataGrid header

    Sam Vanderstraeten, Oct 1, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    495
    Sam Vanderstraeten
    Oct 1, 2003
  4. Sam Vanderstraeten

    Adding textboxes for filtering in DataGrid header

    Sam Vanderstraeten, Oct 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    2,173
    Sam Vanderstraeten
    Oct 2, 2003
  5. Corey
    Replies:
    4
    Views:
    6,852
    Corey
    Feb 16, 2005
Loading...

Share This Page