Link to home
Start Free TrialLog in
Avatar of saibsk
saibsk

asked on

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.
Avatar of Adam314
Adam314

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

ASKER CERTIFIED SOLUTION
Avatar of FishMonger
FishMonger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial