CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate: Sorting data: Edit Log



Akotb
Novice

Sep 6, 2016, 1:01 PM


Views: 11468
Sorting data

Hello!
I have a problem with sorting data in my program.
While working on the program, there was a problem with sorting the data in the list. Sort does not start, please tell me how to fix the code. Now the program has a row of data for processing

1000001 77.847
1000004 79.914
1234568 89.656
1000001 212.843
1234568 225.265
1000004 234.535

where the first column is represented by an identifier, in the second column of the time, I need to subtract from the current value of the previous time, and write it in the column of the number of times how many of these records in the input data. And then starts sorting, whose time is less tolerated by the higher on the list and the second sorting value, who records on crossing over, the higher on the list is transferred. The output would like to get around it

1. 1000001 134.996
2. 1234568 135.609
3. 1000004 155.621

Now the program looks like this, please tell me how to bring code to mind.


Code
#!perl 

use strict;
use warnings;

open(my $in, "<:encoding(UTF-16)", "INPUT.TXT") || die("Error open INPUT.TXT\n");

# hash sportmans names
my %sportsmans = (
'1000001' => 'First',
'1000004' => 'Second',
'1234568' => 'Third');
my %sportsmans ;
open(my $Nam, "<:encoding(cp1252)", "NAMES.txt") || die("Error open NAMES.TXT\n");
while (my $line = <$Nam>) {
$line =~ /(\d+).(.+?) \s* $/x;
my $id = $1;
my $name = $2;
$sportsmans{$id} = $name;
}
close ($Nam);


# hash results
my %rezults;

while(my $instr = <$in>) {
if ($instr =~ /\@/) {
my $str = $instr;
chop $str; chop $str;
$str =~ m/@\s+(\S.*\S\t?)$/;
my $temp_str = $1;

for (0..1) {
$str = <$in>;
chop $str; chop $str;
$str =~ m/\] (.*)$/;
$temp_str .= $1;
}

$temp_str =~ m/(\d+)\t(\d+\.\d+)/;
unless (exists($rezults{$1})) {
$rezults{$1} = []; # If earlier the pilot did not meet - create a reference to an empty array if the value of the cache pilots with key $ 1
}
push @{$rezults{$1}}, $2; # add the result to the end of the array
}
}

open(my $out, ">", "OUTPUT1.TXT") || die("Error open OUTPUT1.TXT\n");
# reporting results
foreach my $nomer_fio (sort(keys(%sportsmans))){
showResult(\%sportsmans, \%rezults, $nomer_fio);
}
close($out);
close($in);

# function display the results in a particular pilot
sub showResult {
my ($names, $rez, $name) = @_;
my $previousTime = 0; # previous lap time
my $ring = 1; # lap number
print $out "results of the pilot $names->{$name}\n\n";
print $out " lap\current time\lap time\n";


The program displays the results on a specific pilot. And I need to see summarioy result for all pilots
1. 1000001 134.996
2. 1234568 135.609
3. 1000004 155.621
, this requires sorting run. To do this, I change the output process.

Code
sub showReport { 
my ($names, $rez, $name) = @_;
my $previousTime = 0; # previous lap time
my $ring = 1; # lap number
# print $out "results of the pilot $names->{$name}\n\n";
print $out " place | lap |\lapp time\n";


but the code is not running. Please tell me how to run this sort of data.


(This post was edited by Akotb on Sep 12, 2016, 12:17 PM)


Edit Log:
Post edited by Akotb (Novice) on Sep 6, 2016, 1:04 PM
Post edited by Akotb (Novice) on Sep 12, 2016, 12:17 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives