Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$var eq $array["something"] 1

Status
Not open for further replies.

naq2

Programmer
Aug 3, 2004
74
FR
Is there any way of testing if a varible is one of the values contained in an array.
All this in one line! :)

We can do it using a foreach loop, but I was wondering if there was a standart function for this.

Thanks.
 
$myarray = ("a","b","c");
$mytest = "a";
if ($mytest eq $myarray[0]){
.
.
.
do something
.
.
.
}

There's always a better way. The fun is trying to find it!
 
Nice! but this only thest for it on the first element! :)

I'm writing the function, I'll post it in 2mins.
 
Here it is:

Code:
sub testArrayForValue {
	my ($rArray, $value) = @_ ;
	
	foreach my $tmpValue (@$rArray) {
		if ($value eq $tmpValue) {
			return 1 ;	
		}
	}
	
	return 0 ;
}


Hope it helps.
 
Your code will step thru each array element ($tmpValue) and compare it to $value. I thought that's what you wanted. Did I miss something?

There's always a better way. The fun is trying to find it!
 
Yes... this was what I wanted to do but...
All this in one line! :)

I was asking if it was possible do to it WITHOUT a for loop.
 
Yep, missed that completely! In a word, NO! Can't be done, unless you know the exact number of array elements. But then, it wouldn't be any faster than a for loop and would probably take more overhead. The loop you have is the most efficient method available to you at this point.

There's always a better way. The fun is trying to find it!
 
What about grep?
Code:
my @myarray = qw(a b c);
my $mytest = "z";
print grep(/^$mytest$/, @myarray)? "yes": "no", "\n";
 
euh... I think this is smart... but could you explain it a bit more?

Thanks.
 
join
Code:
$list=join "", @array;
if (index $list, $searchstring != -1) {
 # it's in the list, ## but where
} else {
 # it's not in the list
}
it should be possible to
Code:
$list=join "", @array;
if (index (join "",@array), $searchstring != -1) {
 # it's in the list, ## but where
} else {
 # it's not in the list
}
but I haven't tried it

HTH
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Code:
print grep(/^$mytest$/, @myarray)? "yes": "no", "\n";
is a shorthand way of saying
Code:
if (grep(/^$mytest$/, @myarray)) {
    print "yes\n";
} else {
    print "no\n";
}
The original post asked for something in one line.
Here's the documentation on "?:" from perlop
Conditional Operator
Ternary "?:" is the conditional operator, just as in C. It works much
like an if-then-else. If the argument before the ? is true, the argument
before the : is returned, otherwise the argument after the : is
returned. For example:

printf "I have %d dog%s.\n", $n,
($n == 1) ? '' : "s";

Scalar or list context propagates downward into the 2nd or 3rd argument,
whichever is selected.

$a = $ok ? $b : $c; # get a scalar
@a = $ok ? @b : @c; # get an array
$a = $ok ? @b : @c; # oops, that's just a count!

The operator may be assigned to if both the 2nd and 3rd arguments are
legal lvalues (meaning that you can assign to them):

($a_or_b ? $a : $b) = $c;

Because this operator produces an assignable result, using assignments
without parentheses will get you in trouble. For example, this:

$a % 2 ? $a += 10 : $a += 2

Really means this:

(($a % 2) ? ($a += 10) : $a) += 2

Rather than this:

($a % 2) ? ($a += 10) : ($a += 2)

That should probably be written more simply as:

$a += ($a % 2) ? 10 : 2;
So, if grep returns a true value (element is found in list), print "yes", otherwise print "no".
 
actually I know verry well the ?: syntax... but I don't know grep. :)
 
Matt? that you??

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
depends.. which paul is this? ;)

Matt Corby-Eaglen... matt[dot]corby[at]btinternet.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top