LinuxQuestions.org
Review your favorite Linux distribution.
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 05-22-2006, 10:23 AM   #1
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
(Perl) Dereferencing arrays for subroutines


I'm trying to pass an array into a subroutine, but though the routine is small enough to be copied every time, proper coding suggets I use references. The difficulty resides in the fact that for some reason, my dereferencing isn't acting as I expect.

(simplified) example code follows:
Code:
@ref = 1..5;
@rray = &check(\@ref, "Apple");

sub check {
   $temp = shift;
   @temp = @{$temp};
   $apple = shift;
}
If I add in a print $temp, I get the expected memory reference for the array. However if I add in a print @temp I get a return of udef, which is certainly not what I expected.

Is my understanding of dereferencing just faulty or is something else going on here? Thanks in advance.
 
Old 05-22-2006, 01:53 PM   #2
destuxor
Member
 
Registered: Oct 2005
Posts: 51

Rep: Reputation: 16
I think your problem is in your usage of the "shift" function. Ordinarily, if I wanted to copy the array sent to a function I would just copy it like this:
Code:
sub check {
   @temp = @_;
   $apple = pop(@temp);
}
The below code,
Code:
   $temp = shift;
   @temp = @{$temp};
puts $_[0] into $temp, then attempts to create an array with a single element $temp in position zero - I think.
Does that answer your question?
 
Old 05-22-2006, 02:18 PM   #3
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Code:
15:16 aluser@alf:~/test/perl$ cat arrpass.pl
#! /usr/bin/perl

@ref = 1..5;
@rray = &check(\@ref, "Apple");

sub check {
        $temp = shift;
        @temp = @{$temp};
        $apple = shift;

        print "\$temp: $temp\n";
        print "\@temp: @temp\n";
}
15:16 aluser@alf:~/test/perl$ perl arrpass.pl
$temp: ARRAY(0x8152678)
@temp: 1 2 3 4 5
Works fine for me, I think your problem is somewhere else : )

destruxor: "@temp = @{$temp}" says to take $temp as an array reference, dereference it, and copy the result to @temp. $temp and @temp are two separate variables.
 
Old 05-22-2006, 02:19 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
btw, variable names like "temp" will get you into big trouble eventually if you don't localize them with "my". Use "use strict" always, and especially when debugging things : )
 
Old 05-22-2006, 03:36 PM   #5
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Original Poster
Rep: Reputation: 49
Quote:
Originally Posted by aluser
btw, variable names like "temp" will get you into big trouble eventually if you don't localize them with "my". Use "use strict" always, and especially when debugging things : )
Don't worry, that's far from the code in my program
destuxor, I don't think that's right -- $_[0] contains the first element of @_, which is the reference to the array.

The actual relevant code is posted below, taken from my project (slight immeterial edits)... I'm really wondering why this isn't working

Code:
if (@ns) { @txtns = &roundup(\@check, "NS"); }

sub roundup {
     $array = shift @_;
     @array = @{$array};
     $string = shift @_;
     foreach (@array) {
        @splits = split("--");
        push @c, "[Misc information here using the value of $i]";
        $i++;
     }
     $i = 0;
     @c;
}
I've put print statements (or 'push'd an array that was printed later) just after the first shift and then after the @array assignment, but I run into @array being empty for some reason, and as such, it never runs the foreach loop so @c is never added to.
 
Old 05-23-2006, 02:53 AM   #6
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
I always do it the other way around:
Code:
calledsub($scalr, $scalar ... , @array)
sub calledsub
{
  $t1 = shift;
  $t2 = shift;
  .
  .
  .
  @array = @_;
}
BTW, using '&' to call a sub is old/deprecated unless passing a ref-to-sub; just use the sub name.
 
Old 05-23-2006, 03:51 PM   #7
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Code:
16:50 aluser@alf:~/test/perl$ cat arrpass.pl
#! /usr/bin/perl
@ns = (1, 2);
@check = 1..4;
if (@ns) { @txtns = &roundup(\@check, "NS"); }
print "@txtns\n";
sub roundup {
     $array = shift @_;
     @array = @{$array};
     $string = shift @_;
     foreach (@array) {
        @splits = split("--");
        push @c, "[Misc information here using the value of $i]";
        $i++;
     }
     $i = 0;
     @c;
}
16:51 aluser@alf:~/test/perl$ perl arrpass.pl
[Misc information here using the value of ] [Misc information here using the value of 1] [Misc information here using the value of 2] [Misc information here using the value of 3]
Once again, I think your problem is elsewhere...
 
Old 05-23-2006, 05:27 PM   #8
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Original Poster
Rep: Reputation: 49
Thanks for the help

I'll keep poking at it. For some reason nothing in the [misc information] block is getting to @txtns.
 
  


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
perl: concatenate arrays jrtayloriv Programming 2 01-23-2005 07:13 AM
PERL : Arrays confusing? fredgt Programming 3 12-19-2004 08:24 AM
merging arrays suing perl pantera Programming 1 06-03-2004 08:51 AM
dereferencing anonymous arrays in Perl coolman0stress Programming 6 10-27-2003 10:17 PM
arrays in perl BBQ_Matt Programming 3 09-23-2003 07:45 AM

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

All times are GMT -5. The time now is 11:42 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