print array name
print array name
(OP)
How do I return an actual array name? This doesn't work, I just want perl to return "array1" when executed.
my @array1 = qw (1 2 3 4);
&Check(\@array1);
sub Check {
@pushedarray = @{$_[0]};
print \@{$_[0]};
}
Result:
@{ARRAY(0x18a62ca0)}
my @array1 = qw (1 2 3 4);
&Check(\@array1);
sub Check {
@pushedarray = @{$_[0]};
print \@{$_[0]};
}
Result:
@{ARRAY(0x18a62ca0)}
RE: print array name
RE: print array name
CODE
Output:
CODE
RE: print array name
Keith
www.studiosoft.co.uk
RE: print array name
A couple of things I would note.
1. Not sure what version of Perl you are running, but it is considered bad practice to call subroutines using an ampersand. Just use... Check(\@array1);
2. I have been told off many times in Perl IRC for using capitals / camel case for sub routine names, apparently is should be all lowercase with underscores as word separators.
3. It is considered bad to access the in-built array using the index as you have it for argument / parameter mapping so not $_[0] , try...
CODE
Though as audiopro points out passing an array reference just to dereference it is pointless... try this
CODE
If you have multiple arguments eg...
CODE
Would output
Just my 2pence from having had my ear bent a few times
Though it doesn't' answer your "how do I get the name of the variable".
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
RE: print array name
That should be $arg0 surely :)
Mike
RE: print array name
Well it all depends on your point of view; it's the first argument, but index zero of the inbuilt array.
Plus in Perl OOP as 'self' is always passed to a subroutine as the first argument, $_[0] = self, $_[1] = first parameter argument passed to the method.
If I had said
CODE
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
RE: print array name
You mean to pass the name of the array and the array self like this?
CODE
Output:
CODE
RE: print array name
I have done a little googling and perhaps PadWalker could help?
http://search.cpan.org/~robin/PadWalker-1.98/PadWa...
"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."
"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"
Free Electronic Dance Music
RE: print array name
CODE
It depends why you need to output the array name.
Keith
www.studiosoft.co.uk