Solved

Not replicating the query Perl

Posted on 2007-11-27
4
174 Views
Last Modified: 2010-03-05
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.
0
Comment
Question by:saibsk
  • 2
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 20359616
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
 
LVL 28

Accepted Solution

by:
FishMonger earned 400 total points
ID: 20360087
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
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 400 total points
ID: 20360113
>> "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
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 100 total points
ID: 20360317
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

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

810 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