Re: Accomodate for poor db design using Sort::Maker?



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
.



Relevant Pages

  • Re: perl grep problem
    ... point of the excercise for me was just to try and learn some perl and ... first loop as you suggested. ... Prev by Date: ...
    (comp.lang.perl.misc)
  • Re: OpenOffice build
    ... >> Im trying to install openoffice and am getting ... > # use.perl port ... > standard module in more recent versions of perl than the one the ... Updated perl as well as p5-File-Spec. ...
    (freebsd-questions)
  • Re: Distribution of modules, historically
    ... searching for. ... > script which I am responsible for maintaining - is not present at ... On my own system (Perl 5.8.6) the module Date::Calc isn't present. ... isn't a standard module yet. ...
    (comp.lang.perl.misc)
  • Re: How to take apart the filepath using regular expression?
    ... Marco wrote: ... > How can I use regular expression to take it apart? ... # Standard module included in Perl ...
    (perl.beginners)
  • Re: OpenOffice build
    ... >> standard module in more recent versions of perl than the one the ... > portupgrade -rf for this. ... updated perl you've installed. ... depending on which perl port you installed. ...
    (freebsd-questions)