Solved

Not replicating the query Perl

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

910 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

23 Experts available now in Live!

Get 1:1 Help Now