How Can I Find/Remove a Null from a String?

M

msargent100

Perl newbie question -

I am having a problem finding the end of a string. I determined there
are nulls at the end but I do not know how many nor how to get rid of
them. I did a reverse on the variable and then looking at substr($x, 0,
1), I get a null returned.

What happened -

I am reading from a binary file that has fixed length fields but the
contents of the field may be variable length strings. To get the data I
need I pull the contents of the field using the substr function with an
offset and length of the field. So the text field is always
64-characters long, but the values in the field will vary considerably.

So my question is: How can I find the end of the characters and assign
only the valid characters to the variable and drop the nulls?

I'm trying to use the data extracted from the binary file to
access/insert into a database. SQL does not like nulls at the end of
the values being inserted.

Thanks,
Mike
 
A

A. Sinan Unur

(e-mail address removed) wrote in @f14g2000cwb.googlegroups.com:
I am having a problem finding the end of a string. I determined there
are nulls at the end but I do not know how many nor how to get rid of
them. I did a reverse on the variable and then looking at substr($x,
0, 1), I get a null returned.

I am not sure what information you are trying to convey by the last
statement.

#! /usr/bin/perl

my $s = qq{This is a test\000\000\000\000\000\000\000};

print 'length $s = ', length $s, "\n";

$s = substr $s, 0, index $s, "\000";

print 'length $s = ', length $s, "\n"
__END__

D:\Home\asu1\UseNet\clpmisc> n
length $s = 21
length $s = 14

Sinan
 
J

Jürgen Exner

I am having a problem finding the end of a string. I determined there
are nulls at the end but I do not know how many nor how to get rid of
them. I did a reverse on the variable and then looking at substr($x,
0, 1), I get a null returned. [...]
So my question is: How can I find the end of the characters and
assign only the valid characters to the variable and drop the nulls?

Many different ways:
- use index() to find the first \000 and then use substr() to extract the
good part of the string from 1 to that position
- use s/// to replace the trailing \000s with nothing
- use tr/// to transliterate (actually delete) \000s
- "while" the last character of the string is \000 (can easily be checked
with a negative offset argument to substr) shorten the string by one
character
- reverse the string, "while" the first character is \000 remove it, reverse
the string
- split() the string into an array of single characters, grep() for
non-\000, and join() again
- split() the string into an array of single characters, map all \000 into
an empty string, and join() again
- ...

I am sure there are many more obscure ways to do it.

jue
 
M

m

A. Sinan Unur said:
I am not sure what information you are trying to convey by the last
statement.

#! /usr/bin/perl

my $s = qq{This is a test\000\000\000\000\000\000\000};

print 'length $s = ', length $s, "\n";

$s = substr $s, 0, index $s, "\000";

print 'length $s = ', length $s, "\n"
__END__

D:\Home\asu1\UseNet\clpmisc> n
length $s = 21
length $s = 14

Sinan

#! /usr/bin/perl
my $s = qq{This is a test\000\000\000\000\000\000\000};
print "length of string is ",length $s,"\n" ;
$s=~/\000/;
print "length is ",length $`,"\n" ;

is this a "worse" form when compared to the above program?am jst
starting off with perl!
 
A

A. Sinan Unur

m said:
#! /usr/bin/perl
my $s = qq{This is a test\000\000\000\000\000\000\000};
print "length of string is ",length $s,"\n" ;
$s=~/\000/;
print "length is ",length $`,"\n" ;

is this a "worse" form when compared to the above program?am jst
starting off with perl!

They do different things.

I try to write what I mean. If the purpose of the code is to get rid of
everything after the first \000, then that's what it should do.

Sinan
 
J

John W. Krahn

Perl newbie question -

I am having a problem finding the end of a string. I determined there
are nulls at the end but I do not know how many nor how to get rid of
them. I did a reverse on the variable and then looking at substr($x, 0,
1), I get a null returned.

What happened -

I am reading from a binary file that has fixed length fields but the
contents of the field may be variable length strings. To get the data I
need I pull the contents of the field using the substr function with an
offset and length of the field. So the text field is always
64-characters long, but the values in the field will vary considerably.

So my question is: How can I find the end of the characters and assign
only the valid characters to the variable and drop the nulls?

I'm trying to use the data extracted from the binary file to
access/insert into a database. SQL does not like nulls at the end of
the values being inserted.

Use unpack() for fixed length fields:

$ perl -le'
my $x = "abcdef\0\0\0\0\0\0\0";
print "Length: ", length $x;
my $y = unpack "Z13", $x;
print "Length: ", length $y;
'
Length: 13
Length: 6


perldoc -f pack
perldoc perlpacktut



John
 

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,777
Messages
2,569,604
Members
45,222
Latest member
patricajohnson51

Latest Threads

Top