in reply to $var$cnt problems

Instead of saying $fmail$cnt try saying ${"fmail$cnt"}. This uses references, but not the kind that involves an address of memory. Instead you're referring to a variable by name.

This kind of calling convention will break use strict however so it is something that I personally steer clear of.

Another approach would be to create a hash and store your fields in there e.g.:

my %fields = ( fname1 => 'John', fmail1 => 'john.lizmore@something.com', fname2 => 'Lisa', fmail2 => 'lisa.lizmore@something.com' );

Update: thanks to Transient for pointing out an error in my variable name.

Update2: merlyn says PLEASE do not suggest to people to use softrefs! I downvoted your node for that. How kind of merlyn to send me that message. I do want to point out that I believe merlyn did not read this article, as I also discourage the use of softrefs. As a result, to assist some of the skim-readers on this site, I have gone back and bolded the appropriate statement.

Replies are listed 'Best First'.
Re^2: $var$cnt problems
by sgifford (Prior) on Jul 08, 2005 at 18:44 UTC
    Or just use two arrays, since the subscripts are numeric.
    $fname[0]='John'; $fmail[0]='john.lizmore@something.com'; $fname[1]='Lisa'; $fname[2]='lisa.lizmore@something.com';
Re^2: $var$cnt problems
by merlyn (Sage) on Jul 09, 2005 at 08:11 UTC
    I do want to point out that I believe merlyn did not read this article, as I also discourage the use of softrefs. As a result, to assist some of the skim-readers on this site, I have gone back and bolded the appropriate statement.
    Correct. I read your first statement that says:
    Instead of saying $fmail$cnt try saying ${"fmail$cnt"}. This uses references, but not the kind that involves an address of memory. Instead you're referring to a variable by name.
    That uses softreferences. That's what I was downvoting you for. The fact that your statement had to be bolded later proves that it wasn't strong enough when I first skimmed it.

    Had you instead written as your first paragraph:

    There will be other posts in this thread that show you how to do this with soft references. Please ignore them. They are bad.
    You would have gotten an upvote instead of a downvote.

    Just something to note for the future. Do not suggest a broken solution, even if you "fix it" later in the same message.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.


    update:
    merlyn says PLEASE do not suggest to people to use softrefs! I downvoted your node for that.
    By the way, that was a private message to you, not a node. The fact that you took private communication and made it public is both (a) rude and (b) a copyright violation. Please don't do that again, and I won't have charges filed against you this time. {grin}
      You always have the option of not voting?

      I, personally, believe in understand what I am voting on.. arguably politics rarely works this way, but we are programmers so there's no reason for us not to understand what we are saying when we vote.

      I was not attempting to slander you, so I'm confused about the legal charges comment you made. I just wanted to point out the reason why I felt it necessary to bold face a portion of my short comment. You should feel proud that you gave me the inspiration to make judgements easier for those with short attention spans! I am only giving you credit, cheer up!

        I, personally, believe in understand what I am voting on
        I knew exactly what I was voting on. When skimmed, your article supported (until the phrase was bolded) softreferences. Those are bad. I completely understood what I was doing, and support it now as well. The fact that you altered your article after the fact is actually evidence to my claim.
        I was not attempting to slander you, so I'm confused about the legal charges comment you made.
        I wasn't talking about slander. I was talking about copyright. It's a copyright violation to take private mail and rebroadcast it without permission. I believe I was clear earlier when I said "(b) a copyright violation". What part of that did you miss?

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

      The fact that you took private communication and made it public is both (a) rude and (b) a copyright violation. Please don't do that again, and I won't have charges filed against you this time.

      In order to pursue an action against the copyright infringement, you will have to register the copyright. (You may hold the copyright without registration, but it is unactionable until registered.) In this case, how will you register for the copyright in a manner that could support the case for infringement?