Hi Monks!
I included a sample of this code I am working on to explain my concerns about the code. I am wondering if there is a more elegant and efficient way of doing what this sample code is showing.
Reason are the values in @locations that can be larger, and all the "IFs", they will be a very large array of code that could be hard to maintain every time one more element is added to this array @locations.
I was thinking building a hash with all possible $locs (see code) for each element in @locations and do the check using that. My problem is I am lost doing that.
Can any one help or suggest a more efficient way of doing this?
Thanks for the Help!
Here is the sample code for this:
#!/usr/bin/perl -w use strict; print "Content-type: text/html\n\n"; my @locations = qw(LA LB LC LD LE); # this list could have more values +, thats my concern with the rest of the code. my $c_position; foreach my $location(@locations) { # Let me explain this value,$test_loc will have a different value for +each element of @locations, # I just added this way to show how it would work. my $test_loc = "Park: SW at 10 position"; #this could be for LA #my $test_loc = "Park: NE at 21 position"; #this could be for LB #my $test_loc = "Park: E at 11 position"; #this could be for LC #my $test_loc = "Park: NW at 17 position"; #this could be for LD #my $test_loc = "Park: NE at 06 position"; #this could be for LE ... my $locs = substr $test_loc, 6,2; # maybe using regular expression wou +ld be more efficient? $locs=~s/\s+//; if( ($location=~/LA/ig) && ( ($locs eq "NE") || ($locs eq "E") || ($locs eq "SE") || ($locs eq "S") || ($locs eq "SW") )) { $c_position=$c_position." 1LA= NE|E|SE|S|SW"; } elsif( ($location=~/LB/ig) && ( ($locs eq "NE") || ($locs eq "SW") || ($locs eq "W") || ($locs eq "NW") )) { $c_position=$c_position." 2LB= NE|SW|W|NW"; } elsif( ($location=~/LC/ig) && ( ($locs eq "SE") || ($locs eq "S") || ($locs eq "SW") )) { $c_position=$c_position." 3LC= SE|S|SW"; } elsif( ($location=~/LD/ig) && ( ($locs eq "SE") || ($locs eq "S") || ($locs eq "SW") )) { $c_position=$c_position." 4LD= SE|S|SW"; } else { $c_position=$c_position." 5ALL OTHERS HERE."; } } print "Results= $c_position\n\n"; exit;

In reply to Code Efficiency and Dynamic Help! by Anonymous Monk

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.