[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Modifiers



At 10.24 -0500 1999.02.05, Emmanuel. M. Decarie wrote:
>@list =  (1..9);
>
>foreach(@list) {print "$_\n"} if (@list);  # Syntax error...
>
>or
>
>(@list) ? foreach(@list) {print "$_\n"} : print "Empty list\n"; #Syntax
>error...

Well, the clean, canonical way is:

  if (@list) {
    foreach (@list) {
      print "$_\n";
    }
  } else {
    print "Empty list\n";
  }

But you could do:

  print @list ? map {"$_\n"} @list : "Empty list\n";

Where @list (forced into scalar context by the ?:) is true, return a list
of the elements in @list with \n appended, or the string "Empty list\n".

Or, for those who don't like map:

  print @list ? join("\n", @list) . "\n" : "Empty list\n";

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10  1FF77F13 8180B6B6'])

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch