how match/expose the value in an empty variable having length of 1

J

Jack

Hi folks,

Ok I am stumped... I am reading a fileline in as follows:

12535957@140@2006-07-08 09:34:45.000@@@2005-11-24@

and splitting on the "@"...
open(SOURCE1,$filename1)
@columnarray = split(/\@/, $_);

Whats really wierd is $columnarray[4] has a length of 1, when clearly
above its NULL !!
I want to be able to recognize this as null, but you cant even match
regex it... I tried

if ($columnarray[4] =~ m/.*?[[:print:]]/) { print " NON PRINTABLE
"."\n"; }
$columnarray[4] =~ s/^\s+//;
$columnarray[4] =~ s/\s+$//;
$length = length @columnarray[4]."\n";
and a number of other things that dont work.. how do I identify "WHAT"
is making up the length =1 ?

Also, what is the detection match test for NULL ?

Thank you all much,
Jack
 
J

Jack

Jack said:
Hi folks,

Ok I am stumped... I am reading a fileline in as follows:

12535957@140@2006-07-08 09:34:45.000@@@2005-11-24@

and splitting on the "@"...
open(SOURCE1,$filename1)
@columnarray = split(/\@/, $_);

Whats really wierd is $columnarray[4] has a length of 1, when clearly
above its NULL !!
I want to be able to recognize this as null, but you cant even match
regex it... I tried

if ($columnarray[4] =~ m/.*?[[:print:]]/) { print " NON PRINTABLE
"."\n"; }
$columnarray[4] =~ s/^\s+//;
$columnarray[4] =~ s/\s+$//;
$length = length @columnarray[4]."\n";
and a number of other things that dont work.. how do I identify "WHAT"
is making up the length =1 ?

Also, what is the detection match test for NULL ?

Thank you all much,
Jack

NEVERMIND.. sorry.. I figured it out.. no need to post.
 
T

Tad McClellan

Jack said:
$length = length @columnarray[4]."\n";
^^
^^ this is one character long

And you should always enable warnings when developing Perl code.

how do I identify "WHAT"
is making up the length =1 ?


By looking closely at the string above that is the argument for length().

Also, what is the detection match test for NULL ?


Perl does not have anything known as NULL. I expect you mean
an "empty string".

print "empty string\n" unless length $columnarray[4];

or

print "empty string\n" if $columnarray[4] =~ /^\z/;
 
J

J. Gleixner

Jack said:
Hi folks,

Ok I am stumped... I am reading a fileline in as follows:

12535957@140@2006-07-08 09:34:45.000@@@2005-11-24@

and splitting on the "@"...
open(SOURCE1,$filename1)
@columnarray = split(/\@/, $_);

Post real code as an example. If your code is actually the above, then
it has a lot of problems.
Whats really wierd is $columnarray[4] has a length of 1, when clearly
above its NULL !!

How do you know its length is 1?
I want to be able to recognize this as null, but you cant even match
regex it... I tried

if ($columnarray[4] =~ m/.*?[[:print:]]/) { print " NON PRINTABLE
"."\n"; }
$columnarray[4] =~ s/^\s+//;
$columnarray[4] =~ s/\s+$//;
$length = length @columnarray[4]."\n";

Adding these to your code would have caught the error..

use strict;
use warnings;

my $length = length $columnarray[4]
and a number of other things that dont work.. how do I identify "WHAT"
is making up the length =1 ?

Also, what is the detection match test for NULL ?

Maybe this will help you:

perldoc -f defined

A short script as an example:

#!/usr/bin/perl
use strict;
use warnings;
my $str='12535957@140@2006-07-08 09:34:45.000@@@2005-11-24@';
my @columnarray = split(/\@/, $str);
print "length=", length( $columnarray[4] ), "\n";
print "columnarray[4] is an undefined value" unless defined $columnarray[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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top