Qtperlcode4u@myspace.com



  • This is just something I was messing around with while at work.  I'm a total noob, and constructed this from reading a few sections in a perl book I found on perl.org.  Could someone check this out and let me know if there's a more elegant way to achieve similar results?

    This code loads a bunch of variables into an array (list?), which is then checked to see if any of the elements are blank.  If an element does contain a string, it dumps that string into ANOTHER array, which formats the string with an html <br> tag at the end.

     So, what I end up with is an array (list?) holding the modified strings that I need to access later.

    Please take a look and let me know if there's a better way to do this.  I read a little on foreach loops but didn't have the time to really understand how they work.  I just went with what I was familiar with (a while loop).  Thanks.

    @array = ($Service, $Performance, $Innovation, $Respect, $Integrity, $Teamwork);

    my @list;
    my $c = 0;
    my $i = 0;

    while ($i < @array) {
        if ($array[$i] ne "") {
            $list[$c] = "$array[$i]<br>";
            $c++;
        }
        $i++;
    }
     



  • perl has builtin functions to handle lists without using location variables, so you can do this:

     

    my @array = ($Service, $Performance, $Innovation, $Respect, $Integrity, $Teamwork);

    my @list;

    foreach $entry (@array){

        if($entry ne ""){

            push @list, "$entry<br>"; 

        }else{

            # do nothing ( psst, you dont really need the else case, but I like them. )

        }

     

     

    From what I understand, foreach can be defined as follows:

     

    foreach $entry (@array) {

        // code goes here

    }

    is equal to

    for($i=0;$i<@array;$i++){

        $entry=$array[$i];

      //code goes here

    }
     

     




  • @fumoh said:

    Could someone check this out and let me know if there's a more elegant way to achieve similar results?

    TMTOWTDI. Always.

     

    This code loads a bunch of variables into an array (list?)

    Array. A list is a sequence of elements. An array is an object storing a list in a manner that permits retrieval of any element by its index. 

     

    which is then checked to see if any of the elements are blank

    grep {$_ ne ''} 

     

    If an element does contain a string, it dumps that string into ANOTHER array, which formats the string with an html <br> tag at the end.

    map {"$_<br>"}

     

    So, what I end up with is an array (list?) holding the modified strings that I need to access later.

    Please take a look and let me know if there's a better way to do this.  I read a little on foreach loops but didn't have the time to really understand how they work.  I just went with what I was familiar with (a while loop).  Thanks.

    @array = ($Service, $Performance, $Innovation, $Respect, $Integrity, $Teamwork);

    my @list;
    my $c = 0;
    my $i = 0;

    while ($i < @array) {
        if ($array[$i] ne "") {
            $list[$c] = "$array[$i]<br>";
            $c++;
        }
        $i++;
    }

    my @list = map {"$_<br>"} grep {$_ ne ''} ($Service, $Performance, $Innovation, $Respect, $Integrity, $Teamwork);

    As a general rule, any operation that constructs a list based on another list should not involve loops. If it's complicated, move the contents of the {} blocks into subs so you can still see what you're doing.



  • @coplate said:

    From what I understand, foreach can be defined as follows:

    foreach $entry (@array) {

        // code goes here

    }

    is equal to

    for($i=0;$i<@array;$i++){

        $entry=$array[$i];

      //code goes here

    }

    Eek! No. It's rather more like this:

    for(my $i=0; $i<@array; $i++) {
      my $x = \$array[$i];
      # All uses of the loop variable replaced with $$x in the body
    }
    

    Except that perl really does it with a typeglob alias. The loop variable is each cell in the loop array, not a copy of its value. You can write things like foreach my $x (@a) {$x++}, for those occasions when map won't cut it. Also, the current position is not remembered based on the index; the interaction between a foreach loop and modifications to the loop array from within the loop is rather more like iterating a linked list (although there are still some caveats).



  • Very nice.  Thanks for the breakdown, asuffield.  That map function is pretty awesome, and I didn't know that perl had a grep function either.  Now I just need to figure out how to do regular expressions ;P

     

    Thanks again.
     


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.