For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > November 2006 > Sorted Hash and or Array









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorted Hash and or Array
banker123

2006-11-03, 6:57 pm

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

Manish

2006-11-03, 6:57 pm


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

banker123

2006-11-03, 6:57 pm

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

charley@pulsenet.com

2006-11-03, 6:57 pm

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};
}

banker123

2006-11-05, 6:57 pm

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};> }


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com