in reply to random variable declaration based on given count

Beyond the almost universal inadvisability of "using a variable as a variable name" (already discussed in links from above), I get the feeling from your code that you expect a newly-defined Perl scalar to have a random value. This is not the case. A newly-defined, uninitialized scalar has the very well-defined state of undef, the, well, undefined value. But it's not random! You may come from a C/C++ background and have in mind the initial state of, e.g., a stack variable, but Perl doesn't work that way. In addition to rand, try searching CPAN or MetaCPAN for "random number", "random number generator", "pseudo-random number generator" or "PRNG".

  • Comment on Re: random variable declaration based on given count

Replies are listed 'Best First'.
Re^2: random variable declaration based on given count
by davido (Cardinal) on Mar 26, 2014 at 16:05 UTC

    There certainly is a misunderstanding of the initial state of a variable which isn't explicitly initialized, but I doubt someone coming from a C or C++ background would come up with the idea of dynamically generating variables via symbolic references. There's nothing of the sort available in either of those languages. :)

    As a matter of fact, I wonder where newcomers come up with the tendency to gravitate toward symbolic references. They're not a part of the languages I grew up with such as Pascal and C. I don't know Java, but I don't get the impression that symbolic references are in common use in that language either.


    Dave

      "As a matter of fact, I wonder where newcomers come up with the tendency to gravitate toward symbolic references. They're not a part of the languages I grew up with such as ..."

      Given that a large proportion of these sort of questions seem to have declarations like:

      my ($var0, $var1, ..., $varN);

      I wonder if this is the result of learning array emulation in languages that don't support arrays.

      Here's a rather contrived Bourne shell (circa 1978) example:

      $ for i in 0 1 N; do eval var$i=x$i; done; echo "$var0 $var1 $varN" x0 x1 xN

      I've encountered more recent scripting languages (i.e. from this millenium) which don't support arrays and emulate them in much the same way by tagging a number (index) onto a base variable name.

      -- Ken

        So the question everyone wants to know now is what language encourages such "symbolic ref" practices, and doesn't have a predictable implicit initialization of variables? Because the original question in this thread seems to be grasping for symbolic refs, and seems to assume that variables not explicitly initialized will be "some random value" (which we know, even for languages such as C, isn't really random).

        This is just a muse out on a tangent, of course. ;)


        Dave

      As a matter of fact, I wonder where newcomers come up with the tendency to gravitate toward symbolic references. They're not a part of the languages I grew up with such as Pascal and C. I don't know Java, but I don't get the impression that symbolic references are in common use in that language either.

      Having done quite a bit of shell scripting at some point in the past, I was also once or twice tempted to use symbolic references in Perl, it seemed to me that constructing variable names might be practical in some cases. I quickly gave up the idea when I figured out that: 1. it can't be done with lexical variable; 2. it requires no strict refs; and 3. most knowledgeable people advise against it. I also found that, in the only specific case where I initially tried to use symbolic references, a hash of hashes would just do what I needed far better (and in a much clearer way).

      Other than various breeds of shells, DCL (a command language more or less equivalent to the shell under VMS) also has similar constructs (but, granted, this is not exactly a modern language). And, if I remember correctly from my use of the language in the late 1990s, TCL also has these things.

      ... I wonder where newcomers come up with the tendency to gravitate toward symbolic references. They're not a part of the languages I grew up with such as Pascal and C.

      I dunno. I came to Perl 4.x from C, and I remember reading about symbolic references in, I think, the first Randal Schwartz Camel book. I thought to myself "Gee, that ought to be good for something..."     And an evil ember kindled in my heart.

      PHP has 'variable variables', which work just like in Perl.