473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Advanced Sorting in Perl

Hello.
I have a hash which I need sorting. Each record in the hash consists of a string with numbers separated by commas. For example ('2.1, 4.3, 2.5,...3.1').
I wish to sort my hash by the sum of the values. I wrote a function, sum_fields, to receive the string and return the total the values. However I was hoping to reference the subroutine / function like this:

for $key (sort { sum_fields{$csv{$b}} <=> sum_fields{$csv{$a}} } keys %csv)

However, Perl doesn't like this. I get:
Bareword found where operator expected at D:\Mydata\sandpit\awrcsv.pl line 548,
near "<=> sum_fields"
(Missing operator before sum_fields?)
Any ideas how best to achieve this? I am being lazy and trying to avoid loading my hash into a more complex hash structure which I could readily sort.
Thanks,
Clive
Jun 23 '11 #1
1 2114
miller
1,089 Expert 1GB
You called your function, sum_fields using braces {} instead of parenthesis (). That's the cause of your error.

I personally would use the List::Util core library and a Schwartzian Transform:

Expand|Select|Wrap|Line Numbers
  1. use List::Util qw(sum);
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. my %csv = (
  7.     '2.1, 4.3, 2.5, 3.1' => 1,
  8.     '2.1, 4.3, 2.5, 3.3' => 1,
  9.     '2.1, 4.3, 2.5, 3.2' => 1,
  10. );
  11.  
  12. for my $key (map {$_->[0]} sort {$b->[1] <=> $a->[1]} map {[$_, sum split ',', $_]} keys %csv) {
  13.     print "$key\n";
  14. }
  15.  
- Miller
Jun 23 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ben moretti | last post by:
hi i'm learning python, and one area i'd use it for is data management in scientific computing. in the case i've tried i want to reformat a data file from a normalised list to a matrix with some...
2
by: Xah Lee | last post by:
Today we'll write a program that can sort a matrix in all possible ways. Here's the Perl documentation. I'll post a Perl and Python version in 2 days. ----------- sort_matrix( $matrix, , ,...
2
by: Asteras | last post by:
Hi there, If we have a student score table with the names of 5 students and their marks in two exams: +--------+-------+-------+ | Name | Mark1 | Mark2 | +--------+-------+-------+ | John...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
7
by: Peter Kirk | last post by:
Hi say I have a list of Person objects, which I want to sort. But I don't just want to sort by "name" say, but by several fields in the Person object. Maybe 1st priority by height, 2nd priority...
5
KevinADC
by: KevinADC | last post by:
Introduction This discussion of the sort function is targeted at beginners to perl coding. More experienced perl coders will find nothing new or useful. Sorting lists or arrays is a very common...
1
KevinADC
by: KevinADC | last post by:
Introduction In part one we discussed the default sort function. In part two we will discuss more advanced techniques you can use to sort data. Some of the techniques might introduce unfamiliar...
6
by: gopalsd | last post by:
Hi Friends, Can anyone pls help me to resolve my school test in PERL................ as follows..... #!/usr/bin/perl @data=N; $sum=0; print"enter the required No. of numbers to be inputed...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
9
by: grocery_stocker | last post by:
How are references and aliases in perl different than references in aliases in C++?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.