Trivial 'unless' Question

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Re: Trivial 'unless' Question

    On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote:
    > # ... but can I do something like....
    > print "first\n" unless ($counter) else { print "second\n";
    Not really. You could use the conditional operator, though.

    print $counter ? "second\n" : "first\n";

    --
    Steve
    Steve Grazzini Guest

  2. Similar Questions and Discussions

    1. #9516 [Opn->Bgs]: No trivial way to bypass safe mode when running as a shell
      ID: 9516 Updated by: rasmus@php.net Reported By: bram at xspace dot com -Status: Open +Status: Bogus...
    2. a trivial type question
      When I'm doing magazine ads, there is frequently a lot of pushing and pulling to get type to fit in a certain area of the ad... for instance,...
    3. Trivial encryption
      "William Tasso" wrote: Map everything to zero. It (a) is trivial, (b) is one-way, and (c) will confound almost anyone. -- Dave Anderson ...
    4. Trivial stuff
      Please settle an argument... Some file extensions are pronounced as a word...like "jaypeg" for .jpg. I've been doing graphics for a few years...
    5. Trivial sqlplus questions
      On Sat, 21 Dec 2002 20:06:35 GMT, CoolDude <nospam@nospam.it> wrote: some *sql*plus* help *can* be stored in the instance, provided one does...
  3. #2

    Default Trivial 'unless' Question

    Hi ..... very trivial ... is there a way or correct syntax to add an 'if' tp
    the following 'unless' statement?

    # this works fine ...
    print "first\n" unless ($counter);
    # ... but can I do something like....
    print "first\n" unless ($counter) else { print "second\n";
    # (syntax error)

    I know I can do this:

    #!/usr/bin/perl
    use warnings;
    use strict;
    my $counter = 0;

    unless ($counter) {
    print "first\n";
    } else {
    print "second\n";
    }

    ..... but I wanted to put the 'unless' later in the statement. I couldn't
    find anything in the FAQ or perldoc on this one.

    TIA!

    -JW




    __________________________________
    Do you Yahoo!?
    The New Yahoo! Shopping - with improved product search
    [url]http://shopping.yahoo.com[/url]
    Jeff Westman Guest

  4. #3

    Default Re: Trivial 'unless' Question

    On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote:
    > Steve Grazzini <grazz@pobox.com> wrote:
    > > On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote:
    > > > # ... but can I do something like....
    > > > print "first\n" unless ($counter) else { print "second\n";
    > >
    > > Not really. You could use the conditional operator, though.
    > >
    > > print $counter ? "second\n" : "first\n";
    >
    > True, but I was looking for a way to do this with a somewhat "buried"
    > unless keyword.
    print +( qw(second first --> unless <--) )[ not $counter ];

    --
    Steve
    Steve Grazzini Guest

  5. #4

    Default Re: Trivial 'unless' Question

    Steve Grazzini <grazz@pobox.com> wrote:
    > On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote:
    > > # ... but can I do something like....
    > > print "first\n" unless ($counter) else { print "second\n";
    >
    > Not really. You could use the conditional operator, though.
    >
    > print $counter ? "second\n" : "first\n";
    True, but I was looking for a way to do this with a somewhat "buried" unless
    keyword.

    TA

    -JW

    __________________________________
    Do you Yahoo!?
    The New Yahoo! Shopping - with improved product search
    [url]http://shopping.yahoo.com[/url]
    Jeff Westman Guest

  6. #5

    Default Re: Trivial 'unless' Question

    In article <20031021195420.GE10120@grazzini.net>, Steve Grazzini wrote:
    > On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote:
    >> Steve Grazzini <grazz@pobox.com> wrote:
    >> > On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote:
    >> > > # ... but can I do something like....
    >> > > print "first\n" unless ($counter) else { print "second\n";
    >> >
    >> > Not really. You could use the conditional operator, though.
    >> >
    >> > print $counter ? "second\n" : "first\n";
    >>
    >> True, but I was looking for a way to do this with a somewhat "buried"
    >> unless keyword.
    >
    > print +( qw(second first --> unless <--) )[ not $counter ];
    ??? :-|


    --
    Kevin Pfeiffer

    Kevin Pfeiffer Guest

  7. #6

    Default RE: Trivial 'unless' Question

    On Tuesday, October 21, 2003 16:01, Kevin Pfeiffer wrote:
    >In article <20031021195420.GE10120@grazzini.net>, Steve Grazzini wrote:
    >
    >> On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote:
    >>> Steve Grazzini <grazz@pobox.com> wrote:
    >>> > On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote:
    >>> > > # ... but can I do something like....
    >>> > > print "first\n" unless ($counter) else { print "second\n";
    >>> >
    >>> > Not really. You could use the conditional operator, though.
    >>> >
    >>> > print $counter ? "second\n" : "first\n";
    >>>
    >>> True, but I was looking for a way to do this with a somewhat "buried"
    >>> unless keyword.
    >>
    >> print +( qw(second first --> unless <--) )[ not $counter ];
    >
    >??? :-|
    >
    I think the author was trying to be cute... I got a chuckle out of it!

    print +( qw(second first --> unless <--) )[ not $counter ];

    Basically, you are creating an anonymous (right term?) array, and printing
    the "not $counter"'th element. So if $counter is 0, you print "first",
    otherwise you print "second". The other stuff in the array is there just to
    satisfy the "buried" unless requested by the OP. However, as you see here,
    it does not use the keyword "unless", just a string "unless". The only
    elements that will ever be accessed are "second" and "first"...

    It could also be written as:

    print +( qw(second first) )[ not $counter ];

    HTH
    Alan
    Alan Perry Guest

  8. #7

    Default Re: Trivial 'unless' Question

    [email]westfour@yahoo.com[/email] (Jeff Westman) wrote in message news:<20031021191717.66026.qmail@web41107.mail.yah oo.com>...
    > # ... but can I do something like....
    > print "first\n" unless ($counter) else { print "second\n";
    The predicate conditionals are limited: there is no elsing with them.
    The suggestion to use the ternary operator (?:) is appropriate for
    this example. If you really wanted to sneak some more conditional
    logic into a predicate, you'll have to do it with operators:
    print "first\n" unless ($counter and print "second\n");

    I strongly advise against that sort of garbling. It would be much
    clearer to write
    print(($counter) ? 'second'
    : 'first'
    , "\n");
    or just
    if ($counter == 0) { print "first\n" }
    else { print "second\n" }
    Roy Johnson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139