Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

random variable declaration based on given count

by shan_emails (Beadle)
on Mar 26, 2014 at 11:07 UTC ( #1079801=perlquestion: print w/ replies, xml ) Need Help??
shan_emails has asked for the wisdom of the Perl Monks concerning the following question:

Hi Team

I need to generate and declare the random variable based on the given count. I have tried like below but it throws error like below

"Can't modify concatenation (.) or string in scalar assignment at test +pl.pl line 16, near "$n;" Execution of testpl.pl aborted due to compilation errors."
And the code is
#!/usr/bin/perl use strict; my $txt='some'; my $arg_cnt=3; my ($rv1,$rv2,$rv3)=&get_val($arg_cnt); print "rv1:$rv1\n"; print "rv2:$rv2\n"; print "rv3:$rv3\n"; sub get_val { my $cnt = shift; # generate random and declare variable depends on $cnt value my $random_var; my ($var,$var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8,$var9,$va +r10,$var11,$var12,$var13); for my $n (1..$cnt){ $var.$n = $n; $random_var .= '$var'."$_".','; } $random_var =~ s/\,$//; print "get_var: $random_var\n"; return($random_var); }
I would appreciate some help
Please help me in fixing this. Awaiting for your valuable reply.
Thanks.

Comment on random variable declaration based on given count
Select or Download Code
Re: random variable declaration based on given count
by Anonymous Monk on Mar 26, 2014 at 11:12 UTC
Re: random variable declaration based on given count
by hippo (Curate) on Mar 26, 2014 at 11:15 UTC

    Seeing something like this:

    my ($var,$var1,$var2,$var3,$var4,$var5,$var6,$var7,$var8,$var9,$var10,$var11,$var12,$var13);

    raises the obvious question. Why are you not using an array instead? If you do that, likely most of the related problems would disappear.

Re: random variable declaration based on given count
by Bloodnok (Vicar) on Mar 26, 2014 at 11:30 UTC
    rand would seem to be your friend e.g. sub get_val { my $n = int(rand(shift())); return "var$n" } - added to which, I would question the naming of the sub - given that it is attempting to return a var name c/w a val ?

    A user level that continues to overstate my experience :-))
Re: random variable declaration based on given count
by Anonymous Monk on Mar 26, 2014 at 13:12 UTC
    'I need to... declare the random variable based on the given count'. That 1) can't work when you 'use strict' 2) you don't need to do that. Also, this
    $var.$n = $n;
    makes compiler crash. Maybe try to tell us what you're really trying to do, and why. How about some examples.
Re: random variable declaration based on given count
by AnomalousMonk (Abbot) on Mar 26, 2014 at 13:16 UTC

    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".

      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

        PHP has 'variable variables', which work just like in Perl.
        "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

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1079801]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-09-19 02:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls