Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop Help!!!

Status
Not open for further replies.

sasuser2006

Technical User
May 8, 2006
32
US
I'm trying to create a perl script that creates a sql script to load a table to a database based on a CSV file that's passed to perl and I'm having a problem. Currently the piece of the code says:

Code:
...
open (CSV_FILE, "$csv_file") or die "Can't open $csv_file: $!";
open (SQL_FILE, ">$sql_file") or die "Couldn't open $sql_file: $!";

while (<CSV_FILE>)
 {
 	          chomp;
            $line = $_;
            
              {
               my $field = (split (/,/, $line))[0,0];
               my $type = (split (/,/, $line))[0,4];
               my $field_length = (split (/,/, $line))[0,3];
               my $sql_line= $field . " " . $type . "(" . "$field_length" . "),";
            print SQL_FILE "\t\t\t$sql_line\n";
              }
 }
 ...

The problem is in the $sql_line definition it adds a comma which I want except for the final time it goes through. Basically I have:

IP_State varchar2(2),
IP_ZIP varchar2(5),
IP_ZIP4 varchar2(4),

And I want:

IP_State varchar2(2),
IP_ZIP varchar2(5),
IP_ZIP4 varchar2(4)

No comma after IP_ZIP4 varchar2(4)

Can somebody please help me fix this? Thanks ahead of time for any help.
 
there is no need use split() so many times, anway, try this:

Code:
[url=http://perldoc.perl.org/functions/open.html][black][b]open[/b][/black][/url] [red]([/red]CSV_FILE, [red]"[/red][purple][blue]$csv_file[/blue][/purple][red]"[/red][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't open [blue]$csv_file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[black][b]open[/b][/black] [red]([/red]SQL_FILE, [red]"[/red][purple]>[blue]$sql_file[/blue][/purple][red]"[/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Couldn't open [blue]$sql_file[/blue]: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$comma[/blue] = [red]'[/red][purple],[/purple][red]'[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<CSV_FILE>[red])[/red] [red]{[/red]
   [blue]$comma[/blue] = [red]'[/red][purple][/purple][red]'[/red] [olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/eof.html][black][b]eof[/b][/black][/url][red])[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
   [black][b]my[/b][/black] [blue]$line[/blue] = [blue]$_[/blue][red];[/red]
   [black][b]my[/b][/black] [red]([/red][blue]$field[/blue],[blue]$field_length[/blue],[blue]$type[/blue][red])[/red] = [red]([/red][url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url] [red]([/red][red]/[/red][purple],[/purple][red]/[/red], [blue]$line[/blue][red])[/red][red])[/red][red][[/red][fuchsia]0[/fuchsia],[fuchsia]3[/fuchsia],[fuchsia]4[/fuchsia][red]][/red][red];[/red]
   [black][b]my[/b][/black] [blue]$sql_line[/blue] = [red]"[/red][purple][blue]$field[/blue] [blue]$type[/blue]([blue]$field_length[/blue])[blue]$comma[/blue][/purple][red]"[/red][red];[/red]
   [url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] SQL_FILE [red]"[/red][purple][purple][b]\t[/b][/purple][purple][b]\t[/b][/purple][purple][b]\t[/b][/purple][blue]$sql_line[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]



------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
The simplest way is to push the completed statements into an array as you collect them, then use join in the print statement
Code:
my @ddl;

while (<>) {
   chomp;
   my ($col, $type, $len) = split(/,/);
   push @ddl, "$col $type($len)";
   if (some trigger condition) {
      print join(",\n", @ddl), "\n";
      undef @ddl;
   }
}
(not tested)

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top