> -----Original Message----- > From: Rob Dixon [mailto:[EMAIL PROTECTED] > Sent: Wednesday, July 02, 2003 3:15 PM > To: [EMAIL PROTECTED] > Subject: Re: how to remove last comma in file? > > > Rob Dixon wrote: > > Hi Shaunn. > > > > Shaunn Johnson wrote: > > > Howdy: > > > > > > First off, many thanks to all (esp. R. Dixon and T. Lowery) > > > for the help with the 'using table_info()' thread. > > > > > > I'm just about done with that script, but I have > > > one tiny problem. > > > > > > In my SECOND while loop, I place a comma (,) at the end > > > so that when the *.ddl files are built, it looks > > > just like I've done it by hand. And close to the > > > way I want to use the scripts for porting to Oracle. > > > Almost. > > > > > > I have one comma too many and I would like to know > > > how can I either: > > > > > > 1) go back over the files AFTER they're done and > > > remove the very last comma in the file > > > > > > 2) set up the loop statement so that I only put > > > commas on all of the columns except for the last row > > > of data. > > > > > > Are either of these possible? > > > > > > I am including the script in case anyone is interested > > > in how it's set up. Yes, I know it could be shorter > > > and much, much cleaner - please feel free to muck > > > with it and pass on hints to making it better. > > > > [snip bulk of script] > > > > I think it's this comma you're talking about? > > > > > while ( my($first, $second)=$sth->fetchrow ) { > > > print FILE "$first\t\t$second,\n"; > > > } > > > > Firstly, I'm surprised the 'fetchrow' method still works > > with 'DBI'. What I would normally call here is > > 'fetchrow_array' which returns a list, regardless of > > context. Anyway, the answer... > > > > The classical solution is to print a comma /before/ > > each record unless it is the first record. This is > > often easier because checking for the first item > > is usually easier than checking for the last. In this > > case though, the most straightforward way is to count > > the number of rows you've printed and compare it with > > $sth->rows to see if it needs terminating. Like this > > > > my $row = 0; > > while ( my($first, $second) = $sth->fetchrow ) { > > print FILE "$first\t\t$second"; > > print "," unless ++$row >= $sth->rows; > > print "\n"; > > } > > > > Written in haste, and my apologies to all. The value of > $sth->rows isn't in general guaranteed for 'select' > statements as the database will do its filtering as > required by subsequent 'fetch' operations. So we revert > to the less obvious but fully functional 'classical' > solution: > > my $row = 0; > while ( my($first, $second) = $sth->fetchrow ) { > print "\n," if $row++; > print FILE "$first\t\t$second"; > } > print "\n"; > Or,
my $row = 0; while ( my($first, $second) = $sth->fetchrow ) { print "," if $row++; print FILE "$first\t\t$second\n"; } -Mark -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]