Vishal Khemani
Vishal Khemani

Reputation: 171

Passing array to a routine going wrong

In the following code I am expecting output in ScheduleRequestWrite() to be : 5,10

sub ProcessItem
{
    my @writeVal = ("5,10");

    foreach my $str (@writeVal)
    {
        print "\nProcessItem = $str\n";
    }
    ScheduleRequestWrite(\@writeVal);
}

sub ScheduleRequestWrite()
{
   my @write_value = $_[0];

   foreach my $str (@write_value)
   {
        print "\n$str\n";
   }
}

ProcessItem();

But I am getting : ARRAY<0x2ccf8>

Could anyone please help me point out my mistake. Thanks in advance!

Upvotes: 0

Views: 43

Answers (1)

Tanktalus
Tanktalus

Reputation: 22264

You're passing in an array reference, \@writeVal, and then using that reference in your array @write_value ... so your array @write_value has a single item in it, a reference to another array.

You may have meant my @write_value = @{$_[0]}; which makes a copy of the array, or you may have meant to loop over the original array directly:

sub ScheduleRequestWrite
{
   my $write_value = $_[0];

   foreach my $str (@$write_value)
   {
        print "\n$str\n";
   }
}

(You also don't want the () prototype since you are taking a parameter! Just leave the prototype off.)

Upvotes: 6

Related Questions