Link to home
Start Free TrialLog in
Avatar of haast
haast

asked on

Sorting & displaying a file.

The following code snppet opens a flat file with a list of 4 pipe delimited fields terminated with a new line and displays them in sequential order.

QUESTION:

How would I display the file contents say, SORTED on field 1 ($f1) and NOT sequentially ????

I would like a working code example.
Thanks, In advance.

#!/usr/bin/perl

$dbase = "/home/blah/dbase.txt";

print "Content-type: text/html\n\n";
print "<html><head><title></title></head>\n";
print "<body bgcolor=#FFFFFF\n";
print "<br>\n";

open(FILE,"$dbase") || die "Cannot Open Database File: $!\n";

&dall;

print "<br></body></html>\n";
close(FILE);
exit;

# Display all records
sub dall {
 while(<FILE>) {
 chop;
 @all = split(/\n/);
  foreach $line(@all) {
   local($f1,$f2,$f3,$f4)=split (/|/,$line);
   &display;
  }
 }
}

# Display results
sub display {
print "$f1, $f2, $f3, $f4\n";
print "<br>\n";
}

ASKER CERTIFIED SOLUTION
Avatar of maneshr
maneshr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of haast
haast

ASKER

Hang on, I will try it.....
Avatar of haast

ASKER

Maneshr, that is excellent, I want to award you the points for this.

I am a newbie and I really appreciate your help, I would like to ask you ONE more question, and either add 100 more points to this or ask a NEW one (please advise !!!!).

It is a variation on my original one.

#!/usr/bin/perl

$dbase = "/home/blah/dbase.txt";

# Parse Form
read(STDIN, $input, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $input);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

# Translate Hash References
$find  = $FORM{'term'};

print "Content-type: text/html\n\n";
print "<html><head><title></title></head>\n";
print "<body bgcolor=#FFFFFF\n";
print "<br>\n";

open(FILE,"$dbase") || die "Cannot Open Database File: $!\n";

&search;

print "<br></body></html>\n";
close(FILE);
exit;

# Search all records for matches
sub search {
$match = 0;
 while(<FILE>) {
 chop;
 @all = split(/\n/);
  foreach $line(@all) {
   local($f1,$f2,$f3,$f4)=split (/|/,$line);

    if($find eq $f1){
      &display;
    }
  }
 }
}

# Display matched results
sub display {
print "$f1, $f2, $f3, $f4\n";
print "<br>\n";
}

-------------------
MY NEW QUESTION IS:

How would I sort the results of the matched records ($find eq $f1) , and display them say, in ascending alfa sorted order based on field $f2.

Thanks !!!!






Avatar of haast

ASKER

Adjusted points from 100 to 200
Avatar of haast

ASKER

Maneshr, even if you do not answer the second part of my question, please collect your points. Your help is much appreciated.
Avatar of haast

ASKER

Maneshr, I have figured my second question out, PLEASE collect your points.

Thanks !!