On Wed, 2004-05-26 at 15:07, willy.k wrote:
> Jose
> 
> Many thanks for replying so quickly.
> I will explain again, sorry but is complicated.
> 
> 
> 
> ###########################
>  I have an array called group_vals.
> 
> This array can be populated by any combination of values
>  e.g.  /sp|lp|ep12|ffp|vid|dvd|bx|bkk|cd|csd/
> 
>  I want to be able to say that:
> 
>  if the array contains ANY COMBINATION OF ONLY the values 'cd' and/or 'csd'
> 
> #Ex 1.  cd
> #Ex 2.  csd
> #Ex 3.  cd csd
> #Ex 4.  cd cd csd
> #Ex 5. csd csd cd
> #Ex 6. cd cd cd cd .....upto 10 items
> ###################################
> # THIS WORKS, thanks                                    #
> #   unless ( grep { !/^(?:cd|csd)$/ } @array) {  #
> ###################################
> 
> 
> 
> then do x
> }
> elsif
> 
> if the array contains any combination of values from the populated group,
> but not cd or csd by themsleves
> 
> #Ex 1.  sp
> #Ex 2.  bkk
> #Ex 3.  cd sp
> #Ex 4.  cd bkk lp
> #Ex 5. cd bkk bkk
> #Ex 6. cd sp bkk ep12
> #Ex 7. cd csd bkk lp ep12.....upto 10 items
> ###########################################################
> # THIS DOES NOT WORK
> #
> # elsif ( grep { /^(?:cd|csd)$/ } @array and grep { !/^(?:cd|csd)$/ }
> @array) {  #
> ###########################################################

well, but this will (I hope)

elsif ( grep { /^(?:sp|lp|ep12|ffp|vid|dvd|bx|bkk|cd|csd)$/ } @array and
grep { ! /^(?:cd|csd)$/ } @array ) {

# which fails only if there are other elements in the array that you
don't want or if the array is constituted only by cd's and csd's


This should work, unless I mistyped something... :-)


If you're using /^(?:sp|lp|ep12|ffp|vid|dvd|bx|bkk|cd|csd)$/ more than
once, I would advise this instead:

my $acceptable = qr/^(?:sp|lp|ep12|ffp|vid|dvd|bx|bkk|cd|csd)$/;

and then using $acceptable to do the matchings, such as

grep /$acceptable/, @array


it would be faster and more readable :-)

HTH,

jac

> then do y
> 
> 
> #####################################
> 
> 
> 
> Thanks
> William
-- 
Josà Alves de Castro <[EMAIL PROTECTED]>
Telbit - Tecnologias de InformaÃÃo


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to