'%found_one' starts off empty.  Everytime we find a new element, we add one
to '$found_one{$_}' via '++'.  Since '++' is postfix, it occurs AFTER the
value is returned.  In otherwords, '$found_one{$_}++' returns the current
value and then adds one.

Now, if the current element has never been read before, then
'$found_one{$_}' doesn't exist, and so returns false.  '$found_one{$_}++' in
this case is equivalent to '$found_one{$_}=1'.  In otherwords, we create an
entry in '%found_one' with  key of '$_'.  If, on the other hand, the element
was read before, then '$found_one{$_}' returns true.  It then increments the
value and sets it equal to 2, but we don't care about that, since we've
already jumped out of the sub via return.  By the end of the loop, if we
haven't returned out, we know that each element in '@_' is unique, and
'%found_one' has exactly one key for each $_ in @_, each set to 1.  So, we
just return false.

Besides the fact that you introduce a lot of unnecessary variables, the only
difference between your sub and mine is that yours performs '$n log $n'
comparisons (is that right ... ?) whereas mine only performs '$n'.  I don't
know enough about perl to know which method is more efficient.  It could be
that the overhead involved in hashing outweighs the comparisons and array
lookups that you used.  I don't know.  I use this method because it's less
cluttered and makes more sense to me.

Although, if i were going to write this sub for script i were using, i would
DEFINITELY use array-refs like you did.

Oh yeah, and another thing; i think
        for (my $i=0; $i<$n; $i++)
is preferable to
        for my $i (0...$n-1)
since the latter creates the entire array and then goes through the
elements, instead of merely going from one integer to the next.

hth!
christopher

-----Original Message-----
From: Wagner Jeff Civ Logicon/TTMS
[mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 31, 2001 11:40 AM
To: [EMAIL PROTECTED]
Subject: RE: check array element (HELP)


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]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to