For Quality purpouses, Lone Wolf 's mail on Thursday 05 February 2004 04:23 
may have been monitored or recorded as:

Hi
 
> Thank goodness I never said I had perfect code, because I would
> definitely be lying.

no worries - I post code to get feedback. Thats the whole ideaof learning it.

> I attached 2 files, one the beginning data, the other the .sql file that
> I load into MySQL database.  The files are about 3000 lines before and
> after so I cut out the first 30 lines and put them in the files to the
> list.

Ok - then, again: Do not read these files into mem at once unless you really 
have to (which should be close to never).

here is a script that uses your given data:

---snip---
#!/usr/bin/perl

use strict;
use warnings;
my (@fields, $lng);

opendir INDIR , "./sql" or die "Can't open dir with before files:$!";

foreach my $infile (grep {!/^\./} readdir INDIR) {
#read all the files in your home/sql dir
#read only files that do not start with a .
  my ($i,$rec);

  open INFILE, "<./sql//$infile" or die "Can't open $infile: $!";
  open OUTFILE, ">./${infile}.out" or die "Can't open ${infile}.out at home: 
$!";
  while (<INFILE>) {
   $rec++;
   chomp;
   @fields = split /\s*\|\s*/, $_;
   $fields[0] =~ s/^\s+//; 
   #there is probably a way to get rid of the trailing spaces in the first 
entry using split,I just couldnt think of any.

   $lng = @fields unless $lng; #set $lng for first record
   print "The following record: $i has ", scalar @fields, " fields as compared 
to $lng fields in the first record! Skip. : $_\n" and next unless $lng == 
@fields;
#poor quality control of your input data: check if all reords have the same 
number of fields or skip and print record otherwise.
   $i++;
   print OUTFILE $i;
   print OUTFILE "|$_" foreach (@fields);
   print OUTFILE "|$fields[0]\n"; #your trailing ID
  }
  close INFILE;
  close OUTFILE;
  print "Read $rec records from ./sql/$infile and printed $i into ./
${infile}.out\n";
}
closedir INDIR;
---snap---

A couple of hints:

The script reads all files in the sql subdir of your home dir and produces the 
corrosponding filname.out in your homedir.

the split splits as written by Jeff et al.
I coulndt think of a better way to substtute the leading spaces for the first 
field.
Anyone better suggestions?

you end up with a final \n in each outfile.

You rewrite it into a sub by substititing the line
foreach my $infile (grep {!/^\./} readdir INDIR) {
with

sub whatever{
...
foreach my $infile (@_) {

and call th sub with
&whatever ("file1", "file2", ...);

of course you may want to change the open statements to, if you dont have your 
infiles in ./sql

Hope that gets you started, Wolf







-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to