in reply to Data Structure advice

One question regarding your post is what were you thinking of for the hash key?
In any case, here is a proposal for a solution to your problem. I'll leave initializing the structure up to you. I propose just storing the data in an array of arrayrefs. Then, sorting could go something like this:
... # assume the @data is your array of arrayrefs. # get the data sorted on field 2 (zero based) @data = numeric_sorter(2, @data) sub numeric_sorter { my ($field_num, @data) = @_; my @sorted_items = sort {$a->[$field_num] <=> $b->[$field_num]} @d +ata; return @sorted_items; }
If you wanted an alphabetic sorter, replace "<=>" with "cmp"
I chose to use an array of arrayrefs for memory efficiency, but it could be an array of hashrefs if you wanted to reference the columns by name instead of field number:
@data = numeric_sorter(2, @data) sub alpha_sorter { my ($field_name, @data) = @_; my @sorted_items = sort {$a->{$field_name} <=> $b->{$field_name}} +@data; return @sorted_items; }

Replies are listed 'Best First'.
Re: Re: Data Structure advice
by waswas-fng (Curate) on Nov 22, 2002 at 23:16 UTC
    I think we both misread his question -- here is what I missed: Unique items are only line per line, and he wants to sort on one coulumn and then if a "tie" happens break it with second sort on another coulmn.

    I am trying to think of a good way to do this.
    Edited this should work as long as both are the same type:
    @data = numeric_sorter(2,4, @data) sub numeric_sorter { my ($field_num, $field_num_secondary, @data) = @_; my @sorted_items = sort { if ($a->[$field_num] != $b->[$field_num]) { $a->[$field_num] <=> $b->[$field_num] } else { $a->[$field_num_secondary] <=> $b->[$field_num_secondary] }} @data; return @sorted_items; }


    -Waswas
      using me above array of arrayrefs implementation, if you want to sort on column 3 and break ties with column 1, do this:
      @data = sort { $a->[3] <=> $b->[1] || $a->[1] <=> $b->[1] } @data;
      that can be extended indefinitely with or "||" clauses, but to completely generalize this is much harder.
      actually the first column is a timestamp, and the third column is a hex number

        Is your only purpose to sort the data? Are you intending to write it back to a file sorted? What else are you intending to do with the data within your perl script?

        Half a dozens lines of the data would make things a lot easir to answer with authority.


        Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
        Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
        Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
        Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.