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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help using or in perl.

Status
Not open for further replies.

Perlguy128

Programmer
Joined
Jul 7, 2007
Messages
1
Location
US
I'm making a program that has a number random out of 64, and when it lands on that number, a variable is set to that number. Then it does random again and before doing anything else it checks to see that the number isn't equal to one of the 64 variables.
But I need help with something else.

if($var eq $a || $var eq $b || $var eq $c)

I need it to check like this every variable at once so if it's equal to all of them it goes to another part of the program. The problem is I have so many variables, they won't fit on one line, so is it possible to put the if on two lines or perhaps shorten it?
 
Code:
[gray][i]# Manually List out the values[/i][/gray]
[olive][b]if[/b][/olive] [red]([/red][url=http://perldoc.perl.org/functions/grep.html][black][b]grep[/b][/black][/url] [red]{[/red][blue]$var[/blue] eq [blue]$_[/blue][red]}[/red] [red]([/red][blue]$a[/blue], [blue]$b[/blue], [blue]$c[/blue], [blue]$d[/blue], [blue]$e[/blue], [blue]$f[/blue][red])[/red][red])[/red] [red]{[/red]

[gray][i]# Or accumulate your list into an array[/i][/gray]
[olive][b]if[/b][/olive] [red]([/red][black][b]grep[/b][/black] [red]{[/red][blue]$var[/blue] eq [blue]$_[/blue][red]}[/red] [blue]@valuesToTest[/blue][red])[/red] [red]{[/red]

- Miller
 
This can be done quite easily with a few lines of code. See this FAQ:

Get 'x' number of unique, random, elements from an array




------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top