User Control Panel
Advertisements

HELP US, HELP YOU!

Using variable contents with defined

 
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl
View unanswered posts
Author Message
Xyem
Young One
Young One


Joined: 03 Jan 2005
Posts: 54

Reputation: 17.4Reputation: 17.4

PostPosted: Mon Mar 07, 2005 9:42 pm    Post subject: Reply with quote

I'm attempting to get my code to check if an array has been defined. However, the name of the array is inside a variable as a string and I am fairly adamant that the define is checking if the actual variable is defined and not the array.

Very very difficult to explain, hopefully showing you the code will help. (BTW this code isn't from the main code, it's just an example) And I know my formatting is different to everyone elses...

Code:
my $ArrayName = '@Test';<br />my @Test = ("Value");<br /><br />if (defined "$ArrayName")<br />     {print "Defined";}<br />else<br />     {print "Undefined";}<br />


The idea is that the defined checks that @Test is defined, whereas it seems to only check if $ArrayName is defined, as removing 'my @Test = ("Value");' still prints "Defined".

Any thoughts would be appreciated.
Back to top
mattaustin
Sentinel
Sentinel


Joined: 19 Jul 2004
Posts: 556
Location: Los Angeles, CA
Reputation: 50.7
votes: 1

PostPosted: Mon Mar 07, 2005 9:44 pm    Post subject: Reply with quote

should be:

if (defined @$ArrayName){

_________________
[ matt ]
Back to top
Xyem
Young One
Young One


Joined: 03 Jan 2005
Posts: 54

Reputation: 17.4Reputation: 17.4

PostPosted: Mon Mar 07, 2005 9:47 pm    Post subject: Reply with quote

Doesn't work...

if (defined @$ArrayName) always prints undefined
if (defined "@$ArrayName") always prints defined

Edit: Wrong way around
Back to top
Cer
Upgraded Agent
Upgraded Agent


Joined: 03 Feb 2004
Posts: 3776
Location: Michigan
Reputation: 146.9
votes: 4

PostPosted: Mon Mar 07, 2005 9:50 pm    Post subject: Reply with quote

Your best bet is to make an array reference.

Code:
my @array = ('a', 'b', 'c');<br />my $ref = \@array;<br /><br />if (defined @$ref) {<br />}


You could probably use eval, but that's a little more dangerous.

_________________
Current Site (2008) http://www.cuvou.com/
Back to top
..::BIGmouth( )::..
God Like
God Like


Joined: 05 Feb 2004
Posts: 801

Reputation: 44.1Reputation: 44.1Reputation: 44.1Reputation: 44.1

PostPosted: Mon Mar 07, 2005 10:01 pm    Post subject: Reply with quote

Just do something like..
Code:
my @Test = ('Test');<br />my $ArrayName = "@Test";<br />print "Defined!" . "\n" if (defined $ArrayName && length($ArrayName) != 0) || print ("Undefined!\n");
Back to top
Xyem
Young One
Young One


Joined: 03 Jan 2005
Posts: 54

Reputation: 17.4Reputation: 17.4

PostPosted: Mon Mar 07, 2005 10:05 pm    Post subject: Reply with quote

I've used your method Cer (make an array reference) and it works with checking for sub existance too with a little modification. I did use eval at first but then thought that eval'ing a subs name would run it... not appropiate. Thanks for the help!

However, one question that needs to be answered by Perl people. I was under the impression that ' ' means take it literally and " " meant take the values of any variables... therefore my first code should, theoretically, have worked. *shrugs*
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Mon Mar 07, 2005 10:15 pm    Post subject: Reply with quote

Yeah, Bigmouth, you're interpolating the array @Test inside that string, so it will always be defined. That's not what Xyem is asking for.

I believe Xyem is talking about using symbolic (or soft references). I was trying to find a link to more docs on perldoc, but the site is very slow right now.

Xyem, your first exmple should work if you remove the @ from the first line. And then do what mattaustin showed.


Symbolic references (from India?) lol
Back to top
Xyem
Young One
Young One


Joined: 03 Jan 2005
Posts: 54

Reputation: 17.4Reputation: 17.4

PostPosted: Mon Mar 07, 2005 10:38 pm    Post subject: Reply with quote

Should... but doesn't Razz
Back to top
draget
Not Yet a God
Not Yet a God


Joined: 29 Dec 2004
Posts: 367
Location: Australia
Reputation: 32.2Reputation: 32.2Reputation: 32.2

PostPosted: Tue Mar 08, 2005 9:29 am    Post subject: Reply with quote

post the code, then we can go further
Back to top
Mojave
Almost An Agent
Almost An Agent


Joined: 01 Nov 2003
Posts: 1434

Reputation: 66.4

PostPosted: Tue Mar 08, 2005 10:41 am    Post subject: Reply with quote

QUOTE(Xyem @ Mar 7 2005, 02:38 PM)
Should... but doesn't Razz
[right][snapback]46561[/snapback][/right]


Now it does:

Code:
my $ArrayName = "Test";<br />@Test = ("Value");<br /><br />if( defined @$ArrayName )  { print "Defined : @$ArrayName"; }<br />else           { print "Undefined"; }<br />


Apparently you can't make @Test a my variable. Not sure right and I'm too tired to think. Razz
Back to top
Xyem
Young One
Young One


Joined: 03 Jan 2005
Posts: 54

Reputation: 17.4Reputation: 17.4

PostPosted: Tue Mar 08, 2005 4:24 pm    Post subject: Reply with quote

Thanks Mojave. I like that better than the reference variable (less lines!)
And I'm quite happy with having them global as I will undef all the arrays at the end of that code block anyway *thumbs up*
Back to top
eric256
The Keymaker
The Keymaker


Joined: 03 May 2006
Posts: 2292
Location: Colorado
Reputation: 47Reputation: 47Reputation: 47Reputation: 47Reputation: 47

PostPosted: Tue Mar 08, 2005 5:01 pm    Post subject: Reply with quote

Normaly when this question is asked, the answer is "don't do it that way." I don't know why exactly you want to store the name of an array in a variable, there are some reason i'm sure, but i've never encountered them.

The best way is normaly to use a hash that stores ref's to arrayrs.

Code:
<br />my @array = (1 .. 10);<br />my $names = { array1 => \@array };<br /> # later <br />my @numbers = @{$names->{array1}};<br />


This might not be the solution to your problem, but if you want to be able to access different sets of data using the name in a variable then a hash is normaly the way to go.

_________________
Eric256
Proud previous owner and current admin of Bot-depot.com
Back to top
Xyem
Young One
Young One


Joined: 03 Jan 2005
Posts: 54

Reputation: 17.4Reputation: 17.4

PostPosted: Tue Mar 08, 2005 10:37 pm    Post subject: Reply with quote

Well when the code is finished, I will be posting it so then you can see why I needed it. Until then you'll just have to wait in suspense and wonder what kind of monster I am creating...

Thanks for all the help Smile
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Bot Depot Forum Index -> Perl All times are GMT
Page 1 of 1

 



Protected by phpBB Security phpBB-TweakS
phpBB Security Has Blocked 9 Exploit Attempts.
Antispam Captcha Mod by phpbb-security.com
Powered by phpBB © 2001, 2005 phpBB Group