[thelist] Convert php to perl

Jeremy Ashcraft ashcraft at 13monkeys.com
Thu Aug 2 00:00:16 CDT 2001


I haven't seen a built in function for in_array in perl, and since in_array 
is a PHP4 function, I had my own in_array function I wrote back in the PHP3 
days.  Here it is ported to perl.  Just put this in your perl script or 
include it in from another file.

sub in_array() {
    my $val = shift(@_);
 
    foreach $elem(@_) {
        if($val == $elem) {
            return 1;
        }
    }
    return 0;
}

note: this only works comparing numbers, if you want to compare strings, use 
if($val eq $elem)

enjoy

jeremy

On Wednesday 01 August 2001 21:43, you wrote:
> I am creating a perl version of a php game I wrote some time
> ago.   Unfortunately, my knowledge of perl is rather limited.  In PHP,
> there is a function called in_array.  I have not been able to find an
> equivalent in perl.   Can anyone offer any advice I completing this
> section?
>
> $winning_id = 1;
> $max_id = 5;
>
> for($count = 1; $count < 3; $count++ )
> {
> do{
>          $tmp = int(my_rand($max_id)) + 1;
> }while(in_array($tmp, $my_rand) || ($tmp == $winning_id));  # <------
> In_array line
>          $my_rand[$count] = $tmp;
> }
>
> Thanks for any help
>
> Chris
>
>
> ---------------------------------------
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt !




More information about the thelist mailing list