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

Creating a Variable Name ???

Expand Messages
  • alex2100k
    how to construct a variable call from text string + second variable? hi, i m trying to call a variable from a loop in which the variables name is constructed
    Message 1 of 2 , Jul 31 9:13 AM
    • 0 Attachment
      how to construct a variable call from text string + second variable?

      hi, i'm trying to call a variable from a loop in which the variables
      name is constructed using the current count rather than being written
      in from the outset, the kind of code that i'm trying to write would
      be something like this:

      #!/usr/bin/perl -w

      $var1 = "one";
      $var2 = "two";
      $var3 = "three";

      $total = 3;


      for($count=1; $count<=$total; $count++) {
      $varx = '$var' . $count;
      print "$varx";
      }

      but rather than printing '$var1$var2$var3' i want it to print the
      contents of those variables, so it should print 'onetwothree', i
      couldn't find the answer in the perl books, probably because don't
      know the keyword to look for, or else just one of those things that's
      so simple it's assumed you know already, so any advice would be
      gratefully received.

      thanks in advance,

      Alex.
    • Jenda Krynicky
      From: alex2100k ... Don t do that. Use either an array or a hash. See Why it s stupid to `use a variable as a variable name :
      Message 2 of 2 , Aug 5, 2003
      • 0 Attachment
        From: "alex2100k" <alex2100k@...>
        > how to construct a variable call from text string + second variable?

        Don't do that. Use either an array or a hash.

        See Why it's stupid to `use a variable as a variable name' :
        http://www.plover.com/~mjd/perl/varvarname.html

        Jenda
        ===== Jenda@... === http://Jenda.Krynicky.cz =====
        When it comes to wine, women and song, wizards are allowed
        to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery
      Your message has been successfully submitted and would be delivered to recipients shortly.