Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

tr/// vs s/// The question.

by muzakfetch (Scribe)
on Dec 31, 2002 at 20:45 UTC ( [id://223454]=perlquestion: print w/replies, xml ) Need Help??

muzakfetch has asked for the wisdom of the Perl Monks concerning the following question:

Howdy everyone.
Was talking to deprecated today about a problem I was having. Basically I wanted to eliminate all unwanted (non-word non-whitespace) and a few other characters.

So I used: s/[^\w\s\/=.\]//g

He said: tr/[^A-Za-z0-9_ ]//d

Now I realize that mine includes a few more characters but basically the moral is that his left nothing but the characters I was seeking to destroy. Which was weird since we used the [^. Anyway here is the complete code and maybe someone can give me some pointers on why his wouldnt work.

#!/usr/bin/perl # setup needed modules. use warnings; use strict; use DBI; use IO::Socket; use Data::Dumper; $|++; # No buffering plz kthx $SIG{HUP} = 'IGNORE'; # solaris likes to kill all of your processes when you exit with a HUP fork and exit; my $dbh = DBI->connect("dbi:Pg:dbname=syslogs"); # setup some sql queries for later #This one inserts the log entry. my $sth = $dbh->prepare("insert into current_logs (host, ip, log) valu +es ( ? , ? , ? )"); #this two are for updating the hosts table. my $sth2 = $dbh->prepare("select hostname from hosts where ip ~* ?"); my $sth3 = $dbh->prepare("insert into hosts (hostname, ip) values ( ? +, ? )"); # Masquerade as the syslogd daemon my $server = IO::Socket::INET->new(LocalPort => 514, Proto => 'udp') or die "Couldnt listen on UDP 514, $!\n"; my %cachedhosts; # used to keep hosts in a hash so we dont keep hammer +ing the name servers. while (my $length=$server->recv(my $data, 65536, 0)) { # now is the ti +me on sprockets when we wait die "sysread: $!" if (!defined($length)); chomp $data; # data hygeine there has to be a better way! $data =~ s/(-|\"|<.*>)//g; #damn pix $data =~ s/^\w{3}\s+\d+\s+\d+:\d+:\d+//; # damn pix $data =~ s/^\w{3}\s+\d+\s+\d+\s+\d+:\d+:\d+:\s+//; #damn cabletrons $data =~ s/[^\w\s=\/.]//g; # deprecated says to use tr/// here but +I cant get it to work. # I think this was nifty but some guru can do this in one line I ju +st know it. if (!$cachedhosts{$server->peerhost()}) { $sth2->execute( $server->peerhost() ); while (my $row = $sth2->fetchrow_array) { $cachedhosts{$server->peerhost()} = $row; } unless ($cachedhosts{$server->peerhost()}) { my $host = gethostbyaddr(inet_aton($server->peerhost()), AF_IN +ET); if (!$host) { $host = qq{unknown.} . $server->peerhost(); } $cachedhosts{$server->peerhost()} = $host; $sth3->execute( $host, $server->peerhost() ); } } # Sometimes the cleaning above leaves nothing behind. # the sonicwall is notorious for sending absolute junk to syslog. unless ($data =~ /^$/) { $sth->execute( $cachedhosts{$server->peerhost()}, $server- +>peerhost(), $data ); } } # in case we escape $dbh->disconnect();
Also any helpful pointers as to how to clean it up a little would be appreciated.

muzakfetch

Replies are listed 'Best First'.
Re: tr/// vs s/// The question.
by lemming (Priest) on Dec 31, 2002 at 20:58 UTC

    tr/[^A-Za-z0-9_ ]//d is the same as tr/A-Za-z0-9_ ]^[//d

    tr doesn't pay attention to the brackets and treats them as characters.

    You'll have better luck with tr/A-Za-z0-9_ //cd
    the c stands for complement, d for delete. See perldoc perlops for more info

Re: tr/// vs s/// The question.
by Aristotle (Chancellor) on Dec 31, 2002 at 20:59 UTC
    Because tr is not a regex and doesn't take character classes. What you wanted is tr/A-Za-z0-9_ //cd Note the /c switch, which means "complement" and is the logical equivalent to a character classes ^ negation.

    Makeshifts last the longest.

Re: tr/// vs s/// The question.
by chromatic (Archbishop) on Dec 31, 2002 at 20:57 UTC

    The transliteration operator doesn't take character classes.

Re: tr/// vs s/// The question.
by ChemBoy (Priest) on Dec 31, 2002 at 20:59 UTC

    Now I realize that mine includes a few more characters but basically the moral is that his left nothing but the characters I was seeking to destroy
    That would be because tr doesn't take a regex character class, it takes a simple list of characters. :-)

    This should do what you want...

    tr{A-Za-z0-9_ .=/\\}{}cd;
    For the record, the /c complements the search list, the /d deletes the matching characters.



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders

Re: tr/// vs s/// The question.
by fuzzycow (Sexton) on Jan 02, 2003 at 17:24 UTC
    Actually I've posted a snippet about this some time ago at http://www.perlmonks.org/index.pl?node_id=82539.

    You can actually review the existing code and snippets sections before you post.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 19:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found