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
1 2114
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: -
use List::Util qw(sum);
-
-
use strict;
-
use warnings;
-
-
my %csv = (
-
'2.1, 4.3, 2.5, 3.1' => 1,
-
'2.1, 4.3, 2.5, 3.3' => 1,
-
'2.1, 4.3, 2.5, 3.2' => 1,
-
);
-
-
for my $key (map {$_->[0]} sort {$b->[1] <=> $a->[1]} map {[$_, sum split ',', $_]} keys %csv) {
-
print "$key\n";
-
}
-
- Miller
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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, , ,...
|
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...
|
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:
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: grocery_stocker |
last post by:
How are references and aliases in perl different than references in
aliases in C++?
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |