Click here to get back home

sorting an array of hashrefs

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
sorting an array of hashrefs monkeys paw 08-29-2006
Get Chitika Premium
Posted by monkeys paw on August 29, 2006, 4:29 pm
Please log in for more thread options
>>
>> [snip]
>>
>> > if I print "$1\n",
>> > the file prints just fine. But, if I do something like print "$1 after
>> > \n", the whole output is messed up. If I print "before $1\n", nothing
>> > prints at all. If I print "before $1 after\n", only after prints.
>>
>> not really sure, but could be a rogue "\r" in $1,


> There
> is a rogue carriage return (0xd) in the string

> Is there something I can do to deal with this
> situation?


Repair the corrupted file:

perl -p -i -e 'tr/\r//d' bad_file


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas

Posted by John W. Krahn on August 29, 2006, 3:48 pm
Please log in for more thread options
monkeys paw wrote:
> The following is intended to sort an array of hash refs based on
> id_type. It is not working however. Does anyone know why?
>
> use Data::Dumper;
>
> our %datatype_sort = (
> bill => 0,
> reg => 10,
> );
>
>
> my @ary = (
> {id_type => 'reg', id => 'KY200616813'},
> {id_type => 'bill', id => 'CA2005000A5'},
> {id_type => 'bill', id => 'CA2005000A250'},
> );
>
> my @x = sort _sortval_datatype @ary;
>
> print Dumper(\@x);
>
> sub _sortval_datatype {
> my($av, $bv) = @_;
> return $datatype_sort} <=>
> $datatype_sort};
> }


sub _sortval_datatype {
return $datatype_sort} <=> $datatype_sort};
}



John
--
use Perl;
program
fulfillment

Posted by A. Sinan Unur on August 29, 2006, 3:50 pm
Please log in for more thread options

> The following is intended to sort an array of hash refs based on
> id_type. It is not working however. Does anyone know why?
>

use strict;
use warnings;

missing.

> use Data::Dumper;
>
> our %datatype_sort = (
> bill => 0,
> reg => 10,
> );
>
>
> my @ary = (
> {id_type => 'reg', id => 'KY200616813'},
> {id_type => 'bill', id => 'CA2005000A5'},
> {id_type => 'bill', id => 'CA2005000A250'},
> );
>
> my @x = sort _sortval_datatype @ary;
>
> print Dumper(\@x);
>
> sub _sortval_datatype {
> my($av, $bv) = @_;
> return $datatype_sort} <=>
> $datatype_sort};
> }

The elements to be compared are not passed to _sortval_datatype in
subroutine arguments, but rather in the package variables $a and $b.

my @x = sort { $datatype_sort}
<=>
$datatype_sort}
} @ary;

Do read perldoc -f sort.

Incidentally, you could just use cmp:

my @x = sort { $a-> cmp $b-> } @ary;

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


Similar ThreadsPosted
iterating over a hashref of hashrefs June 7, 2005, 3:21 pm
Sorting an Array by Another January 21, 2005, 12:56 pm
Perl sum of array and help with sorting August 20, 2007, 3:44 pm
Confused by sorting array elements August 4, 2004, 5:39 am
Sorting Array of String Backward October 13, 2004, 9:32 am
Sorting a Multi-dimenional Array February 11, 2005, 2:55 pm
sorting array reference of hashes October 4, 2006, 1:50 pm
Sorting array of hash references October 26, 2006, 6:21 am
sorting array in multiple columns November 8, 2006, 4:51 pm
Sorting a nested array (table) (alphabetically) September 29, 2005, 2:44 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap