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: Beginner:
Sorting Question

 



adamjazz1
Novice

Dec 3, 2009, 12:20 PM

Post #1 of 5 (943 views)
Sorting Question Can't Post

Hi everyone,

I have a quick question on sorting.


Code
/yellow/3/sim/t2.pdf 1.299149 
/yellow/5/sim/t3.pdf 0.522122

I would like to sort this data ascending numerically.

So far I have this:
my @data= split(/\d\.\d+);
@srt= sort { $a <=> $b } @data
I would like to know where the file is in relation to the sorting.

Thanks for any input!


7stud
Enthusiast

Dec 3, 2009, 12:32 PM

Post #2 of 5 (942 views)
Re: [adamjazz1] Sorting Question [In reply to] Can't Post


Code
my @data= split(/\d\.\d+);

Huh? Your post is incomprehensible.

You can do something like this:


Code
use strict; 
use warnings;
use 5.010;

my %hash = (
1.299149 => '/yellow/3/sim/t2.pdf',
0.522122 => '/yellow/5/sim/t3.pdf'
);

my @sorted_keys = sort {$a <=> $b} keys %hash;

for (@sorted_keys) {
say "$_ => $hash{$_}";
}

--output:--
0.522122 => /yellow/5/sim/t3.pdf
1.299149 => /yellow/3/sim/t2.pdf



(This post was edited by 7stud on Dec 3, 2009, 12:38 PM)


shawnhcorey
Enthusiast


Dec 4, 2009, 7:38 AM

Post #3 of 5 (926 views)
Re: [adamjazz1] Sorting Question [In reply to] Can't Post

You could use the Schwartzian transform:

Code
#!/usr/bin/env perl 

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;

my @data = <DATA>;
print '@data : ', Dumper \@data;

my @sorted = map { $_->[0] }
sort { $a->[2] <=> $b->[2] || $a->[0] cmp $b->[0] }
map { [ $_, split ] }
@data;
print '@sorted : ', Dumper \@sorted;

__DATA__
/yellow/3/sim/t2.pdf 1.299149
/yellow/5/sim/t3.pdf 0.522122


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


adamjazz1
Novice

Dec 10, 2009, 1:02 PM

Post #4 of 5 (898 views)
Re: [7stud] Sorting Question [In reply to] Can't Post

Hi, Sorry about my post. I was in a hurry.

Here is my script that I have been working on. I have made some changes, and I almost have it figured out...


This is what the data looks like.
output from showcluster.pl

Code
 t.28           385     0 
t.2 395 0
t.20 620 0
t.12 685 0
t.7 2820 0
t 9501 49

output from ensfiles.pl

Code
/s3/nh/nh2/1/85/nh2ct2.x 1.716882 
/s3/nh/nh2/12/92/nh2ct2.x 1.881869

What I want to do is take the first column "t.###" sorted sorted by the 2nd column. I only need no more than 9.

From this t.## I pass this to ensfiles.pl and get "/s3/nh/nh2/1/85/nh2ct2.x 1.716882" I want to make a hash of this data and sort it ascending by the 2nd column of data (the number).

I only need the top 3 from the sorted hash. Ideally I would like to have the printout be

Code
"t.## -> /s3/nh/nh2/1/85/nh2ct2.x (without this number though 1.716882)" 
"t.## -> /s3/nh/nh2/12/92/nh2ct2.x (w/o this number 1.881869)"



Code
#!/usr/bin/perl 
use strict;
use warnings;

my $n="nh.ens";
my $tag="nh";
my $dir="/orange/nh/";

my $qq;
my @ww=();
my @data=();
my $d;
my @f;
my $dat;
my $key;
my %newhash;

open INP, "showcluster.pl -dir $dir/$n $tag | sort -k2n | tail |";
while (<INP>) {
chomp;
#print $_,"\n";
$qq=substr($_,2,4 );
#$qq=~/^t\./g;
$qq=~s/\s$//g;
push(@ww, $qq);

}
close INP;

for (my $i=0; $i <=8; $i++) {
open INP, "ensfiles.pl -cluster $ww[$i] -dir $dir/$n/ $tag | sed q | ";
while(<INP>) {
chomp;
@f=split(/\s/);
$dat={};
$dat->{fname}=$f[0];
$dat->{val}=$f[1];
push(@data, $dat);
}
close INP;

open OUT, "| awk '{print $1}' ";
for $d ( @data) {
$newhash{$d->{val}}=$d->{fname};
}
foreach $key (sort (keys %newhash)) {
printf OUT "%s -> %s\n", $ww[$i],$newhash{$key};
}
close OUT;
}


Thanks everyone for your help!


johnpc831
Novice

Dec 10, 2009, 3:16 PM

Post #5 of 5 (892 views)
Re: [adamjazz1] Sorting Question [In reply to] Can't Post

I get the sorting question. I'm not following the rest of it.

If you have your data in a file called data.txt, and it looks like this:


Code
t.28           385     0 
t.2 395 0
t.20 620 0
t.12 685 0
t.7 2820 0


Then you can get an array of the first column values sorted by the second column values like this:


Code
open(FH,"data.txt"); 
my @lines = <FH>;
close(FH);

my @sorted_first_column = map { $_->[0] }
sort { $a->[1] <=> $b->[1] }
map {[first_column($_),second_column($_)]} @lines;

sub first_column {
my $data_value = shift;

$data_value =~ /^(t\.\d+)/;
return $1;
}

sub second_column {
my $data_value = shift;

$data_value =~ /^t.\d+\s+(\d+)/;

return $1;
}


It looks like you only need the top three values, which you can access using their specific index values.

 
 


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

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