Sorted Hash and or Array

B

banker123

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
 
M

Manish

banker123 said:
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 ...
 
B

banker123

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

charley

banker123 said:
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};
}
 
B

banker123

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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top