|
| Author |
Message |
Xyem Young One

Joined: 03 Jan 2005 Posts: 54
  
|
Posted: Mon Mar 07, 2005 9:42 pm Post subject: |
|
|
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

Joined: 19 Jul 2004 Posts: 556 Location: Los Angeles, CA
  votes: 1
|
Posted: Mon Mar 07, 2005 9:44 pm Post subject: |
|
|
should be:
if (defined @$ArrayName){ _________________ [ matt ] |
|
| Back to top |
|
 |
Xyem Young One

Joined: 03 Jan 2005 Posts: 54
  
|
Posted: Mon Mar 07, 2005 9:47 pm Post subject: |
|
|
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

Joined: 03 Feb 2004 Posts: 3776 Location: Michigan
  votes: 4
|
Posted: Mon Mar 07, 2005 9:50 pm Post subject: |
|
|
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

Joined: 05 Feb 2004 Posts: 801
    
|
Posted: Mon Mar 07, 2005 10:01 pm Post subject: |
|
|
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

Joined: 03 Jan 2005 Posts: 54
  
|
Posted: Mon Mar 07, 2005 10:05 pm Post subject: |
|
|
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

Joined: 01 Nov 2003 Posts: 1434
 
|
Posted: Mon Mar 07, 2005 10:15 pm Post subject: |
|
|
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

Joined: 03 Jan 2005 Posts: 54
  
|
Posted: Mon Mar 07, 2005 10:38 pm Post subject: |
|
|
Should... but doesn't  |
|
| Back to top |
|
 |
draget Not Yet a God

Joined: 29 Dec 2004 Posts: 367 Location: Australia
   
|
Posted: Tue Mar 08, 2005 9:29 am Post subject: |
|
|
| post the code, then we can go further |
|
| Back to top |
|
 |
Mojave Almost An Agent

Joined: 01 Nov 2003 Posts: 1434
 
|
Posted: Tue Mar 08, 2005 10:41 am Post subject: |
|
|
QUOTE(Xyem @ Mar 7 2005, 02:38 PM) Should... but doesn't  [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.  |