Solved

Not replicating the query Perl

Posted on 2007-11-27
4
154 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now