in reply to a little problem with sorting my data

It would be helpful to know what exactly you have tried, and how it failed. Just feeding the data as-is line by line to sort groups them by ID (although it doesn't sort them numerically if the ID is of variable width).
  • Comment on Re: a little problem with sorting my data

Replies are listed 'Best First'.
Re^2: a little problem with sorting my data
by aash08 (Initiate) on Jul 27, 2009 at 17:54 UTC
    Ok. here is what makes my output file;
    #!usr/bin/perl $k = 1; $file_name = "QoEWeb_DB_Log.txt"; #An input LOG file which holds infor +mation about different users. open(SW,$file_name) or die "ERROR OPENING IN FILE"; open FILE, ">output.txt" or die "ERROR..unable to write" ; #Will write + the result into an OUTPUT file. while (<SW>) { chomp($eachline); @file_name1 = ("Carlo_Arnold_2.232_Final.txt", "Sohaib_Ahmad_2.225 +_Final.txt"); #Input LOG files; Each file holds informaton about Indi +vidual user. I will be adding about 30 files here. @logarray = split(/:/,$_); # Taking required fields from the first + input file. $field1 = @logarray[2]; $field2 = @logarray[4]; $key1 = @logarray[8]; $field5 = @logarray[6]; $x = scalar(@file_name1); for($j=0;$j<$x;$j++) { open(RW,@file_name1[$j]) or die "ERROR OPENING IN FILE"; while (<RW>) { chomp($eachline); @ff_array = split(/:/,$_); #Taking required fields from the s +econd set of input files. $key2 = @ff_array[0]; $field3 = @ff_array[1]; if( $key1 == $key2) # Finding a match between the two input f +iles { print FILE "$field1:$field2:$key1:$field5:$field3"; #P +rinting the desired result from both batch of input files. } } } } close FILE; close SW or die "Cannot close"; close RW or die "Cannot close";

    Problem is
    - i want the data in output file to be in sequence according to the field $field1
    - let me assure you that all the $field1 is not of variabe length ... its between the range of 2.221 and ends at 2.252 ... which means that 2.XXX .. while the XXX changes.

    - it would be so much better if these lines are in arranged manner in my OUTPUT file. any views ?
      Instead of while (<RW>) you can write for (sort <RW>) and be done.

      But I strongly recommend to use strict; use warnings; and to declare your variables with my.

        Changing the code as you advised; but still the same result: not effect on sorting. Am i missing something here
        #!usr/bin/perl use strict; use warnings; my $file_name; my $field1; my $field2; my $key1; my $key2; my $field5; my $field3; my $k; my $j; my $x; my $eachline = 0; my $eachline1 = 0; my @logarray; my @ff_array; my @file_name1; $k = 1; $file_name = "QoEWeb_DB_Log.txt"; #An input LOG file which holds infor +mation about different users. open(SW,$file_name) or die "ERROR OPENING IN FILE"; open FILE, ">output.txt" or die "ERROR..unable to write" ; #Will write + the result into an OUTPUT file. for (sort <SW>) { chomp($eachline); @file_name1 = ("Carlo_Arnold_2.232_Final.txt", "Sohaib_Ahmad_2.225 +_Final.txt"); #Input LOG files; Each file holds informaton about Indi +vidual user. I will be adding about 30 files here. @logarray = split(/:/,$_); # Taking required fields from the first + input file. $field1 = $logarray[2]; $field2 = $logarray[4]; $key1 = $logarray[8]; $field5 = $logarray[6]; $x = scalar(@file_name1); for($j=0;$j<$x;$j++) { open(RW,$file_name1[$j]) or die "ERROR OPENING IN FILE"; for (sort <RW>) { chomp($eachline1); @ff_array = split(/:/,$_); #Taking required fields from the s +econd set of input files. $key2 = $ff_array[0]; $field3 = $ff_array[1]; if( $key1 == $key2) # Finding a match between the two input f +iles { print FILE "$field1:$field2:$key1:$field5:$field3"; #P +rinting the desired result from both batch of input files. } } } } close FILE; close SW or die "Cannot close"; close RW or die "Cannot close";