chicken: (27. Braaaains!)
[personal profile] chicken
Question for the geeks.

I'm trying to find a Perl way of doing PHP's in_array.

I feel very stupid right now. Help!

(no subject)

Date: 2005-08-11 08:30 pm (UTC)
From: [identity profile] kernelm.livejournal.com
Looking around on google, it doesn't seem there is a direct equivalent. You'll have to write your own. Shouldn't be too difficult though. Any ol' search algorithm will do.

(no subject)

Date: 2005-08-11 08:42 pm (UTC)
From: [identity profile] chicken-cem.livejournal.com
I was hoping not to have to do so. I could just do a foreach, or I could use join to splat the array out into a string and then grep the string. I was just hoping there was some kind of equivalent to that very handy PHP function, but I guess not.

(no subject)

Date: 2005-08-11 08:37 pm (UTC)
From: [identity profile] paulv.livejournal.com

my @array = qw(buffy angel firefly);
my $wanted = "angel";

foreach my $array_value (@array) {
        if ($array_value eq $wanted) {
                print "yay!\n";
        } else {
                print "boo!\n";
        }
}

(no subject)

Date: 2005-08-11 08:44 pm (UTC)
From: [identity profile] chicken-cem.livejournal.com
I thought of that. However, I was hoping for something less verbose. I suppose as [livejournal.com profile] kernelm says, I could wrap that in a subroutine (returning a boolean ) and then call it as if it were a built-in function. I suppose having used PHP for so long now, going back to Perl feels like too much work.

(no subject)

Date: 2005-08-11 08:49 pm (UTC)
From: [identity profile] paulv.livejournal.com
Well, there's also this question in perlfaq4. It's probably right about using a hash instead of an array.

(no subject)

Date: 2005-08-11 09:14 pm (UTC)
From: [identity profile] chicken-cem.livejournal.com
Yes, a hash really is better in this case. Funny I didn't think of it because I've used three other hashes in this program so far. Grr. Argh!

BTW, major points for making your example use Whedonverse content. Shouldn't all programming examples work this way? ;-)

sub buffyBotSubRoutine_boolean001 {
  # Are we allowed to pass a whole
  #  array as a param?
  my @nest = shift;
  my $wanted = "Spike";;
  foreach my $vampire (@nest) {
    if ($vampire eq $wanted) {
      buffyBotSubRoutine_shagHim($vampire);
    } else {
      buffyBotSubRoutine_stakeHim($vampire);
    }
  }
}

(no subject)

Date: 2005-08-11 09:22 pm (UTC)
From: [identity profile] kernelm.livejournal.com
Careful, that's not really what you want.

You need to break upon finding a positive result. And only print a negative result at the end if there was no positie hit.

(no subject)

Date: 2005-08-11 09:33 pm (UTC)
From: [identity profile] chicken-cem.livejournal.com
Right, of course -- I made a subroutine and used 'return' to break out of the foreach and return the appropriate boolean. No big deal.

Profile

chicken: (Default)
chicken

April 2009

S M T W T F S
   1234
56 78 9 1011
12131415161718
192021 22232425
2627282930  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags