in reply to Re: Sorting a hash
in thread Sorting a hash

Here is what I am doing. The file format is one long line of data that is supposed to be broken into records of 434 characters. I need to read all the records into a hash and then be able to sort the hash(so to speak) based on one of the fields. Once the field is sorted I need to be able to print all the fields that are associated with it. Any suggestions would be appreciated. Sorry for it being such a long message, but I figure the more I put the better you all are able to assist me.
$DAT = "/where/my/file/is"; use constant CLAIM_TEMPLATE => 'A8A6A2A2A6A4A30A4A4A8A8A2A11A25A1A8A3A +8A8A8A8A6A8A8A8A1A10A2A20A2A14A6A4A8A16A20A1A2A1A6A1A14A2A2A8A8A2A8A2 +A1A16A8A1A1A1A10A5A20A15A1A1'; use constant CLAIM_KEYS => qw( claim_batch_number claim_number claim_type payment_direct pharmacy_number chain_number pharmacy_name reject_code_1 reject_code_2 rx_number rx_date drug_type_code national_drug_code product_name newrefill_indicator metric_quantity days_supply ingredient_cost_billed ingredient_cost_paid dispensing_fee copay tax total_amount_paid ucr_amount member_birthdate member_sex cardholder_number member_number alternate_card_number patient_relationship physician_number diagnosis_code pdm_system_number pdm_sponsor_number pdm_group_number group_number generic_code mac_number daw_indicator therapeutic_class_code rx_otc_code gpi exception_code override_code period_ending paid_date compound_code batch_date claim_counter mail_order benefit_code awp claim_indicator drug_preference_indicator pricing_indicator drug_manufacturer controlled_substance patient_last_name patient_first_name patient_middle_initial third_party_code ); Main: { &Open_File; my @claim_records = (); while (<CLAIMS>) { push(@claim_records, read_record($_)); } my $sort_by = 'claim_number'; &Sort_File(); &Close_File; } sub Sort_File { my $sort_by = 'claim_number'; foreach $record ( sort { $a->{$sort_by} <=> $b->{$sort_by} || $a{$ +sort_by} cmp $b{$sort_by} } @claim_records ) { print STDOUT $record->{'claim_number'}, "\n"; } } sub read_record { my ($line) = @_; my %claim_record = (); @claim_record{ CLAIM_KEYS } = unpack(CLAIM_TEMPLATE, $line); return \%claim_record; } sub Open_File { open(CLAIMS, $DAT) || die "CANNOT OPEN FILE"; } sub Close_File { close(CLAIMS); }
Update: The claim_number example data includes:
003549
000081
004951
001249
000059
Hope this help.
Prince99

Too Much is never enough...

Replies are listed 'Best First'.
Re: Re: Re: Sorting a hash
by suaveant (Parson) on Apr 11, 2001 at 20:40 UTC
    ahhh. I see, you are trying to sort by a field in claim records... hmmm... can you keep track of which records are numeric? then you could do...
    %numeric = (mac_number => 1, generic_code => 1); foreach $record ( sort { if($numeric($sort_by)) { return $a->{$sort_by +} <=> $b->{$sort_by} } else { return $a{$sort_by} cmp $b{$sort_by} } +} @claim_records ) { print STDOUT $record->{'claim_number'}, "\n"; }
    you may want to move your sort code into a subroutine and just call sort with the sub name instead of direct code, make your code easier to read since the code in the sort is getting long
                    - Ant