For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2004 > Calculate Min Value









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 Calculate Min Value
Ilyas Hassan

2004-10-26, 3:56 pm

Please see the code below based on suggestions from this group.
=========== Actual Code ============
$i=0;
sub min {
my $min = shift(@_);
foreach my $value (@_) {
if ($value < $min) {
$min = $value;
}
}
return $min;
}

open INV_RPT, "$_inventory_rpt" or die "Can't open file handler";
open (INV_RPT_MIN, ">$_inventory_rpt_min");

while($Ctmp1 = <INV_RPT> )
{
chop($Ctmp1);
@Ctmp2=split(/~/,$Ctmp1);
$items{$Ctmp2[0]}=$Ctmp2[0];
}
foreach my $item (<> )
{
my @columns = split(/\~/, $line);
push($data{$columns[0]}, \@columns);
}

foreach $item (keys %items)
{
my @colums2values = map {$_->[3]} (@{$items{$item}});
my @colums2values = map {$_->[4]} (@{$items{$item}});
my @colums2values = map {$_->[5]} (@{$items{$item}});

print INV_RPT_MIN "$item: ".min(@colums2values);
}
=======

I get the following error message on executing the above code,
==== error message ========
Type of arg 1 to push must be array (not hash element) at
F:\scripts\inve
ntory_rpt_min.pl line 42, near "@columns)"
============================

Q: How do I resolve the above issue? Please point out problems with
the code.
Q: Is there a way to provide dynamic variable $i to @columns2values so
it looks at all columns starting at col.2 to the end-of-file.

Thanks for the help!!
Sponsored Links







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

Copyright 2008 codecomments.com