sorting by prior value in a deeply nested hash

Discussion in 'Perl Misc' started by ccc31807, Nov 6, 2013.

  1. ccc31807

    ccc31807 Guest

    I have a reference to a hash defined like this:
    $chpr->{$college}{$dept}{$rank}{$fac_id}{$subj} = {
    ...,
    sort_name = $sort_name,
    ...};

    I print an Excel report like this:
    #open Excel workbook for COLLEGE
    foreach my $college (sort keys %{$chpr}) {
    foreach my $dept (sort keys %{$chpr->{$college}}) {
    foreach my $rank (sort keys %{$chpr->{$college}{$dept}}) {
    #open new worksheet for DEPARTMENT
    foreach my $fac_id (sort keys %{$chpr->{$college}{$dept}{$rank}}) {
    foreach my $subj (sort keys %{$chpr->{$college}{$dept}{$rank}{$fac_id}}) {
    #$worksheet->write_row($row, $col, $arrayref);
    #here, I would like to alphabetically sort by
    #$chpr->{$college}{$dept}{$rank}{$fac_id}{$subj}{sort_name}
    #so that the faculty names appear in alphabetical order listed
    #by college, department, rank, and subject (faculty ID isn't wanted)
    }
    }
    }
    }
    }

    I can't see the $subj from $fac_id, and once I get to $subj, the has is already sorted by $fac_id. I can't figure out a way to retroactively unsort by $fac_id and sort by ...{$fac_id}...{sort_name}.

    Ordering the faculty by sort_name isn't possible because sort_name is not unique, but fac_id is unique.

    Suggestions? Other than rewriting the whole thing?

    Thanks, CC.
     
    ccc31807, Nov 6, 2013
    #1
    1. Advertising

  2. ccc31807

    Jim Gibson Guest

    In article <>,
    ccc31807 <> wrote:

    > I have a reference to a hash defined like this:
    > $chpr->{$college}{$dept}{$rank}{$fac_id}{$subj} = {
    > ...,
    > sort_name = $sort_name,
    > ...};
    >
    > I print an Excel report like this:
    > #open Excel workbook for COLLEGE
    > foreach my $college (sort keys %{$chpr}) {
    > foreach my $dept (sort keys %{$chpr->{$college}}) {
    > foreach my $rank (sort keys %{$chpr->{$college}{$dept}}) {
    > #open new worksheet for DEPARTMENT
    > foreach my $fac_id (sort keys %{$chpr->{$college}{$dept}{$rank}}) {
    > foreach my $subj (sort keys %{$chpr->{$college}{$dept}{$rank}{$fac_id}}) {
    > #$worksheet->write_row($row, $col, $arrayref);
    > #here, I would like to alphabetically sort by
    > #$chpr->{$college}{$dept}{$rank}{$fac_id}{$subj}{sort_name}
    > #so that the faculty names appear in alphabetical order listed
    > #by college, department, rank, and subject (faculty ID isn't wanted)
    > }
    > }
    > }
    > }
    > }
    >
    > I can't see the $subj from $fac_id, and once I get to $subj, the has is
    > already sorted by $fac_id. I can't figure out a way to retroactively unsort
    > by $fac_id and sort by ...{$fac_id}...{sort_name}.
    >
    > Ordering the faculty by sort_name isn't possible because sort_name is not
    > unique, but fac_id is unique.
    >
    > Suggestions? Other than rewriting the whole thing?


    I would stop at the $rank iteration and create a temporary data
    structure that holds the data you want to print for that combination of
    ($college,$dept,$rank). I think you want to basically invert the hash
    array for that point onward.

    If you have a hash called %data, then iterate over $fac_id and $subj,
    saving the data as $data{$subj}{$fac_id}. Then, when you are done, you
    can iterate over subjects, iterate over faculty name, and print.

    I might also create a %faculty_names hash using $faculty_name{$fac_id}
    = sort_name for looking up the faculty name given the ID.

    I would also recommend saving each hash reference in a scalar variable,
    thereby getting rid of the multi-level hash fetches:

    foreach my $college (sort keys %{$chpr}) {
    my $college_ref = $chpr->{$college};
    foreach my $dept (sort keys %{$college_ref} ) {
    my $dept_ref = $college_ref->{$dept};
    ...

    --
    Jim Gibson
     
    Jim Gibson, Nov 7, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward C. Jones

    Pickle vs. eval for deeply nested objects

    Edward C. Jones, Feb 18, 2004, in forum: Python
    Replies:
    0
    Views:
    502
    Edward C. Jones
    Feb 18, 2004
  2. Ori Y
    Replies:
    1
    Views:
    414
    Bob Ippolito
    Feb 28, 2004
  3. Kirk Strauser
    Replies:
    1
    Views:
    312
    Kirk Strauser
    Jun 11, 2004
  4. Kay Schluehr
    Replies:
    7
    Views:
    371
    Kay Schluehr
    Jan 20, 2006
  5. Replies:
    3
    Views:
    121
Loading...

Share This Page