#! /usr/bin/perl use DBI; use CGI qw(:standard); my ($dsn) = "DBI:mysql:username:yourserver.com"; # data source name my ($user_name) = "myusername"; # user name my ($password) = "mypassword"; # password my ($dbh, $sth); # database and statement handles my (@ary); # array for rows returned by query #create new CGI object $q = new CGI; if( $q->param() ){ &getValues; &processPage; &sendPage; }else{ &myForm; } exit (0); sub myForm { print "Content-type: text/html\n\n"; print< Enter Medline Record EOM print start_form, "Paste in the record you'd like to parse:",p, textarea(-name=>'med_text',-cols=>'80', -rows=>'14'),"
\n", p, submit, end_form, hr; } sub getValues { $med_text = $q->param('med_text'); } sub processPage { print header; # print "
\n"; #for debugging

    # take care of line endings
    $med_text =~ s/\n\r|\r\n/\n/g;
#    $med_text =~ s/\r\n/\n/g;

    @line_list = split(/\n|\r/, $med_text);
    $nlines = @line_list;
    $i=0;
    while($line = shift(@line_list)){
	if($line =~ /^\d/){ #got begining of record
            #find first letter to set indentation
	    $spaces = 0;
	    $index = 0;
            while( substr($line,$index,1) !~ /[a-z]/i ){
               ++$spaces;
               ++$index;
             }
	    $line =~ s/^\d+\. //;
	    $authors = $line;
            # get next line
	    $line = shift(@line_list);
	    while($line =~ /^ {$spaces}\w/){
	      $line =~ s/^ +/ /; #take care of leading space
	      $authors .= $line;
	      $line = shift(@line_list);
	    }
	    &space_Shave($line, $spaces, $index);
	    $title = $line;
	    $line = shift(@line_list);
	    while($line =~ /^ {$spaces}\w/){
	      $line =~ s/^ +/ /; #take care of leading space
	      $title .= $line;
	      $line = shift(@line_list);
	    }
	    $title =~ s/^ +//;
	    &space_Shave($line, $spaces, $index);
	    $journal = $line;
	    $line = shift(@line_list);
	    while($line =~ /^ {$spaces}\w/){
	      $line =~ s/^ +/ /; #take care of leading space
	      $journal .= $line;
	      $line = shift(@line_list);
	    }
	    $journal =~ s/^ +//;
            while($line !~ /\(UI: *(\d+)/){
	      $line = shift(@line_list);
            }
            $line =~ /\(UI: *(\d+)/;
	    $med_UI = $1;
            #now we're looking for the abstract
	    while($line !~ /^Abstract:/){
	      $line = shift(@line_list);
	    }
	    $abstract = $line."\n";
	    $abstract =~ s/^Abstract: //;
	    $line = shift(@line_list);
	    &space_Shave($line, $spaces, $index);
	    while($line =~ /^ {$spaces}[\w\(\)]/){
#	      $line =~ s/^ +/ /; #take care of leading space
	      $abstract .= "$line\n";
	      $line = shift(@line_list);
	    }
	    $abstract =~ s/^ +//;
	    $abstract =~ s/"/\\"/g; #take care of quotes in string
	    $authors[$i] = $authors;
	    $title[$i] = $title;
	    $journal[$i] = $journal;
	    $abstract[$i] = $abstract;
	    $med_UI[$i] = $med_UI;

	    @j_list = split(/,/,$journal[$i]);
	    $j_list[0] =~ s/^ +//; #take care of leading space
	    $periodical[$i] = $j_list[0];
	    $j_list[1] =~ s/^ +//; #take care of leading space
	    $date[$i] = $j_list[1];
	    $j_list[2] =~ s/^ +//; #take care of leading space
	    $issue[$i] = $j_list[2];

	    ++$i;

	    $templist = split(/,/, $journal);

	    $authors = "";
	    $title = "";
	    $journal = "";
	    $abstract = "";
	    $med_UI = "";
	}
    }

}
sub sendPage {


    print "Edit Submitted\n";
    print "

Thanks!

\n"; print " Submit More

";

    print <connect ($dsn, $user_name, $password, { RaiseError => 1 });

    for($j=0; $j<$i; ++$j){
	&insert_Data;

    print "
"; print "Thanks for the submission. "; print "Edit this record | Submit Citations | Search \n"; } print ""; } sub insert_Data { print "looking for citation:\n"; print "Title: $title[$j]\n\n"; print "Authors: $authors[$j]\n\n"; print "Journal: $journal[$j]\n\n"; print "Abstract: $abstract[$j]\n\n"; print "Journal Title: $periodical[$j]\n"; print "Date: $date[$j]\n"; print "Issue: $issue[$j]\n"; print "UI: $med_UI[$j]\n"; print "-----------------------------------------\n"; $query = qq{ SELECT * FROM cwslib WHERE title = "$title[$j]" }; print $query, "\n"; $sth = $dbh->prepare ($query); $sth->execute (); @ary = $sth->fetchrow_array(); if(@ary == 0){ #query didn't find anything print "Record not found\n"; &insert_record; }else{ #found record print "Found something for:\n"; $sth->finish (); } $sth->finish (); } sub insert_record { $insert = qq{INSERT INTO cwslib ( title, author, abstract, journal, date, issue, med_UI ) VALUES( "$title[$j]", "$authors[$j]", "$abstract[$j]", "$journal[$j]", "$date[$j]", "$issue[$j]", $med_UI[$j] ) }; print "Updating with:\n$insert\n"; #execute the insert statement $rows = $dbh->do($insert); if(!$rows){ print "Unable to insert record.\n"; }else{ printf("%d rows affected.\n", $rows); } } sub space_Shave { #finds how many spaces are at the front of a line #takes variables $line, $spaces, $index #count spaces $spaces = 0; $index = 0; while( substr($line,$index,1) eq " " ){ ++$spaces; ++$index; } }