ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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?
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.
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 : )
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.
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]
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.