Sorted Hash and or Array

Discussion in 'Perl Misc' started by banker123, Nov 3, 2006.

  1. banker123

    banker123 Guest

    I have three variables that are extracted and captured in my perl
    program. I currently write these variables to a report using the perl
    format function which works great. I would now like to sort this data
    using a sort table.

    Example: (Read variables into an Array or Hash)
    $box=123456 $box=999999
    $job=123456N $job=999999N
    $file=110306_123456.bdf $file=110306_999999.bdf

    Sort Table (Array or Hash)
    Box Priority
    999999 1
    123456 2

    Output
    Sorted array or hash I presume, sorted using the priority column in the
    sort table, like this:

    999999 999999N 110306_999999.bdf #Priority #1
    123456 123456N 110306_123456.bdf #Priority #2
     
    banker123, Nov 3, 2006
    #1
    1. Advertising

  2. banker123

    Manish Guest

    banker123 wrote:
    > I have three variables that are extracted and captured in my perl
    > program. I currently write these variables to a report using the perl
    > format function which works great. I would now like to sort this data
    > using a sort table.
    >
    > Example: (Read variables into an Array or Hash)
    > $box=123456 $box=999999
    > $job=123456N $job=999999N
    > $file=110306_123456.bdf $file=110306_999999.bdf
    >
    > Sort Table (Array or Hash)
    > Box Priority
    > 999999 1
    > 123456 2
    >
    > Output
    > Sorted array or hash I presume, sorted using the priority column in the
    > sort table, like this:
    >
    > 999999 999999N 110306_999999.bdf #Priority #1
    > 123456 123456N 110306_123456.bdf #Priority #2


    and your question ... is ...
     
    Manish, Nov 3, 2006
    #2
    1. Advertising

  3. banker123

    banker123 Guest

    > and your question ... is ...

    How do I sort this data using the sort table as explained in the first
    post. Should I read the variables into an array or hash? Help with
    this is appreciated, I am still new to perl.
     
    banker123, Nov 3, 2006
    #3
  4. banker123

    Guest

    banker123 wrote:
    > I have three variables that are extracted and captured in my perl
    > program. I currently write these variables to a report using the perl
    > format function which works great. I would now like to sort this data
    > using a sort table.
    >
    > Example: (Read variables into an Array or Hash)
    > $box=123456 $box=999999
    > $job=123456N $job=999999N
    > $file=110306_123456.bdf $file=110306_999999.bdf
    >
    > Sort Table (Array or Hash)
    > Box Priority
    > 999999 1
    > 123456 2
    >
    > Output
    > Sorted array or hash I presume, sorted using the priority column in the
    > sort table, like this:
    >
    > 999999 999999N 110306_999999.bdf #Priority #1
    > 123456 123456N 110306_123456.bdf #Priority #2


    The data structure you use could determine how you do your problem. The
    hash I set up below could be one solution. (I haven't used formats, so
    can't help you there)

    Chris

    #!/usr/bin/perl
    use strict;
    use warnings;

    my %data = (123456 => { job => '123456N',
    file => '110306_123456.bdf',
    priority => 2
    },
    999999 => { job => '999999N',
    file => '110306_999999.bdf',
    priority => 1
    }
    );

    for my $box (sort by_priority keys %data) {
    my @data = ($box, @{ $data{$box} }{qw/job file/});
    print "@data\n";
    }

    sub by_priority {
    $data{$a}{priority} <=> $data{$b}{priority};
    }
     
    , Nov 3, 2006
    #4
  5. banker123

    banker123 Guest

    This works an accomplishes the objective however my code loops
    extracting the $box, $job, and $filename, I am not sure how to put
    thsese variables into a hash in the format below. Please help

    Also I would like to mantain a seperate hash with the priority, the
    priority would be looked up in the hash.

    Example Hash:
    my %priority=
    (123456=>2),
    (999999=>1)

    > #!/usr/bin/perl
    > use strict;
    > use warnings;
    >
    > my %data = (123456 => { job => '123456N',
    > file => '110306_123456.bdf',
    > priority => 2
    > },
    > 999999 => { job => '999999N',
    > file => '110306_999999.bdf',
    > priority => 1
    > }
    > );
    >
    > for my $box (sort by_priority keys %data) {
    > my @data = ($box, @{ $data{$box} }{qw/job file/});
    > print "@data\n";
    > }
    >
    > sub by_priority {
    > $data{$a}{priority} <=> $data{$b}{priority};> }
     
    banker123, Nov 5, 2006
    #5
    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. rp
    Replies:
    1
    Views:
    581
    red floyd
    Nov 10, 2011
  2. Chris
    Replies:
    12
    Views:
    170
    Brian Candler
    Sep 18, 2004
  3. Anthony Martinez
    Replies:
    4
    Views:
    296
    Robert Klemme
    Jun 11, 2007
  4. Michal Suchanek
    Replies:
    6
    Views:
    256
    Nobuyoshi Nakada
    Jun 13, 2007
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    670
    David A. Black
    Jul 2, 2008
Loading...

Share This Page