Filter String

By Zmodem on Aug 21, 2008

This is a 'very simple' snippet that allows you to strip everything from a string, but the numbers.

Use: $filter_num(string)

For example: $filter_num(abc123kaljsdflkjdsfkljaslkjsda456lakjdflksadj7kalsdkoi8kljadsflkjdsa9)

Will return: 123456789

alias filter_num {
  var %i = $1-, %c = 1, %t = $mid(%i,1,0), %h, %r
  while (%c <= %t) {
    %h = $mid(%i,%c,1)
    if (%h isnum) %r = %r $+ $mid(%i,%c,1)
    inc %c
  }
  return %r
}

Comments

Sign in to comment.
Zmodem   -  May 11, 2009

That should work exactly as you have it.

 Respond  
ScreWe   -  May 09, 2009

How would I get something like this to work?

      if  (amp; isin %ipb.link) {
        $remove ($1,amp;)
      }
 Respond  
rhasttaff   -  Sep 15, 2008

was useful to me Thanks!

 Respond  
Zmodem   -  Aug 23, 2008

How about Ymodem, Xmodem & Kermit? :)

Oh, and thanks for the comments! :) I appreciate your fine analogy of something I was trying to do ;) It's great to hear people's responses, whether supportive or critical! It is, however, the only way we learn! :)

Thanks again!

 Respond  
Typo   -  Aug 22, 2008

I've just always done that out of habbit. I think its something I started doing after reading this from the help file on IF.

The ( ) brackets enclose comparisons, whereas the { } brackets enclose the commands you want to be performed if a comparison is true. You must make sure that the number of ( ) and { } brackets match to make sure that the correct comparisons are made, and that the correct commands are executed.

Using brackets speeds up processing. If an alias uses too few brackets then the statement might be ambiguous and the alias will take longer to parse, might be parsed incorrectly, or might not be parsed at all.
The second half being the part I'm talking about and the speeds up part in particular.
In the end I know were talking milliseconds but hey, a tiny bit more effecient is still more effecient.

Anyways, Zmodem, I forgot to say earlier, good post, we need more posts like this, sure its not the best code on the planet but you saw a problem, coded a solution and posted a working and usefull peice of code that even got a few people talking so they could give you some ideas.

Keep it up man.

Btw, your nickname takes me back like 15 years or so when I was running a wwiv bbs and zmodem was the latest and greatest protocal. Good times. I was such a noob!

 Respond  
Eugenio   -  Aug 22, 2008

wtf is this all about ffs, I know what it does but wtf @ all these comments

 Respond  
guest598594   -  Aug 22, 2008

Yea, hi 5! But I just figured what the hell. Also I'd have had to add all them other codes, like: !@#$%^&*_-=+;':"><./? and ALL of the ALT+KeyPad combos you can perform, like: æ°Þ¡þ blah blah. Anyhow, thanks.

Yea, good point...

I imagine typo's first one would have worked (assuming he put the backslash before "D"), but for some reason the comments strips backslashes.

 Respond  
Zmodem   -  Aug 22, 2008

Sweet. Very fun, I'd say. but why the { }? Why not just

 alias filter_num return $regsubex($1-,/[^0-9]/gi,) 

Doesn't look any dirtier or cleaner; preference I guess ;)

 Respond  
Typo   -  Aug 22, 2008

Ok then how about:

alias filter_num { return $regsubex($1-,/[^0-9]/gi,) }

That seemed to pass the line u gave just fine and a couple other ones I tried.

 Respond  
Scakk   -  Aug 22, 2008

Or full on alias

Code:

alias filtering {
  if ($isid) {
    if ($prop = c) {
      var %string $1-, %e 256
      while (%e) {
        if (($chr(%e) isalpha) || ($chr(%e) isnum)) { %string = $remove(%string, $chr(%e)) }
        dec %e
      }
      return $iif(%string, Results for Characters: %string , No results for Characters.)
    }
    if ($prop = l) { 
      var %string $1-, %e 256
      while (%e) {
        if ($chr(%e) !isalpha) { %string = $remove(%string, $chr(%e)) }
        dec %e
      }
      return $iif(%string, Results for Letters: %string , No results for Letters.)
    }
    if ($prop = n || !$prop) {
      var %string $1-, %e 256
      while (%e) {
        if ($chr(%e) !isnum) { %string = $remove(%string, $chr(%e)) }
        dec %e
      }
      return $iif(%string, Results for Numbers: %string , No results for Numbers.)
    }
  }
}
 Respond  
Zmodem   -  Aug 22, 2008

Typo: Using yours, this won't work:

 //echo -s $filter_num(983939jhkjajlkadsjdfhsay@*$@^&^$@$&@$:'></?) 

However, it works with mine...

FUR...forget it: FURBY: Good idea, why not submit? lol

 Respond  
Typo   -  Aug 22, 2008

You could always just use:

alias filter_num { return $regsubex($1-,/\D/gi,) }

;p

 Respond  
napa182   -  Aug 22, 2008

@ FURBY*
you can do it with out the second if as well

alias filter_num {
  var %s $1-, %e 256
  while (%e) {
    if ($chr(%e) !isnum) %s = $remove(%s,$v1) 
    dec %e
  }
  return %s
}
 Respond  
F*U*R*B*Y*   -  Aug 22, 2008
alias filter_numbers {
  var %string $1-, %e 256
  while (%e) {
    if ($chr(%e) !isnum) { %string = $remove(%string, $chr(%e)) }
    dec %e
  }
  return %string
}

alias filter_letters {
  var %string $1-, %e 256
  while (%e) {
    if ($chr(%e) !isalpha) { %string = $remove(%string, $chr(%e)) }
    dec %e
  }
  return %string
}

alias filter_chars {
  var %string $1-, %e 256
  while (%e) {
    if (($chr(%e) isalpha) || ($chr(%e) isnum)) { %string = $remove(%string, $chr(%e)) }
    dec %e
  }
  return %string
}

winner? :P

 Respond  
Zmodem   -  Aug 22, 2008

Yea, hi 5! But I just figured what the hell. Also I'd have had to add all them other codes, like: !@#$%^&*_-=+;':"><./? and ALL of the ALT+KeyPad combos you can perform, like: æ°Þ¡þ blah blah. Anyhow, thanks.

 Respond  
guest598594   -  Aug 21, 2008

or simply

$remove($1,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z)

:P

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.