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

help with syntax, if anyone could tell me this syntax will be ok.$MetaTag_$j is a dynic string, I try to search character from these two string. but when I perform ($MetaTag_$j !=~ /description=/gi) seems not working right.
for my $j(1,2){ if (($MetaTag_$j !=~ /description=/gi) && ($MetaTag_$j =~ /posted=/gi) +) { print <<END_OF_HTML; <font size=-1 color="#000000" face=arial,sans-serif>$Summary<br></font +> END_OF_HTML } }

Replies are listed 'Best First'.
Re: Can I do this?
by jarich (Curate) on Jan 10, 2002 at 04:31 UTC
    Looks to me that what you're trying to do is this:
    if( ( ${"MetaTag_$j"} !~ /description=/gi) && ( ${"MetaTag_$j"} =~ /posted=/gi )) { ... }
    Now the honest answer, is yes, you can use symbolic references, but you really really really don't want to.

    Now how to avoid it, depends lots on how the $MetaTag_* variables are being generated. Are they coming in with CGI? Perhaps you could use an array?

    my @MetaTags = (.....); foreach my $tag (@MetaTags) { if(($tag !~ /description/gi) && $tag =~ /posted=/gi)) { print qq{<font size=-1 color="#000000" face=arial,sans-serif>$ +Summary<br></font>}; } }
    Note also that I use !~ not !=~. !=~ will not work. !~ says this string does not match this pattern. =~ says this strings will match this pattern.

    I use qq{...} instead of a here document because I feel that they look nicer.

    For reasons why you don't want to use symbolic references look no further than that search button up the top. Really, there are way too many good posts on why for me to repeat them. Look here for an interesting start.

Re: Can I do this?
by particle (Vicar) on Jan 10, 2002 at 03:59 UTC
    perhaps you mean to evaluate $MetaTag_$j.

    try
    join('_',$MetaTag,$j)
    or
    $MetaTag. '_' . $j
    in it's place.

Re: Can I do this?
by Flame (Deacon) on Jan 10, 2002 at 03:30 UTC
    I can't say for sure, but last I checked, you can't use $ inside a variable name.

    Edit: Essentially, you are saying you want "$MetaTag_" and "$j" with no operator between them...


    -----BEGIN GEEK CODE BLOCK-----
    Version: 3.12
    GIT d- s:++ a--- C++++ UL P+++>++++ L+ E- W++>+++ N !o K- w+ O---- M-- V--
    PS PE Y- PGP t++(+++) 5(+++)++++ X R+@ tv+ b+++ DI+ D- G e->+++ h! r-- y-
    ------END GEEK CODE BLOCK------
    Translate

    "Weird things happen, get used to it."

    Flame ~ Lead Programmer: GMS

      Nope you can't have a $ in your variable name.... The camel book states: "Scalar variable names begin with a dollar sign followed by a letter, and then possibly more letters, or digits, or underscores".