Loading ...
Sorry, an error occurred while loading the content.

[PBML] Re: Perl: Processing lists

Expand Messages
  • Frank Hale
    ... Thank you it works fine. I think if I would have applied myself a little more I could have come up with this. Its just I ve been working on some code for a
    Message 1 of 3 , Nov 12, 1999
    • 0 Attachment
      > #!/usr/bin/perl
      > #@desc has already been loaded with you description
      > #@link already has links
      > for ($i=0;$i<$#desc;$i++){
      > print "<a href=\"$link[$i]\">$desc[$i]</a><br>";
      > }
      > This might work for you, I didn't test it yet. I'm
      > at work.

      Thank you it works fine. I think if I would have
      applied myself a little more I could have come up with
      this. Its just I've been working on some code for a
      about 2 weeks now and its starting to blur my vision.

      Thank you!!!
      __________________________________________________
      Do You Yahoo!?
      Bid and sell for free at http://auctions.yahoo.com
    • Jeff Boes
      ... Ugh, that is certainly not what you want. If the lists are pre-loaded AND have the exact same number of elements, then try this: print join( ,
      Message 2 of 3 , Nov 12, 1999
      • 0 Attachment
        --- Frank Hale <frankhale@...> wrote:
        > Okay I have 2 lists, one which stores URL's and
        > another which stores the description of the URL's. I
        > want to build up HTML code for these links but if I
        > use the following code it loops for a very long time
        > creating multiple copies of the same links. How can
        > I
        > build up the list of links using the 2 lists?
        >
        > foreach $desc (@desc)
        > {
        > foreach $link (@links)
        > {
        > print "<a ref=\"$link\">$desc</a><br>";
        > }
        > }

        Ugh, that is certainly not what you want. If the lists
        are pre-loaded AND have the exact same number of
        elements, then try this:

        print join('<br>',
        map(qq!<a href="$link[$_]">$desc[$_]</a>!,
        0..$#link)
        );

        Deconstruction (this is after all a beginner's list!):

        join($str, @list) - this takes a list and produces a
        string, using $str as a separator. It's equivalent to:

        $list[0] . $str . $list[1] . $str . $list[2] ...

        map(expr, @list) - this is my vote for the most
        powerful Perl built-in. Once you figure out how map()
        can be used, you are well on your way to Perl
        wizardry. map() evaluates the expression once for each
        item in the @list. Within the expression it sets the
        variable $_ to each successive value of @list. The
        whole thing is returned as a list.

        So for example,

        @listb = map($_+1, @lista);

        sets @listb to the contents of @lista, but increments
        each element by one.

        qq!...! is nothing more than another way to write
        "...", a quoted string with interpolation, but without
        having to 'escape' all the internal double-quote
        marks.

        0..$#link is the list of all the valid subscripts of
        @link.



        =====
        Jeff Boes//ICQ=3394914//Yahoo!=jeffboes//AOL IM=jboes
        //home=jboes@...//professional=mur@...
        __________________________________________________
        Do You Yahoo!?
        Bid and sell for free at http://auctions.yahoo.com
      Your message has been successfully submitted and would be delivered to recipients shortly.