Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print array name 3

Status
Not open for further replies.

IVJP

Programmer
Jul 21, 2014
11
US
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)}
 
I tried the module mentioned above:
Code:
[COLOR=#804040][b]use warnings[/b][/color];
[COLOR=#804040][b]use strict[/b][/color];
[COLOR=#804040][b]use [/b][/color]Data::Dumper::Simple;

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$var_01[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]foo[/color][COLOR=#ff00ff]'[/color];

[COLOR=#804040][b]print[/b][/color] [COLOR=#008080]Dumper[/color]([COLOR=#008080]$var_01[/color]);
[COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$name[/color]) = [COLOR=#804040][b]split[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#6a5acd]\s[/color][COLOR=#6a5acd]*[/color][COLOR=#ff00ff]=[/color][COLOR=#804040][b]/[/b][/color], Dumper([COLOR=#008080]$var_01[/color]));
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]* variable name = '[/color][COLOR=#008080]$name[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array_01[/color] = [COLOR=#ff00ff]qw ([/color][COLOR=#ff00ff]1 2 3 4[/color][COLOR=#ff00ff])[/color];

[COLOR=#804040][b]print[/b][/color] [COLOR=#008080]Dumper[/color]([COLOR=#008080]@array_01[/color]);
([COLOR=#008080]$name[/color]) = [COLOR=#804040][b]split[/b][/color]([COLOR=#804040][b]/[/b][/color][COLOR=#6a5acd]\s[/color][COLOR=#6a5acd]*[/color][COLOR=#ff00ff]=[/color][COLOR=#804040][b]/[/b][/color], Dumper([COLOR=#008080]@array_01[/color]));
[COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]* variable name = '[/color][COLOR=#008080]$name[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];

Output:
Code:
$ perl varname.pl
$var_01 = 'foo';
* variable name = '$var_01'
@array_01 = (
              '1',
              '2',
              '3',
              '4'
            );
* variable name = '@array_01'
 

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:
my @pushedarray = @{shift};

Though as audiopro points out passing an array reference just to dereference it is pointless... try this

Code:
my @array1 = (1,2,3,4);

check(@array1);

sub check 
{
    my @pushedarray = shift;

    print @pushedarray; 
}

If you have multiple arguments eg...

Code:
sub my_function {

my ($arg1, $arg2, $arg3) = @_;

print "arg1 = $arg1, arg2 = $arg2, arg3 = $arg3";

}

my_function('This','Is','Fun');

Would output

arg1 = This, arg2 = Is, arg3 = Fun

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
 
@MikeLacey

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:
my ($index1, $index2, $index3) = @_;
That would have been wrong :p

"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
 

audiopro said:
Why not just pass the name of the array to your sub routine when you call it?

You mean to pass the name of the array and the array self like this?

Code:
[COLOR=#804040][b]use warnings[/b][/color];
[COLOR=#804040][b]use strict[/b][/color];

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]$var_01[/color] = [COLOR=#ff00ff]'[/color][COLOR=#ff00ff]foo[/color][COLOR=#ff00ff]'[/color];

print_var([COLOR=#ff00ff]'[/color][COLOR=#ff00ff]$var_01[/color][COLOR=#ff00ff]'[/color], [COLOR=#008080]$var_01[/color]);

[COLOR=#804040][b]my[/b][/color] [COLOR=#008080]@array_01[/color] = [COLOR=#ff00ff]qw ([/color][COLOR=#ff00ff]1 2 3 4[/color][COLOR=#ff00ff])[/color];

print_var([COLOR=#ff00ff]'[/color][COLOR=#ff00ff]@array_01[/color][COLOR=#ff00ff]'[/color], [COLOR=#008080]@array_01[/color]);

[COLOR=#0000ff]#-------------------------------------------[/color]
[COLOR=#804040][b]sub [/b][/color][COLOR=#008080]print_var [/color]{
  [COLOR=#804040][b]my[/b][/color] ([COLOR=#008080]$var_name[/color], [COLOR=#008080]@var_value[/color]) = [COLOR=#008080]@_[/color]; 
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]* variable name  = '[/color][COLOR=#008080]$var_name[/color][COLOR=#ff00ff]'[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color];
  [COLOR=#804040][b]print[/b][/color] [COLOR=#ff00ff]"[/color][COLOR=#ff00ff]           value =  [/color][COLOR=#008080]@var_value[/color][COLOR=#6a5acd]\n\n[/color][COLOR=#ff00ff]"[/color];
}

Output:
Code:
$ perl varname_simple.pl
* variable name  = '$var_01'
           value =  foo

* variable name  = '@array_01'
           value =  1 2 3 4

 
[lol] - yes! - I slightly miss-read Keith's post and saw it as pass the array!

I have done a little googling and perhaps PadWalker could help?



"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
 
Yes, pass the name to the sub routine or even simpler, print the array name before the sub routine call.

Code:
my @array1 = qw (1 2 3 4);

print 'Sending array - @array1 to sub Check';
Check(\@array1);

sub Check {
 my @pushedarray = @{$_[0]};
 print \@{$_[0]};
}

It depends why you need to output the array name.



Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top