in reply to Testing for a string value within an array
I would write it like this:
$filename = "$directory/$entry"; if( myExists( $filename, @mounts ) == 0 ){ push(@mounts, $filename); } sub myExists(){ my $compare = shift; foreach my $element (@_){ if( $element eq $compare ){ return 1; } } return 0; }
I know that this is more code than some of the previous solutions, but it has advantages. It is more readable and can allow you to more precisely define what it means to exist. Say for instance that you wanted to only add it if it were unique in the first 256 charachters, or alter it otherwise, this would be easy to do. In addition, it is also more resource friendly and less confusing than maintaining both arrays and hashes.
Good luck
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Testing for a string value within an array
by Roy Johnson (Monsignor) on May 03, 2005 at 23:58 UTC | |
|
Re^2: Testing for a string value within an array
by Fletch (Bishop) on May 04, 2005 at 00:02 UTC |