malaga has asked for the wisdom of the Perl Monks concerning the following question:

i'm trying to say that for each my $ref (@records ) if there is an entry for {qw/URL/}, then print 'Webpage'. i've been trying to say the "if" part in the first line, but not having any luck. here's what i have, but it prints 'webpage' whether or not it exists. (i've also tried if exists, btw)
for my $ref ( @records ) { print "<B>", 'Webpage:', ' ', "</B>"; print "<A HREF\= \"@{$ref}{qw/URL/}\">", @{$ref}{qw/URL/}, "</A>", + "<br>"; }

Replies are listed 'Best First'.
Re: if
by chipmunk (Parson) on Feb 13, 2001 at 23:58 UTC
    A simple approach:
    for my $ref ( @records ) { if ($ref->{URL}) { print qq{<B>Webpage:</B> <A HREF="$ref->{URL}">$ref->{URL}</A> +<br>}; } }
    But I think you're asking for an approach that puts the check for URL in the foreach line:
    for my $ref ( grep $_->{URL}, @records ) { print qq{<B>Webpage:</B> <A HREF="$ref->{URL}">$ref->{URL}</A><br> +}; }
    I combined the print statements into a single print of a single string, and used qq{} to avoid having to escape the double quotes.

    That's two ways to do it...

Re: if
by epoptai (Curate) on Feb 14, 2001 at 00:05 UTC
    You should carefully study the source code of working scripts for examples of looping and matching, you will learn a lot! In the meantime pay close attention to the difference between this and your original code which lacked the 'if' condition:
    foreach my $ref (@records){ if($ref=~/URL/){ print qq(<B>Webpage: </B><A HREF="$ref">$ref</A><br>); } }
      i know it. i was trying to follow other scripts, but i wasn't able to apply it here. sorry bout that. i still get confused sometimes when i'm dealing with hashes, especially when i'm being rushed, which i am. thanks.
Re: if
by MeowChow (Vicar) on Feb 14, 2001 at 00:13 UTC
    Is there any reason you are dereferencing your ref hash with a hash slice of one element?
    @{$ref}{qw/URL/}
    is just a convoluted way of saying:
    $ref->{URL}
    There may be some tricks to be played by returning a singleton list instead of a scalar, but I don't think you're looking for anything of that sort.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      I swear there's a book somewhere teaching people a Bondage and Discipline version of Perl. This convoluted access syntax has shown up time after time in posts.

      Instead, make it all cute and fluffy with curly braces:      @{${ref}}{map{qw{URL}}} Just kidding
      i knew that, and the code started out that way, but at a certain point i decided to leave it open for more than one field in each print line. but it doesn't make sense to me now and i just went in and changed them all. thanks.
(sacked) Re: if
by sacked (Hermit) on Feb 14, 2001 at 00:12 UTC
    Try replacing qw with q or just single quotes. qw is used to return a list of words, and q is equivalent to using single quotes. Also, because we're looking for a single element in the hash, you don't need the hash slice notation. Now we have:
    for my $ref ( @records ) { if ( $ref->{'URL'} ) { print qq(<B>Webpage:</B>) . qq(<A HREF="$ref->{'URL'}">$ref->{'URL'}</A><br>); } }

    --sacked
Re: if
by malaga (Pilgrim) on Feb 14, 2001 at 00:06 UTC
    perfect. i was trying every which way, but couldn't get the reference right. thanks a lot.