For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2006 > Count the total element in 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 Count the total element in array
jyetying

2006-06-25, 9:58 pm

Hello all,
I need a script to count the total element in a array and print error
if the total is not even numbers.

My script :
@par1=("(x1 y1)", "(x2 y2)", "(x3 y3)");
@par2=("(x1 y1)", "(x2 y2)", "(x3 y3)", "(x4 y4)");
@par3=("(x1 y1)", "(x2 y2)");


$count1=$#par1 +1;
print ("$count1","\n");
if($count2%2 != 0){
print("Error","\n");
}

$count2=$#par2 +1;
print ("$count2","\n");
if($count2%2 != 0){
print("Error","\n");
}

$count3=$#par3 +1;
print ("$count3","\n");
if($count3%2 != 0){
print("Error","\n");
}

The script above can run. But, how can I put it in a more sistematic
and shorter script? For example, if I have par1 until par10, how can I
shorten my scripts? Need help?

DJ Stunks

2006-06-26, 6:58 pm

jyetying wrote:
> Hello all,
> I need a script to count the total element in a array and print error
> if the total is not even numbers.
>
> My script :
> @par1=("(x1 y1)", "(x2 y2)", "(x3 y3)");
> @par2=("(x1 y1)", "(x2 y2)", "(x3 y3)", "(x4 y4)");
> @par3=("(x1 y1)", "(x2 y2)");
>
>
> $count1=$#par1 +1;
> print ("$count1","\n");
> if($count2%2 != 0){
> print("Error","\n");
> }
>
> $count2=$#par2 +1;
> print ("$count2","\n");
> if($count2%2 != 0){
> print("Error","\n");
> }
>
> $count3=$#par3 +1;
> print ("$count3","\n");
> if($count3%2 != 0){
> print("Error","\n");
> }
>
> The script above can run. But, how can I put it in a more sistematic
> and shorter script? For example, if I have par1 until par10, how can I
> shorten my scripts? Need help?


Two things - first, to calculate the size of an array:

>From perldoc perlintro:


You might be tempted to use $#array + 1 to tell you how many
items there are in an array. Don't bother. As it happens,
using @array where Perl expects to find a scalar value
(``in scalar context'') will give you the number of elements
in the array:

if (@animals < 5) { ... }

therefore, in your script
$count3 = @par3

Second, to shorten your script and make it more portable you need to
come up with a better way of storing your arrays rather than giving
them all different names. It's a more advanced concept, but you can
(should) use a structure consisting of arrays of arrays:

my @pars = (
[], # $pars[0]
["(x1 y1)", "(x2 y2)", "(x3 y3)"], # $pars[1]
["(x1 y1)", "(x2 y2)", "(x3 y3)", "(x4 y4)"], # $pars[2]
["(x1 y1)", "(x2 y2)"], # $pars[3]
);

this will give you the opportunity to perform your tests in a loop, and
you can have as many par arrays as you like:

PAR:
for my $i ( 1..$#pars ) {
if ( @{ $pars[$i] } %2 ) {
warn "Error: \$pars[$i] contains an odd number of pairs\n";
next PAR;
}

# do something with valid @pars
}

If you have trouble understanding this you should read perlreftut and
perldata.

HTH,
-jp

Sponsored Links







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

Copyright 2008 codecomments.com