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...

In reply to Re: Re: Sorting a hash by Prince99
in thread Sorting a hash by Prince99

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.