LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-11-2007, 03:33 PM   #1
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Perl - returning array from a function


This is ridiculous. I'm trying to get the elements of an array returned from a function in Perl, and somewhere in here I'm missing the boat:
The parts which are actually relevant to this are bolded. I'm not interested in actually passing an array to the function, but rather in how to get ahold of the array the function returns.

The Example's version:
Code:
@a = (1, 2);
@b = (5, 8);
@c = add_vecpair( \@a, \@b );
print "@c\n";
6 10 

sub add_vecpair {       # assumes both vectors the same length
    my ($x, $y) = @_;   # copy in the array references
    my @result;

    for (my $i=0; $i < @$x; $i++) {
      $result[$i] = $x->[$i] + $y->[$i];
    }

    return @result;
}
My code:
Code:
@temp = my_function($var);
print "Temp: @temp.\n";
...

sub my_function {
	my $entry = shift;
	my @ret = [$val1, $val2, $val3];
	return @ret;
}
Instead of the neat list of values which the example promises will be printed, I get:

Temp: ARRAY(0x397ab4).

For some reason, passing arrays has always been a foil of mine. Can anyone give me a pointer or two in the right direction? Thanks!
 
Old 07-12-2007, 12:42 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
That's because you're creating an anonymous arrayref to be returned.
To initialise an array (which is what you seem to want) it's:

@array = ($var1, $var2);
 
Old 07-12-2007, 02:41 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
yes,
you can do it like that but references are scalars
Code:
#!/usr/local/bin/perl -w

my $temp = my_function();
print "Temp: @$temp.\n";

sub my_function {
    my $entry = shift;
    my ($val1, $val2, $val3) = qw(one two trois);
    my $ret = [$val1, $val2, $val3];
    return $ret;
}
the hard bits of perl are really understanding that the context of
an operation are vital, i.e. list, scalar, etc. which is weird compared to other languages
and then getting references, that's about it really.
It's really simple once you "get" it but for some reason it takes a while.

Last edited by bigearsbilly; 07-12-2007 at 02:42 AM.
 
Old 07-12-2007, 04:06 AM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by bigearsbilly
yes,
you can do it like that but references are scalars
Code:
#!/usr/local/bin/perl -w

my $temp = my_function();
print "Temp: @$temp.\n";

sub my_function {
    my $entry = shift;
    my ($val1, $val2, $val3) = qw(one two trois);
    my $ret = [$val1, $val2, $val3];
    return $ret;
}
Shouldn't that say:
Code:
#!/usr/local/bin/perl -w

my $temp = my_function();
print "Temp: @$temp.\n";

sub my_function {
    my $entry = shift;
    my ($val1, $val2, $val3) = qw(one zwei trois);
    my $ret = [$val1, $val2, $val3];
    return $ret;
}
:}



Cheers,
Tink
 
Old 07-12-2007, 04:41 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
hey, I'm the funny one

 
Old 07-12-2007, 09:29 AM   #6
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Original Poster
Rep: Reputation: 30
() instead of [] - foiled again!

Thanks very much guys, I can't believe I didn't spot that.

Cheers!
 
Old 07-13-2007, 01:02 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Yeah, it's a Perl quirk;

Init empty array, hash
@array = ();
%hash = ();

but assign (or access) using [], {}
$array[1] = "true";
$hash{1} = "true";
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with function returning pointers ***func() mlaich Programming 5 01-19-2006 09:17 PM
returning an array from a function.. javascript sonesay Programming 1 06-07-2004 05:28 AM
what are the Hexadecimal function and ASCII function in Perl Bassam Programming 1 06-03-2004 01:44 AM
Perl exec function in linux (and system-function) nazula Programming 1 04-19-2004 12:21 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration