Re: Accomodate for poor db design using Sort::Maker?
- From: "John W. Krahn" <someone@xxxxxxxxxxx>
- Date: Sun, 10 Dec 2006 07:09:25 GMT
harryfmudd [AT] comcast [DOT] net wrote:
Have you looked at List::Util? It's a standard module in Perl 5.8, and
contains a max function. So the first loop becomes something like
use List::Util qw{max};
my $mode = max (map {$hashref->{"col$_"}} 1..5);
Assuming the only zeros are trailing, the second loop could be
my $max = grep {$_ > 0} map {$hashref->{"col$_"}} 1..5
since the value of a list in scalar context is the number of elements in
the list. If you have zeros embedded, this won't give the right answer,
since what it really does is find the number of values > 0, not the
highest index with a value > 0.
Of course, for either of these you might want to make the hashref into a
real list, to avoid mapping twice.
The OP's code produces:
id mode max
1 2 3
2 1 1
3 3 5
4 3 4
5 2 3
6 1 3
7 4 4
8 2 2
9 1 5
10 2 3
11 1 1
12 1 5
But your's produces:
id mode max
1 8 3
2 5 1
3 8 5
4 8 4
5 7 3
6 9 3
7 7 4
8 3 2
9 9 5
10 4 2
11 0 0
12 1 5
So neither $mode nor $max is correct for all lines.
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.
- Follow-Ups:
- Re: Accomodate for poor db design using Sort::Maker?
- From: harryfmudd [AT] comcast [DOT] net
- Re: Accomodate for poor db design using Sort::Maker?
- References:
- Accomodate for poor db design using Sort::Maker?
- From: DJ Stunks
- Re: Accomodate for poor db design using Sort::Maker?
- From: John W. Krahn
- Re: Accomodate for poor db design using Sort::Maker?
- From: harryfmudd [AT] comcast [DOT] net
- Accomodate for poor db design using Sort::Maker?
- Prev by Date: Re: Proposal: String::Format::General
- Next by Date: Re: Accomodate for poor db design using Sort::Maker?
- Previous by thread: Re: Accomodate for poor db design using Sort::Maker?
- Next by thread: Re: Accomodate for poor db design using Sort::Maker?
- Index(es):
Relevant Pages
|
|