Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I see what you mean, that this is a "dangling else" clause.

I guess something like:

$trimmedcolr = $colr; $trimmedcolr = 'BB' if $colr eq 'baby blue'; $trimmedcolr = 'BP' if $colr eq 'baby pink'; $trimmedcolr = 'DB' if $colr eq 'dark blue'; $trimmedcolr = 'DP' if $colr eq 'dark purple'; $trimmedcolr = 'HP' if $colr eq 'hot pink'; $trimmedcolr = 'LP' if $colr eq 'light purple';
would have worked although the hash based solutions in this thread are probably more efficient. BTW, I have no problem with this trailing "if" syntax in a situation like this were the intent and readability is very clear.

The "why" question wasn't asked. However since Perl is so good at processing strings, I would suggest that translating an easily understandable string into a shorter more cryptic string is usually just not necessary or advisable.

I would not do this translation without a good reason. One reason might be that this program talks to something else that only understands the two letter abbreviations, however in that case, the default of not abbreviating the color doesn't appear to make sense. Making this a subroutine and returning an error in the case of an unknown color might make sense. I don't know what this "abbreviate it if you can" idea accomplishes.

Update:
Without a good reason, I wouldn't do this, but consider this:

#!/usr/bin/perl -w use strict; my @colors = ( 'baby blue', 'baby pink', 'dark blue', 'dark purple', 'hot pink', 'light purple', 'wild green', 'crazy yellow', 'wild crazy purple', 'deep purple'); foreach (@colors) { print getColorAbreviation($_),"\n"; } # The translation algorithm appears to be straight- # forward, so a table independent translation is # possible ... Of course 'dark purple' and 'deep purple' # would translate into the same thing, but maybe that is # ok? This is application dependent. sub getColorAbreviation { my $color = shift; #like: 'wild crazy purple' $color = uc $color; my @FirstCaps = $color =~ /(\w)\w+/g; return @FirstCaps; #like: WCP } __END__ BB BP DB DP HP LP WG CY WCP DP

In reply to Re^3: If statement not working by Marshall
in thread If statement not working by perlnoobster

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-03-28 21:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found