Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Sorted Hash and or Array

Reply
Thread Tools

Sorted Hash and or Array

 
 
banker123
Guest
Posts: n/a
 
      11-03-2006
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

 
Reply With Quote
 
 
 
 
Manish
Guest
Posts: n/a
 
      11-03-2006

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

 
Reply With Quote
 
 
 
 
banker123
Guest
Posts: n/a
 
      11-03-2006
> 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.

 
Reply With Quote
 
charley@pulsenet.com
Guest
Posts: n/a
 
      11-03-2006
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};
}

 
Reply With Quote
 
banker123
Guest
Posts: n/a
 
      11-05-2006
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};> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
Benchmark segfault [Was: Array#inject to create a hash versus Hash[*array.collect{}.flatten] ] Michal Suchanek Ruby 6 06-13-2007 04:40 AM
Array#inject to create a hash versus Hash[*array.collect{}.flatten] -- Speed, segfault Anthony Martinez Ruby 4 06-11-2007 08:16 AM
Accessing Hash elements in sorted order? Chris Ruby 12 09-18-2004 04:23 PM



Advertisments