Not replicating the query Perl

I have to retrieve a result set and store the data in a file. I have a column in the database result. If the result is Y i want to store then data in one file and if the result is N I want to store it in another file. How can do this without replicating the select statement in Perl. I know I could use the sprintf but I am not sure how. Please advise.
saibskAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
FishMongerConnect With a Mentor Commented:
Your question implies that you're currently using 2 select statements, 1 for 'Y' and 1 for 'N'.  Instead you should use a single select statement that returns both.

Adams approach is probably the most common method used to handle this, but it could be simplified.  I'm assuming he used a slightly more verbose approach for the sake of clarity.

First, I'm assuming that your select statement is constructed to return either 'Y' or 'N', but not NULL or an empty sting.
Next, I'd use a lexical variable for the filehandle (a hash in this case), and I'd use the 3 arg form of open.

Here's how I'd reduce Adams code.
my %fh;
open($fh{'Y'}, '>', 'file_y.txt') or die "file_y: $!\n";
open($fh{'N'}, '>', 'file_n.txt') or die "file_n: $!\n";
 
while( my $dataref = $sth->fetchrow_arrayref ) {
    print {$fh{$dataref->[0]}} join(",", @$dataref) . "\n";
}

Open in new window

0
 
Adam314Commented:
Something like this:
#assuming you have the statement handle in $sth already executed
open(Y, ">file_y.txt") or die "file_y: $!\n";
open(N, ">file_n.txt") or die "file_y: $!\n";
while(my $dataref=$sth->fetchrow_arrayref) {
    if($dataref->[0] eq 'Y') {        #Use your column instead of 0
        print Y join(",", @$dataref) . "\n";    #or print however you want
    }
    elsif($dataref->[0] eq 'N') {     #Use your column instead of 0
        print N join(",", @$dataref) . "\n";    #or print however you want
    }
    else {
        warn "Data is not an N or Y\n";
    }
}
close(Y);
close(N);

Open in new window

0
 
FishMongerConnect With a Mentor Commented:
>> "First, I'm assuming that your select statement is constructed to return either 'Y' or 'N', but not NULL or an empty sting."

If that assumption is wrong, we'd need to add the conditional test.


while( my $dataref = $sth->fetchrow_arrayref ) {
    if($dataref->[0] =~ /^(Y|N)$/) {  # Use your column instead of 0
        print {$fh{$dataref->[0]}} join(",", @$dataref) . "\n";
    }
    else {  # depending on your needs, you may not need the else clause
        warn "Data is not an N or Y\n";
    }
}

Open in new window

0
 
Adam314Connect With a Mentor Commented:
Using a hash to store the file handles is a nicer method - as it makes the rest of the code simplier.

For the if statement, you could do this:
    if($fh{$dataref->[0]}) {  # Use your column instead of 0
        print .....


This would allow you to create a filehandle for any other possible entries in the column (eg: "Maybe"), and only have to change the open statements.
   
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.

All Courses

From novice to tech pro — start learning today.