Solved

problem when hash value is null

Posted on 2007-11-27
10
190 Views
Last Modified: 2010-03-05
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
Comment
Question by:saibsk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 20361442
What do you want to put in your hash when 4th and 5th fields are null?
0
 

Author Comment

by:saibsk
ID: 20361483
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
 

Expert Comment

by:shayne321
ID: 20361686
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
shayne321 earned 500 total points
ID: 20361707
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
 

Author Comment

by:saibsk
ID: 20361782
doesn't work. Problem still exists. Please advise.
0
 
LVL 39

Expert Comment

by:Adam314
ID: 20361785
How are you writing the data to a file?  Post the code for that part of your program.
0
 

Author Comment

by:saibsk
ID: 20361805
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
 
LVL 39

Expert Comment

by:Adam314
ID: 20361862
push @{$h{$data[1]}{$data[0]}{$data[2]}{$data[3]}},[@data[4,5]];
0
 

Author Comment

by:saibsk
ID: 20365540
Doesn't seem to work
0
 

Author Comment

by:saibsk
ID: 20365859
I had to make few changes to the resultset with that shayne321 solution works.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
delete query using perl dbi 3 122
instmodsh - listing perl modules 2 97
Perl script not writing to .csv file 6 125
Convert grep lines to perl 6 101
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question