Samn has asked for the wisdom of the Perl Monks concerning the following question:

Replies are listed 'Best First'.
Re: Conditional variable assignments
by Abigail-II (Bishop) on May 25, 2003 at 12:03 UTC
    $this_user = $this_user == 0 ? "male" : $this_user == 1 ? "female" : $this_user == 2 ? "robot" : undef;

    Abigail

Re: Conditional variable assignments
by sauoq (Abbot) on May 25, 2003 at 09:04 UTC

    It's hard to decipher exactly what you want to do. It's easy to say that should always give $this_user eq 'robot'; because, if $this_user must be 0, 1, or 2, then it will never be less than 0. Try doing it by subscripting a list:

    $this_user = qw( male female robot )[$this_user];

    Rearrange to taste.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Conditional variable assignments
by PodMaster (Abbot) on May 25, 2003 at 09:00 UTC
    `perldoc perlsyn', ?:, 78752 ! perltutorial ! How to RTFM
    local $\="\n"; for $bar ( 0, 1, 2 ) { print "$bar => ", $bar > 0 ? $bar > 1 ? "male" : "female" : "robot"; } __END__ 0 => robot 1 => female 2 => male
    You should also read `perldoc perlop', cause "=" is not "==".


    MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
    I run a Win32 PPM repository for perl 5.6x+5.8x. I take requests.
    ** The Third rule of perl club is a statement of fact: pod is sexy.

Re: Conditional variable assignments
by dvergin (Monsignor) on May 26, 2003 at 05:15 UTC
    This is a different approach which may or may not suit your needs:
    my %types = ( 0 => 'male', 1 => 'female', 2 => 'robot' ); $this_user = $types{$some_value};
    Or, if you are going to use the categories only one time, you can code them as an on-the-fly anonymous hash...
    $this_user = { 0 => 'male', 1 => 'female', 2 => 'robot' }->{$some_value};
    HTH, David

    ------------------------------------------------------------
    "Perl is a mess and that's good because the
    problem space is also a mess.
    " - Larry Wall