show hidden value in variable.. with mysql

J

joe.henderson1@

All,

I run a mysql db 5.0.11. With a table that contains a few columns
defined as type "text" with indexes..

I running this on a Arch Linux 0.7.1.. 2.6.15 custom.. perl 5.8.8

I run some scripts to parse data from cisco/network devices using snmp
and telnet/ssh..

I retrieve the name, mac, serial, location, description, interfaces,
etc..

The problem I have is the names I recieve from the
devices I run threw a "clean subroutine"..

##########
sub clean
{
my $debug = 0;
my $item = $_[0];
$item =~ s/^\s+//; #remove Leading whitespace
$item =~ s/\s+$//; #remove trailing whitespace
$item =~ s/\r/ /g; #remove those Damn ^M
$item =~ s/\f/ /g; #remove those Damn ^M
$item =~ s/\t/ /g; #remove those Damn ^M
$item =~ s/\n/ /g; #remove those Damn ^M
$item =~ s/\s+/ /g; #replace multiple spaces with one
chomp($item); #remove newline character
return $item;
}
##########

And for some reason when I compare to different names

From DB: $name = "00a45fde0032(sw1)"

And the name I pulled from the device $name: "00a45fde0032(sw1)"
and compare them for exact match

##########
if($name eq $mname)
{ print "MATCH ON NAME: \"$name\" MNAME: \"$mname\"\n"; }
else
{ print "NO MATCH\n"; }
##########

Nothing happens.. No Match.


However when I did a
##########
if($name == $mname)
{ print "MATCH ON NAME: \"$name\" MNAME: \"$mname\"\n"; }
else
{ print "NO MATCH\n"; }
##########

I recieved an error stating "cannot equal on non numeric" which is
what I expected.. However it showed a hidden character of
"MNAME: "00a45fde0032(sw1)\o"

What does the "\o" mean? I have never seen this before..

In the variable their has to be hidden characters that I cannot see.

I thought about converting the string to hex then back to see if
any characters show up..

Or "$item =~ s/[^ A-Za-z0-9\-\:\.\(\)\@]//g;"


A note.. I use the "sub clean" for everthing I receive from my
scripts.. And then insert/update/replace into the database..
I don't know if the column type is causing this error or the
devices that are returning the value.. Due to I cannot see the
hidden values...

If their is a better way of "cleaning" the variables please let me
know..



Joe
 
D

Dr.Ruud

joe.henderson1@ schreef:
$item =~ s/^\s+//; #remove Leading whitespace
$item =~ s/\s+$//; #remove trailing whitespace
$item =~ s/\r/ /g; #remove those Damn ^M
$item =~ s/\f/ /g; #remove those Damn ^M
$item =~ s/\t/ /g; #remove those Damn ^M
$item =~ s/\n/ /g; #remove those Damn ^M
$item =~ s/\s+/ /g; #replace multiple spaces with one
chomp($item); #remove newline character

After some weeding, this remains:

s/^\s+//, s/\s+$//, s/\s+/ /g for $item;

Test:

perl -e '$i="\t\rabc \n def\t\r\n";
s/^\s+//, s/\s+$//, s/\s+/ /g for $i;
print(length $i, ":$i\n")'

7:abc def
 
A

A. Sinan Unur

joe.henderson1@ wrote in 4ax.com:
sub clean
{
my $debug = 0;

What does this line do?
my $item = $_[0];

my ($item) = @_;

would enable you to pass literal strings to this routine as well as
variables.
$item =~ s/^\s+//; #remove Leading whitespace
$item =~ s/\s+$//; #remove trailing whitespace
$item =~ s/\r/ /g; #remove those Damn ^M
$item =~ s/\f/ /g; #remove those Damn ^M
$item =~ s/\t/ /g; #remove those Damn ^M
$item =~ s/\n/ /g; #remove those Damn ^M
$item =~ s/\s+/ /g; #replace multiple spaces with one
chomp($item); #remove newline character
return $item;
}
##########

I would rewrite this subroutine as follows (see also Dr. Ruud's
comments):

sub clean_spaces {
my ($string) = @_;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string =~ s/\s+/ /g;
return $string;
}
And for some reason when I compare to different names

From DB: $name = "00a45fde0032(sw1)"

And the name I pulled from the device $name: "00a45fde0032(sw1)"
and compare them for exact match

##########
if($name eq $mname)
{ print "MATCH ON NAME: \"$name\" MNAME: \"$mname\"\n"; }
else
{ print "NO MATCH\n"; }
##########

Nothing happens.. No Match.

It is not possible that nothing happens.

use constant DEBUG => 1;

....

DEBUG and
warn sprintf("\$name = '%s'\n\$mname = '%s'\n", $name, $mname);

to visually compare the strings during debugging.
However when I did a
##########
if($name == $mname)
{ print "MATCH ON NAME: \"$name\" MNAME: \"$mname\"\n"; }
else
{ print "NO MATCH\n"; }
##########

I recieved an error stating "cannot equal on non numeric" which is
what I expected.. However it showed a hidden character of
"MNAME: "00a45fde0032(sw1)\o"

What does the "\o" mean? I have never seen this before..

Off the top of my head, I have no idea.
I thought about converting the string to hex then back to see if
any characters show up..

Why not?

I am sure there is some pack/unpack magic that would do this in a single
statement, but since I am not that smart, I would use something like:

sub hex_dump_string {
my ($string) = @_;
$string =~ s/(.)/sprintf '%2.2X', ord $1/ges;
return $string;
}
If their is a better way of "cleaning" the variables please let me
know..

For that, it would be useful to see exactly what's in the strings you
are working with.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
J

joe.henderson1@

joe.henderson1@ schreef:


After some weeding, this remains:

s/^\s+//, s/\s+$//, s/\s+/ /g for $item;

Where is the regex for removing the line feeds/new/tab/form feeds?

I see others... for leading ws, trailing ws, and 1 or more replace
with single..
Test:

perl -e '$i="\t\rabc \n def\t\r\n";
s/^\s+//, s/\s+$//, s/\s+/ /g for $i;
print(length $i, ":$i\n")'
Interesting..



7:abc def

Good stuff..

My previous question is repeated here...??


Thanks

Joe
 
J

joe.henderson1@

joe.henderson1@ wrote in 4ax.com:


What does this line do?

All the subs i write have a "magic" debug variable.. I spend more time
on debugging than writting code.. :(
When i turn this on I embed print statments with low level printouts..

ex...

if($debug == 1) { print "Entered sub \"clean\"\n"; }
.... magic ...
if($debug == 1) { print "leaving sub returning: \"$ret\"\n"; }



my $item = $_[0];

my ($item) = @_;

would enable you to pass literal strings to this routine as well as
variables.

K. thanks.. Used a "$tem = shift;" but quit after some problem I don't
remember.. :(

However How would you pass multiple Items..

ex..
########
like ($item) = clean($val1, $val2);

sub clean
{
my $item1 = shift(@_);
my $item2 = shift(@_);
... magic ...
}

########
??


I would rewrite this subroutine as follows (see also Dr. Ruud's
comments):

sub clean_spaces {
my ($string) = @_;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
$string =~ s/\s+/ /g;
return $string;
}


It is not possible that nothing happens.

I get output of "NO MATCH".. not the output of undef...
use constant DEBUG => 1;

I do not have experience with the debug command... Will use.. thanks
for the advise...
...

DEBUG and
warn sprintf("\$name = '%s'\n\$mname = '%s'\n", $name, $mname);

good idea
to visually compare the strings during debugging.


Off the top of my head, I have no idea.


Why not?

I am sure there is some pack/unpack magic that would do this in a single
statement, but since I am not that smart, I would use something like:

sub hex_dump_string {
my ($string) = @_;
$string =~ s/(.)/sprintf '%2.2X', ord $1/ges;
return $string;
}

Good stuff.. I'll read up on the "ord" "/ges"..

don't know what that is... more advance regex..
For that, it would be useful to see exactly what's in the strings you
are working with.

Sinan

Thanks...

Joe
 
J

John W. Krahn

joe.henderson1@ said:
I run a mysql db 5.0.11. With a table that contains a few columns
defined as type "text" with indexes..

I running this on a Arch Linux 0.7.1.. 2.6.15 custom.. perl 5.8.8

I run some scripts to parse data from cisco/network devices using snmp
and telnet/ssh..

I retrieve the name, mac, serial, location, description, interfaces,
etc..

The problem I have is the names I recieve from the
devices I run threw a "clean subroutine"..

##########
sub clean
{
my $debug = 0;
my $item = $_[0];
$item =~ s/^\s+//; #remove Leading whitespace
$item =~ s/\s+$//; #remove trailing whitespace
$item =~ s/\r/ /g; #remove those Damn ^M
$item =~ s/\f/ /g; #remove those Damn ^M
$item =~ s/\t/ /g; #remove those Damn ^M
$item =~ s/\n/ /g; #remove those Damn ^M
$item =~ s/\s+/ /g; #replace multiple spaces with one
chomp($item); #remove newline character
return $item;
}
##########

sub clean
{
my $debug = 0;
#remove those Damn ^M etc. and replace multiple spaces with one
( my $item = $_[0] ) =~ tr/ \r\f\t\n/ /s;
#remove leading and trailing spaces
s/^ +//, s/ +$// for $item;
return $item;
}
And for some reason when I compare to different names

From DB: $name = "00a45fde0032(sw1)"

And the name I pulled from the device $name: "00a45fde0032(sw1)"
and compare them for exact match

##########
if($name eq $mname)
{ print "MATCH ON NAME: \"$name\" MNAME: \"$mname\"\n"; }
else
{ print "NO MATCH\n"; }
##########

Nothing happens.. No Match.


However when I did a
##########
if($name == $mname)
{ print "MATCH ON NAME: \"$name\" MNAME: \"$mname\"\n"; }
else
{ print "NO MATCH\n"; }
##########

I recieved an error stating "cannot equal on non numeric" which is
what I expected.. However it showed a hidden character of
"MNAME: "00a45fde0032(sw1)\o"

What does the "\o" mean? I have never seen this before..

Are you sure that that is "\o" and not "\0"? It could be a NULL terminated "C
string".



John
 
J

joe.henderson1@

joe.henderson1@ said:
I run a mysql db 5.0.11. With a table that contains a few columns
defined as type "text" with indexes..

I running this on a Arch Linux 0.7.1.. 2.6.15 custom.. perl 5.8.8

I run some scripts to parse data from cisco/network devices using snmp
and telnet/ssh..

I retrieve the name, mac, serial, location, description, interfaces,
etc..

The problem I have is the names I recieve from the
devices I run threw a "clean subroutine"..

##########
sub clean
{
my $debug = 0;
my $item = $_[0];
$item =~ s/^\s+//; #remove Leading whitespace
$item =~ s/\s+$//; #remove trailing whitespace
$item =~ s/\r/ /g; #remove those Damn ^M
$item =~ s/\f/ /g; #remove those Damn ^M
$item =~ s/\t/ /g; #remove those Damn ^M
$item =~ s/\n/ /g; #remove those Damn ^M
$item =~ s/\s+/ /g; #replace multiple spaces with one
chomp($item); #remove newline character
return $item;
}
##########

sub clean
{
my $debug = 0;
#remove those Damn ^M etc. and replace multiple spaces with one
( my $item = $_[0] ) =~ tr/ \r\f\t\n/ /s;
#remove leading and trailing spaces
s/^ +//, s/ +$// for $item;
return $item;
}

Thanks for the edition
Are you sure that that is "\o" and not "\0"? It could be a NULL terminated "C
string".

No.. it was "\o".. However if it was.. How could it have occurred?

Advise,

Joe
 
S

Sherm Pendley

joe.henderson1@ said:
Where is the regex for removing the line feeds/new/tab/form feeds?

Those are all different forms of white space, so \s catches them. Of
course, \s catches spaces too, but no harm is done by replacing a space
with a space.

sherm--
 
A

A. Sinan Unur

joe.henderson1@ wrote in 4ax.com:
All the subs i write have a "magic" debug variable.. I spend more time
on debugging than writting code.. :(
When i turn this on I embed print statments with low level printouts..

But that is tedious because you have to edit the body of each sub to
turn debugging on.
my $item = $_[0];

my ($item) = @_;

would enable you to pass literal strings to this routine as well as
variables.

K. thanks.. Used a "$tem = shift;" but quit after some problem I don't
remember.. :(

However How would you pass multiple Items..

ex..
########
like ($item) = clean($val1, $val2);

sub clean
{
my $item1 = shift(@_);
my $item2 = shift(@_);

my ($item1, $item2) = @_;

Or, if you are applying the same transformation to an indefinite number
of arguments:

sub clean {
my @args = @_;
for ( @args ) {
s/^\s+//;
s/\s+$//;
s/\s+/ /;
}
return @args;
}
I do not have experience with the debug command... Will use.. thanks
for the advise...

There is no debug command. I am just proposing setting a global flag for
debugging mode. You could have just as well called it CHEESE.
good idea

Well, then, you might want
Good stuff.. I'll read up on the "ord" "/ges"..

don't know what that is... more advance regex..

perldoc perlop

Now, if you do post the contents of those variables, we might be able to
help further.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
K

Keith Keller

joe.henderson1@ wrote in 4ax.com:

But that is tedious because you have to edit the body of each sub to
turn debugging on.

True, but if you're debugging only one sub, it's helpful to turn on only
those debug messages, rather than turning on all debug messages, which
might swamp a human debugger.

--keith
 
U

Uri Guttman

jh> On Thu, 20 Apr 2006 00:11:16 GMT, "A. Sinan Unur"

jh> All the subs i write have a "magic" debug variable.. I spend more time
jh> on debugging than writting code.. :(

that is a bad sign. learn to analyze your problems better, improve your
coding skills, etc. debugging should be maybe 20-40% maximum of your
development time.

jh> When i turn this on I embed print statments with low level printouts..

jh> if($debug == 1) { print "Entered sub \"clean\"\n"; }
jh> ... magic ...
jh> if($debug == 1) { print "leaving sub returning: \"$ret\"\n"; }

ewww. that is boring sub tracing. and slow in runtime.

jh> I do not have experience with the debug command... Will use.. thanks
jh> for the advise...


that isn't a debug command. that just create a constant called DEBUG
jh> good idea

it also will run faster than your if you set DEBUG to 0

learn about alternate quote. but even better, why are you using ""
inside a "" string? you can use '', [], <> or {} to demark the actual
values. then you don't need those ugly backwhacks. i tend to use [] for
this.

uri
 
D

Dr.Ruud

John W. Krahn schreef:
sub clean
{
my $debug = 0;
#remove those Damn ^M etc. and replace multiple spaces with one
( my $item = $_[0] ) =~ tr/ \r\f\t\n/ /s;
#remove leading and trailing spaces
s/^ +//, s/ +$// for $item;
return $item;
}

Alternative:

tr/ \r\f\t\n/ /s, s/^ //, s/ $// for $item

but that is less ready for publishing than

s/\s+/ /g, s/^ //, s/ $// for $item

because the tr// as shown, misses several whitespace-incarnations that
are in Unicode.
 
A

Anno Siegel

Dr.Ruud said:
joe.henderson1@ schreef:


After some weeding, this remains:

s/^\s+//, s/\s+$//, s/\s+/ /g for $item;

....or even

s/\s+/ /g, s/^\s//, s/\s$// for $item;

Anno
 
A

Anno Siegel

jh> All the subs i write have a "magic" debug variable.. I spend more time
jh> on debugging than writting code.. :(

that is a bad sign. learn to analyze your problems better, improve your
coding skills, etc. debugging should be maybe 20-40% maximum of your
development time.

Coding is debugging an empty source file :)

Anno
 
D

Dr.Ruud

Dr.Ruud schreef:
Test:

perl -e '$i="\t\rabc \n def\t\r\n";
s/^\s+//, s/\s+$//, s/\s+/ /g for $i;
print(length $i, ":$i\n")'

7:abc def

A less platform dependent version:

perl -e '$i = qq[\t\r123 \f\x{2028}\n 567\t\r\n];
s/\s+/ /g, s/^ //, s/ $//, for $i;
print (length $i, qq[:$i\n])'

(for Windows: put on a single line and change the outer ' to ")
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
A. Sinan Unur said:
joe.henderson1@ wrote in 4ax.com:
[...]
my $item = $_[0];

my ($item) = @_;

would enable you to pass literal strings to this routine as well as
variables.

What difference would it make?

Admittedly, not much in the OP's case.

D:\Home\asu1\UseNet\clpmisc> cat test1.pl
#!/usr/bin/perl

use strict;
use warnings;

print s2t1('nos much'), "\n";
print s2t2('nos much'), "\n";

sub s2t1 {
my (@args) = @_;

s/s/t/ for @args;
return @args;
}

sub s2t2 {
s/s/t/ for @_;
return @_;
}


D:\Home\asu1\UseNet\clpmisc> test1
not much
Modification of a read-only value attempted at D:\Home\asu1\UseNet
\clpmisc\test1.pl line 17.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
A

Anno Siegel

A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in
A. Sinan Unur said:
joe.henderson1@ wrote in 4ax.com:
[...]

my $item = $_[0];

my ($item) = @_;

would enable you to pass literal strings to this routine as well as
variables.

What difference would it make?

Admittedly, not much in the OP's case.

D:\Home\asu1\UseNet\clpmisc> cat test1.pl
#!/usr/bin/perl

use strict;
use warnings;

print s2t1('nos much'), "\n";
print s2t2('nos much'), "\n";

sub s2t1 {
my (@args) = @_;

s/s/t/ for @args;
return @args;
}

sub s2t2 {
s/s/t/ for @_;
return @_;
}


D:\Home\asu1\UseNet\clpmisc> test1
not much
Modification of a read-only value attempted at D:\Home\asu1\UseNet
\clpmisc\test1.pl line 17.

Yes, of course, but that isn't what the OP tried to do.

sub foo {
my $item = $_[ 0]; # this is what the OP did
# ...
}

and

sub foo {
my ( $item) = @_; # this is what you suggested instead
# ...
}

are exactly equivalent. In s2t2() you are using the elements of @_
directly. That *is* different.

Anno
 
A

A. Sinan Unur

(e-mail address removed)-berlin.de (Anno Siegel) wrote in
A. Sinan Unur said:
(e-mail address removed)-berlin.de (Anno Siegel) wrote in
joe.henderson1@ wrote in 4ax.com:

[...]

my $item = $_[0];

my ($item) = @_;

would enable you to pass literal strings to this routine as well
as variables.

What difference would it make?
....
sub s2t2 {
s/s/t/ for @_;
return @_;
}


D:\Home\asu1\UseNet\clpmisc> test1
not much
Modification of a read-only value attempted at D:\Home\asu1\UseNet
\clpmisc\test1.pl line 17.

Yes, of course, but that isn't what the OP tried to do.

sub foo {
my $item = $_[ 0]; # this is what the OP did
# ...
}

and

sub foo {
my ( $item) = @_; # this is what you suggested instead
# ...
}

are exactly equivalent. In s2t2() you are using the elements of @_
directly. That *is* different.

OOOOOOPS! No excuse this time. Thanks for catching that.

Sinan
--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
D

Dr.Ruud

Anno Siegel schreef:
Dr.Ruud:

...or even

s/\s+/ /g, s/^\s//, s/\s$// for $item; (2a)

Heheh, it was already one step further:

s/\s+/ /g, s/^ //, s/ $// for $item; (2b)

Next steps:
(3) A variant with the "/x" modifier.
(4) A multiline-variant that lets the linebreaks stay.


My first tries, not yet tested or optimized or Unicode-ready:

s/\s+/\x{20}/gx, s/^\x{20}/x, s/\x{20}$//x for $item; (3)

(EBCDIC & friends just left the building)


s/[\x{20}\f\r\t]+/\x{20}/gmsx, s/^\x{20)/gmsx, s/\x{20}$//gmsx for
$item; (4)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top