Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check for Blanks

Status
Not open for further replies.

Geek8

Programmer
Apr 23, 2003
31
US
Hello,
I am trying to create a report that checks for populated values in certain positions of a fixed length record. If there is nothing in the string, then it increments a certain counter and if something exists in the string then it increments another counter. This is the code I have for that:

while (<INPUTFILE>) {
chomp;
my @record = unpack('x2 A1 x1309 A1 A1 A1 A1 A1 A1 A1 x11 A16',$_);


if ( $record[0] eq 'N'){
if ($record[8] eq &quot; &quot;){
$no_cons++;
}
else{
$cons++;
}
}

The string is 16 bytes long and has been pulled by using unpack. I tested this out on a record that does not have anything populated for this string, and still got a increment as if it did. Am I doing this script incorrectly? Or does someone have any advice as to how I can avoid this? The values in the string IF it is populated are always different.

Thanks for any help.

Geek8
 
What you're saying is

If the first part is 'N' then
if ninth part is &quot; &quot; then
increment $no_cons by 1
end if
else
increment $cons by 1
End if

Based on what you're saying I dont think its what you meant

Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Paul
This is pretty much what I am saying. Basically check for an empty string and then increment the no_cons counter. If there is something in the string then increment the cons counter. Both cases need to check for a &quot;N&quot; in the first part. What happenned in my test is that the first value would be a &quot;N&quot;, then there would be nothing in the string. Yet it would still update the cons counter. In that case it needs to update the no_cons counter.

Thanks.
 
You mean
Code:
if ($record[0] eq 'N'){
  if ($record[8] eq
&quot; &quot;){
$no_cons++;
} else {
$cons++;
}
}[/code]

You're unpacking a single character into the ninth item of your array, so it'll always count 1. You need to differentiate between spaces and empty strings.

Try this
Code:
$count=0;
foreach $record (@record) {
 print &quot;[&quot;.sprintf(&quot;%0.3d&quot;,$count).&quot;][$record]\n&quot;;
 $count++;
}

HTH
Paul



It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Thanks Paul. That helped. I think I have it working now.


Geek
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top