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

my script goes like this

use DBI; print "Enter your file name\n"; chomp($filename=<>); #open (OUT,">>$filename.csv") || die print "Noooooooooooooo"; #print OUT "count,date,Sample_Type,Patient_ID,Sample_Id,Doctor_Id +,Location,Rotor,Serial,QC,HEM,LIP,ICT,para\n"; #close OUT; open (IN,"$filename.txt") || die print "Noo Input"; my @file=<IN>; my $string = join('#', @file); # print "$string"; $string =~ s/\n//g; $string =~ s/\t//g; while($string=~/Equine Profile Plus\s*#(.*?) .*?(Sample. +*?)##/igs){ my($date,$line,$Sample_Type,$Patient_ID,$Sample_Id,$Doctor_Id,$Loc +ation,$Rotor,$Serial,$para,$QC,$HEM,$LIP,$ICT); $count++; $date=$1; $line=$2; #print "$line\n"; if ($line=~/Sample Type:(.*?)#/gis){ $Sample_Type=clean($1); }if ($line=~/Patient ID:(.*?)#/gis){ $Patient_ID=clean($1); }if ($line=~/Sample ID:(.*?)#/gis){ $Sample_Id=clean($1); }if ($line=~/Doctor ID:(.*?)#/gis){ $Doctor_Id=clean($1); }if ($line=~/Location:(.*?)#/gis){ $Location=clean($1); }if ($line=~/Rotor Lot Number:(.*?)#/gis){ $Rotor=clean($1); }if ($line=~/Serial Number:(.*?)#/gis){ $Serial=clean($1); }if ($line=~/#(NA+.*?GLOB.*?)#/gis){ $para=$1; $para =~ s/#/;/g; $para =~ s/\s\s/ /g; $para =~ s/\s\s/ /g; $para =~ s/\s\s/ /g; $para =~ s/\s\s/ /g; $para =~ s/\s\s/ /g; $para =~ s/\s\s/ /g; $para =~ s/ /:/g; $para =~ s/mmol\/L//g; $para =~ s/U\/L/ /g; $para =~ s/mg\/dL/ /g; $para =~ s/g\/dL/ /g; $para=clean($para); }if ($line=~/#QC(.*?) #HEM(.*?) LIP(.*?) ICT(.*?) /gis){ $QC=clean($1); $HEM=clean($2); $LIP=clean($3); $ICT=clean($4); } open (OUT,">>$filename.csv") || die print "Noooooooooooooo"; print OUT "\"$count\",\"$date\",\"$Sample_Type\",\"$Patient_ID\", +\"$Sample_Id\",\"$Doctor_Id\",\"$Location\",\"$Rotor\",\"$Serial\",\" +$QC\",\"$HEM\",\"$LIP\",\"$ICT\",\"$para\"\n"; } close OUT; #Load csv into mysql &loaddata('$filename.csv'); sub clean { my ($line) = shift (@_); $line =~ s/\n//g; $line =~ s/\r//g; $line =~ s/^\s+//g; $line =~ s/\s\s//g; $line =~ s/\s+$//g; $line =~ s/#//g; return ($line); } #init the mysql DB sub init_dbh{ my $dbh = DBI->connect ('DBI:mysql:pfam','username','password') or di +e "Can't connect to database: $DBI::errstr\n"; return $dbh; } #Load data to mysql table sub loaddata{ my ($name) = @_; my $DBH = init_dbh( ); $STH_GO = $DBH->prepare(q{ LOAD DATA LOCAL INFILE '?' INTO TABLE authors FI +ELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n'; }) +or die "ERROR: ".$DBI::errstr; $STH_GO->execute($name); }

the data used is a text file ie

VetScan VS2 Equine Profile Plus 13 Oct 2010 09:19 Sample Type: Horse Patient ID: 019-8592 Sample ID: 019-8592 Doctor ID: 1 Location: 1 Rotor Lot Number: 0052AA8 Serial Number: 0000V01219 .............................. NA+ 136 126-146 mmol/L K+ 3.0 2.5-5.2 mmol/L tCO2 28 20-33 mmol/L CK 216 120-470 U/L GLU 109 65-110 mg/dL CA 12.2 11.5-14.2 mg/dL BUN 13 7-25 mg/dL CRE 1.5 0.6-2.2 mg/dL AST 326 175-340 U/L TBIL 1.7 0.5-2.3 mg/dL GGT 5 5-24 U/L ALB 3.3 2.2-3.7 g/dL TP 7.3 5.7-8.0 g/dL GLOB 4.0 2.7-5.0 g/dL QC OK HEM 0 LIP 0 ICT 0
now my problem is i have inserted the whole data except the numeric values into the script. i have to designate the numeric values as reported value and reference range so how should i insert that into a database. please help me with the script. and is the whole script correct Thanks.

Replies are listed 'Best First'.
Re: have written a script for inserting data into database but stil face probs...plz help
by kcott (Archbishop) on Oct 22, 2010 at 18:59 UTC
    "... i have to designate the numeric values as reported value and reference range so how should i insert that into a database ..."

    Other than suggesting 'start_range' and 'end_range' columns, I don't see what the problem is here. Perhaps you could expand on what you're actually having difficulty with.

    "... and is the whole script correct ..."

    Firstly, Perl will provide much information if you ask it to. Add use strict; and use warnings; to the top of your code. Also add use diagnostics; if you need more verbose output. Running perl -c script_name from the commandline will test the syntax of your code without actually running it.

    Here's a few pointers on what you have presented:

    • /Equine Profile Plus\s*#(.*?)              .*?(Sample.*?)##/: Unless I physically count them, I've no idea how many spaces you're matching here. For readability and maintainability, use Quantifiers as explained in perlre.
    • open (OUT,">>$filename.csv"): I'd suggest opening the file once before you start the while loop.
    • Your multiple "if (match) {do substitution}" lines should probably be: $line =~ s/match/do substitution/ (the perlre link above and perlop have details).
    • Your multilple instances of $para =~ s/\s\s/ /g; probably only need to be a single instance; also take a look at tr/// in the perlop link above.
    • Don't add an ampersand to the front of &loaddata('$filename.csv'); - see perlsub for details.
    • Aligning your code will help readability. perlstyle will give you some pointers on this.

    -- Ken

Re: have written a script for inserting data into database but stil face probs...plz help
by shevek (Beadle) on Oct 23, 2010 at 11:06 UTC
    Are you having specific problems with the numeric values, or is this more of a design question? From a design viewpoint, if I understand your question, I would have a int column for each numeric in my table definition in mysql. I could then use the between sql syntax to do my selects from that table. If you have to keep the range as a single entry into the table, I would guess the column on the table in defined as varchar and the selects will have to do some regex work to pull the data on searches.