Hi Jerry

The simple answer is, you cant.

A better answer is that an if statement used as you have done is not a compund if statement, but an if statement modifier (see perlsyn). Consider

my $x=1 if $y; # statement modifier if ($y) {my $x=1} # compund statement
The two are very different. The first declares $x to be in scope and if $y is true initializes it to 1. The second checks to see if $y is true, then declares $x to be equal to 1 _within_ its own scope, so $x wont be available after the if completes.

But it seems to me that what you want is one of the two following snippets.

# first variant, dont do this. foreach (@input){ push @name_nums, (exists $list{$_}) ? $list{ $_ } : $_; } # second variant, do this @name_nums=map{(exists $list{$_}) ? $list{ $_ } : $_}@input;
This is precisely the task that map was invented for. Basically apply a function to every element of an array putting the results in another array.

Update The solutions above use the secret third kind of if. Its called the Conditional Operator or sometime the ternary operator and works like this:
condition ? result : other result
Where result and other result are evaluated for the value they return. This can very useful when you need an if in a place where normally an if is prohibitied. You may need to add parenthesis depending on the situation. An example is below:

print "Hello my name is ".(defined $name ? $name : "Anonymous Monk")." +\n";
HTH

Yves
--
You are not ready to use symrefs unless you already know why they are bad. -- tadmc (CLPM)


In reply to Re: If /Else Structure when If is used in latter part of a line. by demerphq
in thread If /Else Structure when If is used in latter part of a line. by jerrygarciuh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.