dereference an array problem

Discussion in 'Perl Misc' started by craigandjeanne, Jun 1, 2006.

  1. 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?
    craigandjeanne, Jun 1, 2006
    #1
    1. Advertising

  2. 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?
    craigandjeanne, Jun 1, 2006
    #2
    1. Advertising

  3. craigandjeanne

    Matt Garrish Guest

    "craigandjeanne" <> wrote in message
    news:...
    > 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?
    >
    Matt Garrish, Jun 1, 2006
    #3
  4. craigandjeanne

    Matt Garrish Guest

    "Matt Garrish" <> wrote in message
    news:HRqfg.2636$...
    >
    > "craigandjeanne" <> wrote in message
    > news:...
    >> 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
    Matt Garrish, Jun 1, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard
    Replies:
    0
    Views:
    382
    Howard
    Jul 1, 2003
  2. Jakob Bieling
    Replies:
    0
    Views:
    432
    Jakob Bieling
    Jul 1, 2003
  3. Srinu

    Two dimentional array dereference in C

    Srinu, Nov 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    971
    Barry Schwarz
    Nov 21, 2007
  4. Tomás Ó hÉilidhe

    Dereference an array pointer... UB?

    Tomás Ó hÉilidhe, Feb 11, 2008, in forum: C Programming
    Replies:
    33
    Views:
    966
    Chris Torek
    Feb 28, 2008
  5. ko
    Replies:
    3
    Views:
    130
Loading...

Share This Page