Hi,
I am one of the beginners list's many voyeurs. Can you explain your code?
Where does the "%found_one" hash get its initial value? I understand that
"@_" represents the list that was passed to the subroutine and that "$_" is
the current list element for each loop iteration. Does your code assume
that the list is sorted? How does it compare to this code fragment?
##############################
sub repeated_elements {
my $list = shift; # passed list reference
my $n = $#{$list}; # subscript of last list element
for my $i (0..$n-1) {
for my $j ($i+1..$n) {
if ($list->[$i] eq $list->[$j]) {return 1}
}
}
return 0;
}
if (repeated_elements(\@whatever)) {
print "There are repeated elements\n";
}
else {
print "There are no repeated elements\n";
}
##############################
Thanks,
Jeff
-----Original Message-----
From: Mooney Christophe-CMOONEY1 [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 31, 2001 08:44
To: [EMAIL PROTECTED]
Subject: RE: check array element (HELP)
I would probably do this, although i'm sure some smarty-pants could come up
with a one-liner ;)
sub repeated_elements
{
my %found_one;
for (@_)
{
return 1 if $found_one{$_}++;
}
return 0;
}
if (repeated_elements @whatever)
{
# there are repeated elements
}
else
{
# there are no repeated elements
}
-----Original Message-----
From: GRANATA ROBERTA [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 31, 2001 9:28 AM
To: [EMAIL PROTECTED]
Subject: check array element (HELP)
--- Erhalten von ZBM.ZAGTA 089/32000-414 31-07-01 15.28
hi All,
I have an array of 0...n elements.
I want to check that each element of the array,
must be different to each other.
if it is so -> error,
else -> go on.
please,
Can somebody help me,to write this code.?
thanks in advance,
Best Regards,
roberta
---- 31-07-01 15.28 ---- Gesendet an ------------------------------------
-> beginners(A)perl.org
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]