Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > dereference an array problem

Reply
Thread Tools

dereference an array problem

 
 
craigandjeanne
Guest
Posts: n/a
 
      06-01-2006
Hi,

The following code, I thought, should have worked properly for looping
through an array passed as a reference to a function. But, instead I
end up with the error message: "Can't use string ("string") as an ARRAY
ref while "strict refs" in use at ./test.pl line 29, <FILE> line 2. The
following is a cut down version of the actual code.


my @list = ( 1, 2, 3 );

server_work(@list);
exit;

sub server_work {
my ($a_ref) = shift;
my $val;
foreach $val (@{$a_ref}) {
print STDOUT $val;
}
return(1);
}

What am I doing wrong?

 
Reply With Quote
 
 
 
 
craigandjeanne
Guest
Posts: n/a
 
      06-01-2006
Found my own problem forgot to send it as a reference to the
subroutine. Doh!

server_work(\@list);


craigandjeanne wrote:
> Hi,
>
> The following code, I thought, should have worked properly for looping
> through an array passed as a reference to a function. But, instead I
> end up with the error message: "Can't use string ("string") as an ARRAY
> ref while "strict refs" in use at ./test.pl line 29, <FILE> line 2. The
> following is a cut down version of the actual code.
>
>
> my @list = ( 1, 2, 3 );
>
> server_work(@list);
> exit;
>
> sub server_work {
> my ($a_ref) = shift;
> my $val;
> foreach $val (@{$a_ref}) {
> print STDOUT $val;
> }
> return(1);
> }
>
> What am I doing wrong?


 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      06-01-2006

"craigandjeanne" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> Hi,
>
> The following code, I thought, should have worked properly for looping
> through an array passed as a reference to a function. But, instead I
> end up with the error message: "Can't use string ("string") as an ARRAY
> ref while "strict refs" in use at ./test.pl line 29, <FILE> line 2. The
> following is a cut down version of the actual code.
>
>
> my @list = ( 1, 2, 3 );
>
> server_work(@list);


You aren't passing an array ref here, you're passing the array.

> exit;
>
> sub server_work {
> my ($a_ref) = shift;


Therefore, this assignment gets the first value of the array.


> my $val;
> foreach $val (@{$a_ref}) {
> print STDOUT $val;
> }
> return(1);
> }
>
> What am I doing wrong?
>



 
Reply With Quote
 
Matt Garrish
Guest
Posts: n/a
 
      06-01-2006

"Matt Garrish" <(E-Mail Removed)> wrote in message
news:HRqfg.2636$(E-Mail Removed).. .
>
> "craigandjeanne" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
>> Hi,
>>
>> The following code, I thought, should have worked properly for looping
>> through an array passed as a reference to a function. But, instead I
>> end up with the error message: "Can't use string ("string") as an ARRAY
>> ref while "strict refs" in use at ./test.pl line 29, <FILE> line 2. The
>> following is a cut down version of the actual code.
>>
>>
>> my @list = ( 1, 2, 3 );
>>
>> server_work(@list);

>
> You aren't passing an array ref here, you're passing the array.
>
>> exit;
>>
>> sub server_work {
>> my ($a_ref) = shift;

>
> Therefore, this assignment gets the first value of the array.
>


And thanks to a slip on the keyboard off went that message. To finish, to
pass a reference use a slash in front of the array:

server_work(\@list);

Matt


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dereference an array pointer... UB? Tomás Ó hÉilidhe C Programming 33 02-28-2008 10:01 AM
Two dimentional array dereference in C Srinu C Programming 10 11-21-2007 04:22 AM
lexical declaration and array dereference ko Perl Misc 3 08-27-2003 07:43 AM
Re: how to properly dereference STL list item Howard C++ 0 07-01-2003 05:46 PM
Re: how to properly dereference STL list item Jakob Bieling C++ 0 07-01-2003 05:45 PM



Advertisments