• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 202
  • Last Modified:

problem when hash value is null

I am retrieving a resultset from the database and pushing the data to hash.

push @{$h{$data[1]}{$data[0]}{$data[2]}{$data[3]}},@data[4,5];

This works fine if the data in 4th and 5th field is not null. But if it is null. The hash is not retrieving the result. Please help
0
saibsk
Asked:
saibsk
  • 5
  • 3
  • 2
1 Solution
 
Adam314Commented:
What do you want to put in your hash when 4th and 5th fields are null?
0
 
saibskAuthor Commented:
I am writing the data to a file. So when the data fields 1, 0, 2, 3 are same I group them and the corresponding 4th and 5th values are written to a file. But when the 4th and 5th fields are null. That record from the result set is not being written to the file.  If the 4th and 5th fields are null I want to write empty strings with corresponsing 0,1,2,3 fields to the file.
0
 
shayne321Commented:
This would be one way:

$val1 = $data[4] || '';
$val2 = $data[5] || '';

push @{$h{$data[1]}{$data[0]}{$data[2]}{$data[3]}}, ($val1, $val2);

In this way if the 4th and 5th fields are undefined the "or" condition will evaluate to an empty string ('')
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
shayne321Commented:
Or, without using temporary variables:

$data[4] = '' unless (defined $data[4]);
$data[5] = '' unless (defined $data[5]);
push @{$h{$data[1]}{$data[0]}{$data[2]}{$data[3]}},@data[4,5];
0
 
saibskAuthor Commented:
doesn't work. Problem still exists. Please advise.
0
 
Adam314Commented:
How are you writing the data to a file?  Post the code for that part of your program.
0
 
saibskAuthor Commented:
for my $key1 (sort keys %h) {      
      
            print FH "\n#Group $key1 Accounts\n";
                  for my $key0 (sort keys %{$h{$key1}}){
                  for my $key2 (sort keys % {$h{$key1}{$key0}}){
                        for my $key3 (sort keys % {$h{$key1}{$key0}{$key2}}){
                              #print "\@\$h{$key1}{$key0}{$key2}{$key3} = @{$h{$key1}{$key0}{$key2}{$key3}}\n";
                  
                  @arr = ($key0,$key1,$key2,$key3,@{$h{$key1}{$key0}{$key2}{$key3}});
            print FH join("\t,",@arr)."\n";
            }
      }
}
      print FH "\n";      
}
0
 
Adam314Commented:
push @{$h{$data[1]}{$data[0]}{$data[2]}{$data[3]}},[@data[4,5]];
0
 
saibskAuthor Commented:
Doesn't seem to work
0
 
saibskAuthor Commented:
I had to make few changes to the resultset with that shayne321 solution works.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now