LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2009, 08:15 PM   #1
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
[Perl] Sort a file by column


I have a file 'priority' storing data with two fields:

For example:
Quote:

link_one 3
link_two 5
I am trying to sort that by the second column, my program is as below:

Quote:
#!/usr/bin/perl

use warnings;

$priority_db = "priority";
@data;

open(DATA, "<$priority_db") or die "$!\n";


while(<DATA>)
{
chomp;
push @data, [split, ' ' ,$_];
}

@sorted = sort {$a->[1] <=> $b->[1]} @data;

foreach $item (@sorted)
{
print "$item\n";
}

but it print out some weird results:

ARRAY(0x8154678)
ARRAY(0x818f3a0)
ARRAY(0x818f358)
ARRAY(0x818f310)
ARRAY(0x815460c)


Could anyone figure out the problem of my program ?


Thanks,

-Kun
 
Old 04-23-2009, 09:05 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Kunsheng View Post
I have a file 'priority' storing data with two fields:

For example:


I am trying to sort that by the second column, my program is as below:




but it print out some weird results:

ARRAY(0x8154678)
ARRAY(0x818f3a0)
ARRAY(0x818f358)
ARRAY(0x818f310)
ARRAY(0x815460c)


Could anyone figure out the problem of my program ?


Thanks,

-Kun
Please use CODE tags (see '#' on top of the composition window).
Always use

Code:
use strict;
use warnings; # this is already used

Anyway, what's the purpose of

Code:
push @data, [split, ' ' ,$_];
statement ? Specifically, what type of value will be returned by [...] part ?

Then, remembering that 'sort' works on $a, $b, what do you expect it to do ?

Likewise, what is the type of $item in

Code:
print "$item\n";
?

I know how to resolve all these issues, but the point is to teach you to fish, not to bring you fish.
 
Old 04-24-2009, 08:40 AM   #3
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 60
In a nutshell, you want to create a hash where the keys are the link names (assuming that the link names will be unique) and the values will be the numbers. Then you want to sort the hash by the values, not the keys. Here's a version:

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

my %link_strengths;

while (<>) {
  chomp;
  my ($link, $strength) = split /\s+/;
  $link_strengths{$link} = $strength;
}

my @sorted = sort {
  $link_strengths{$a} <=> $link_strengths{$b}
} keys %link_strengths;

for my $link (@sorted) {
  print "$link: $link_strengths{$link}\n";
}
Save that as 'sort_em' and then run it with perl sort_em <filename>. (Replace <filename> with the name of the file that stores the links and numbers.)

Edit: by using [] around your split command, you're creating a reference to an anonymous array and then (using push) placing those into your @data array. This is not what you want. You are then trying to print the array reference without dereferencing it. That's why you're getting that ARRAY + memory location item (which appears either by mistake or as a debugging aid, in my experience).

Last edited by Telemachos; 04-24-2009 at 08:46 AM.
 
Old 04-24-2009, 09:07 AM   #4
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Original Poster
Rep: Reputation: 16
Thanks, sergei! I tried it myself on hash way and it works like a charm.

The code is as below:


Code:
#!/usr/bin/perl

use warnings;

$priority_db = "priority";


%data=();

sub Ascend {
  
  $data{$a} <=> $data{$b};
}

sub Descend {
  
  $data{$b} <=> $data{$a};

}

open(DATA, "<$priority_db") or die "$!\n";


while(<DATA>)
{
  chomp;
  ($src,$priority) = split(" ", $_);

  $data{$src} = $priority;
}


foreach $link (sort Descend (keys(%data)))
{
  print "$link\n";
}
Hope it could help someone later.
 
Old 04-24-2009, 09:09 AM   #5
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Original Poster
Rep: Reputation: 16
Thanks for that \s+\ regular expression, I have been looking for it a long time.

Quote:
Originally Posted by Telemachos View Post
In a nutshell, you want to create a hash where the keys are the link names (assuming that the link names will be unique) and the values will be the numbers. Then you want to sort the hash by the values, not the keys. Here's a version:

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

my %link_strengths;

while (<>) {
  chomp;
  my ($link, $strength) = split /\s+/;
  $link_strengths{$link} = $strength;
}

my @sorted = sort {
  $link_strengths{$a} <=> $link_strengths{$b}
} keys %link_strengths;

for my $link (@sorted) {
  print "$link: $link_strengths{$link}\n";
}
Save that as 'sort_em' and then run it with perl sort_em <filename>. (Replace <filename> with the name of the file that stores the links and numbers.)

Edit: by using [] around your split command, you're creating a reference to an anonymous array and then (using push) placing those into your @data array. This is not what you want. You are then trying to print the array reference without dereferencing it. That's why you're getting that ARRAY + memory location item (which appears either by mistake or as a debugging aid, in my experience).
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Unable to sort by column in KDE 4.2.2 bassmadrigal Slackware 5 04-18-2009 05:22 AM
sort on a single column? baidym Programming 3 01-03-2009 08:46 AM
How do I do filtering in Perl (keep sort order and sort again by another means)? RavenLX Programming 9 12-19-2008 10:12 AM
Parsing rows and column data from a file using perl dav_y2k Programming 1 10-08-2006 11:57 AM
Can't get "sort' to work on a particular column smkamene Programming 11 06-30-2006 10:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration