vivekvp has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Here is some code:
use DBI; # Load the DBI module + use CGI; # HTML + use strict; # keep it usable + use CGI qw(:standard); + use CGI::Carp qw( fatalsToBrowser ); + + + ### + my ($sth); + my ($ccode,@ccode); + my(@data,$data); + my ($p_str,@p_str); + my $i=0; # counter + my ($CC,@CC); + my ($po,@po); + my ($old,$new); + my (@old,@new); + my $count; + my $total; + my ($head_date,@head_date); + my $rts; + + open (CC,"ccchg.txt"); + while ($_=<CC>) { + push (@CC,$_); + ($old,$new)=split(/\|/,$_); + push @old,$old; + push @new,$new; + } + close (CC); + + + $i=0; + + while ($i<@new) { + print "old $old[$i] new $new[$i] "; + $i++; + } + + #print "$i - EYE \n"; + + ### Attributes to pass to DBI->connect() to disable automatic + ### error checking + my %attr = ( + PrintError => 0, + RaiseError => 0, + ); + + ### Perform the connection using the Informix driver + my $dbh = DBI->connect( "dbi:Informix:nps\@sumitt_aaa","xxx","yyyy" + ,\%attr )or die "Can't connect to database: ", $DBI::errstr, "\n"; + + $sth=$dbh->prepare("SELECT a.rts_nbr, a.ccode, b.orig_date + FROM rtsdet a, rtshead b + WHERE a.rts_nbr=b.rts_nbr + AND b.status MATCHES '[456OP]' + AND b.orig_date>'2002/01/01' + "); + # AND b.time_stamp>'2002-01-01 00:00:00' - using TIME STAMP + + $sth->execute(); + #print "old cc: $old \n"; + # will dump out all fields $data=$sth->dump_results(); + #$data=$sth->dump_results(); + + while ( ($po, $ccode,$head_date) = $sth->fetchrow_array() ){ + push @po,$po; + } + + print "$po \n"; + + $sth->finish; + + $sth=$dbh->prepare("SELECT rts_nbr,ccode FROM rtsdet + + WHERE ccode=? AND rts_nbr=?"); + + foreach $po(@po) { + for ($count=0;$count<@old;$count++) { + $sth->execute($old[$count],$po); + while ( ($rts,$data) = $sth->fetchrow_array() ){ + print "$po $rts $data $old[$count] $new[$coun +t] \n"; } + } + } + $sth->finish; + $p_str=@po; + print " POs: $p_str \n";
The first loop is a list of Purchase Orders (@po). The second loop is a specific code (@old). Basically this thing should go through and finds the POs that have one of these codes. $rts and $data would be the 2 elements from the SQL search. @new is an arra;y with new specific codes (matching the old on i.e. old->dog new->cat. Ultimately i want to replace those POs that have the old code with the new code. Will the above ONLY print out that criteria met by the SQL search? Or my loops to messy. I would like to use a 2 dimensional array (remeber DIM (10,10) from BASIC). And am not familiar with hashes. Any advice would be welcomed! Thanks, V
help?
He who laughs last, doesn't get the joke.

Replies are listed 'Best First'.
Re: VVP:DBI, arrays and loops
by lachoy (Parson) on Mar 23, 2002 at 00:20 UTC

    It appears that this will do what you want. Have you tried it yet? Since it's not doing any data modification (INSERT/UPDATE) I'd imagine that this would be easy to test. (I love code like that :-)

    You should really turn on RaiseError in your assignment to %attr, since you have zero error checking in all the DBI calls you have here. Otherwise something could fail silently and you'd have a difficult time finding out where/why.

    Also, you might want to look at the code you pasted in and get rid of the extra spaces trailing each line. It's quite distracting.

    Good luck!

    Chris
    M-x auto-bs-mode

Re: VVP:DBI, arrays and loops
by vladb (Vicar) on Mar 23, 2002 at 00:07 UTC
    Since...

    Any advice would be welcomed!

    ... let me toss in my $.02. ;)

    To start with, you should consider changing those 2 letter variables to something less obfuscated (aka more readable etc). At the moment, I have a very hard time understanding the very thing that your code is supposed to do and how it does it. If you need an array to store your purchase order numbers, then call it something like @purchase_orders. Apply same principle to the rest of your variables and this will make the code a little more readable.

    "There is no system but GNU, and Linux is one of its kernels." -- Confession of Faith