Click to See Complete Forum and Search --> : Perl: prob w/ variable variable
Salmon
03-27-2001, 10:07 AM
Hi all-
I'm having trouble with using a variable variable in a perl script that I'm writing. Here's the relevent code snippet.
use strict;
# . . .
#lots of other stuff that's irrelavant
# . . .
if ($cardType == 2) {
$cardType = 1;
} else {
$cardType = 2;
}
$currentCellColor = ${ ("cardColor" . $cardType) };
The error I get is this . . .
'Can't use string ("cardColor2") as a SCALAR ref while "strict refs" in use at' . . .
What I'd like to do is to use either the variable $cardColor1 or $cardColor2, depending on the value of $cardType. I can easily come up with a work around, but at the same time I'd also like to find out what I'm doing wrong in this situation.
Also, as an aside, why does this
($cardType == 2) ? $cardType = 1 : $cardType = 2;
Always end up with the value of $cardType being 2?
Thanks for any help.
[ 27 March 2001: Message edited by: Salmon ]
TheLinuxDuck
03-27-2001, 10:26 AM
Originally posted by Salmon:
$currentCellColor = ${ ("cardColor" . $cardType) };
[/CODE]
What I'd like to do is to use either the variable $cardColor1 or $cardColor2, depending on the value of $cardType.
I've never really understood the referencing like this.. I generally avoid it if I can.. my first thought was, why doesn't the code just simply use any array for cardColor? That would seemingly solve the problem.
$currentCellColor = $cardColor[$cardType];
That seems alot easier.. IMHO. :)
Also, as an aside, why does this
($cardType == 2) ? $cardType = 1 : $cardType = 2;
Always end up with the value of $cardType being 2?
It's my experience when using this conditional that it returns the values, instead of simply making assignments. Try it like:
$cardType=$cardType==2?1:2;
That should work just fine. :)
eXtremist
03-27-2001, 10:45 AM
If you're using use strict; then you should declare all your variables using the my() function. You may have done this, but if you never, you could get an error. I was writing a program for school and kept getting a strange error -- it was because I forgot to declare one of my variables.
Try commenting out the use strict; and see if it works after that.
jemfinch
03-27-2001, 10:46 AM
"use strict" won't allow you to use symbolic references (rightfully so, beacuse they're horrid.) If you insist on using symbolic references, you'll have to use "no strict 'refs'" (iirc the syntax) before you plan to use them.
Really, though, you should use a hash, if you ask me.
Jeremy
Salmon
03-27-2001, 11:53 AM
I've never really understood the referencing like this.. I generally avoid it if I can.. my first thought was, why doesn't the code just simply use any array for cardColor? That would seemingly solve the problem..
Yeah, an array is the fix that I'm using now to get it to work. I based on this and jemfinch's comment then that will be the way I keep it.
It's my experience when using this conditional that it returns the values, instead of simply making assignments.
Thanks for the tip and code snippet. It works great.
Salmon
03-27-2001, 11:56 AM
Originally posted by jemfinch:
"use strict" won't allow you to use symbolic references (rightfully so, beacuse they're horrid.)
"Horrid" as in they have a tendency to get confusing, or as in they are poor programming style?
TheLinuxDuck
03-27-2001, 12:26 PM
Salmon, I think he means poor programming style.
Now that jemfinch has said this, I remember reading about the use strict thing (in disallowing that type of referencing).
jemfinch, is there any particular reason why you would suggest a hash, when simply dealing with numbers, instead of an array?
YaRness
03-27-2001, 12:45 PM
Originally posted by Salmon:
"Horrid" as in they have a tendency to get confusing, or as in they are poor programming style?
i think i can safely say on jem's behalf, and my own, that he means horrid as in plain-old butt-ugly. and in most cases poor style as well.
and beware of the duck: he'll talk you into thinking endless dereferences hashes of arrays of references to references of etc. are fun :eek: :D
maybe he means some hash where you would do something like $color{$cardtype} and it would return the proper color.
Salmon
03-27-2001, 08:07 PM
Thanks for the help and tips everyone.
jemfinch
03-28-2001, 01:11 AM
Originally posted by Salmon:
"Horrid" as in they have a tendency to get confusing, or as in they are poor programming style?
Isn't that the same thing?
Jeremy