Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

open and read file using cgi perl

by vivek.vivek (Initiate)
on Sep 29, 2014 at 15:29 UTC ( [id://1102363]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I have 2 files as below

a.txt: LOCK xxx yyy 123 1.1 fff LOCK nnj hhh 789 2.2 uuu
b.txt: LOCK xxx yyy 567 1.1 fff LOCK nnj hhh 000 2.2 uuu LOCK YUI hhh 520 2.3 ujk

I am comparing both files by browsing it through a webpage and parsing the file. I need to compare the lines if only it starts with word LOCK and output the data. The code is doing the comparison, but my problem is that the output is repeated with the count of number of lines in each file.

Eg: if a is compared with b, output repeted twice. if b is compared with a, output is repeated 3 times.

my @files = ($file,$file1); open(INOUT1,"<", $file1) or die "cant open output file "; + while ($linea = <INOUT1>){ open(INOUT,"<", $file) or die "cant open output file"; while ($line = <INOUT>){ chomp $linea; next unless $linea =~ m/^LOCK /; chomp $line; next unless $line =~ m/^LOCK /; foreach $file_n (@files){ $old = $files[0]; $new = $files[1]; if ($old eq $file_n){ @words=split(/\s+/,$line); $feature1[$i]=$words[1]; $ver1[$i]=$words[3]; $exp1[$i]=$words[4]; $no1[$i]=$words[5]; $i++; } else{ @words=split(/\s+/,$linea); $feature2[$ii]=$words[1]; $ver2[$ii]=$words[3]; $exp2[$ii]=$words[4]; $no2[$ii]=$words[5]; $ii++; } } }close INOUT; }close INOUT1; print "<table border =1 align='left' style='font-family:Georgi +a;'> for ($b=0;$b<@feature1;$b++) { $test=0; for($a=0;$a<@feature2;$a++) { if($feature1[$b] ne $feature2[$a]) { $test++; } if($test == @feature2) { print "<tr><td>$feature1[$b]</td><td>$ +ver1[$b]</td><td>$exp1[$b]</td><td>$no1[$b]</td></tr>"; #$test++; } } } print "</table>"; print "<table border=1 align='left' style='font-family:Georgia +;'> for ($b=0;$b<@feature2;$b++) { $test=0; for($a=0;$a<@feature1;$a++) { if($feature2[$b] ne $feature1[$a]) { $test++; } if($test == @feature1) { #print $test; print "<tr><td>$feature2[$b]</td><td>$ver2 +[$b]</td><td>$exp2[$b]</td><td>$no2[$b]</td></tr>"; } } } print "</table>";}

Replies are listed 'Best First'.
Re: open and read file using cgi perl
by Laurent_R (Canon) on Sep 29, 2014 at 19:08 UTC
    Given the simplicity of what you want to compare, your code seems to me to be far too complicated. I don't feel like dwelling into your code, but, setting aside the HTML output, I feel the whole comparison part could probably be done in less than 20 lines of code. But you don't really state what you want to do.

    An important question: how large are your files? Reading the complete second file for each line of input in the first file is most probably extremely inefficient. One of the most common way to do this type of thing is to store one of the files (often the smallest one, but not necessarily) in an appropriate data structure, often a hash or something similar, so that you can then read the other file line by line and access instantly to the data you need for the comparison. But this is not possible if your files are really huge and will exhaust memory.

    If the files are really too large for that (i.e. the file won't fit into memory), there is still another possibility: sort both files according to the comparison key, and read them in parallel.

    But don't read one file for each input line of the other. This is extremely inefficient.

Re: open and read file using cgi perl
by pme (Monsignor) on Sep 29, 2014 at 18:49 UTC
    Could you specify exactly what should be compared? And please also provide the required output of your sample input files.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102363]
Approved by Corion
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-20 01:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found