in reply to Complex hash sorting

This is just an idea, so don't take my word for it..you may want to wait to see what other monks have to say. I've stumbed across this problem a few times (never had the initiative to find a solution though).

You COULD try sorting by the KEY of the hash (for the first value of age), then rebuild your hash so the value you are wanting to split on is first. An example, if you wanted to search for their zip code, just SPLIT your hash and JOIN it again with the zip code being the first thing before your delimiter: value = 12345||39||Somehere in Cali||Cali.

This is untested and just an idea, but hope it helps.



"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
Re: Re: Complex hash sorting
by BigLug (Chaplain) on Feb 01, 2004 at 06:32 UTC
    sulfericacid,
    I tried something similar once. I reordered the data and then just sorted the string.

    However, the problem is that you have to sort ASCII which means that 999 is greater than 1000. The next thing I did was to sprintf my data so that I was comparing 0999 to 1000. That worked. That didn't really sit well though.

    It seems to be a lot of mess. Then I discovered LoLs and HoLs and other complex data types. They make it much easier so I'd go with any of the methods below.

    Personally I'd store the data as a LoH, but that's just me.

    my @data = ( { name => 'Bill Nye', age => 39, town => 'Somewhere in Cali', state => 'Cali', zip => '12345' }, { name => 'Homer Simpson', age => 36, town => 'Springfield', state => undef, zip => '23456' }, { name => 'Barney Rubble', age => 31, town => 'Bedrock', state => 'Cartoon Location', zip => '3456' }, ); print Dumper( sort { $a->{name} cmp $b->{name} } @data );
    Other monks will probably tell you that the above is not a good thing because you're storing your hash keys over and over again. However I think it makes it much easier to track your data. It would, of course, be possible to create your own data structure parsing module that had objects and methods for creating, editing and deleting data but there's a ton already out there.