×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

print array name
3

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)}

RE: print array name

3
I tried the module mentioned above:

CODE

use warnings;
use strict;
use Data::Dumper::Simple;

my $var_01 = 'foo';

print Dumper($var_01);
my ($name) = split(/\s*=/, Dumper($var_01));
print "* variable name = '$name'\n";

my @array_01 = qw (1 2 3 4);

print Dumper(@array_01);
($name) = split(/\s*=/, Dumper(@array_01));
print "* variable name = '$name'\n"; 

Output:

CODE

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

RE: print array name

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

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

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

Quote:

arg1 = This, arg2 = Is, arg3 = Fun

Just my 2pence from having had my ear bent a few times winky smile

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

@1DMF

That should be $arg0 surely :)

Mike


RE: print array name

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

"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


Quote (audiopro)


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

use warnings;
use strict;

my $var_01 = 'foo';

print_var('$var_01', $var_01);

my @array_01 = qw (1 2 3 4);

print_var('@array_01', @array_01);

#-------------------------------------------
sub print_var {
  my ($var_name, @var_value) = @_; 
  print "* variable name  = '$var_name'\n";
  print "           value =  @var_value\n\n";
} 

Output:

CODE

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

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

RE: print array name

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
www.studiosoft.co.uk

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close