A newborn Perl developer here. I've been wracking my brain and searching all over online trying to figure this out... exasperated, I am come to you seeking clarity.
I have the following code (only relevant parts left in) as rest is working):
my @arrMissingTids;
@arrMissingTids = %hshTids;
my $missingtid;
foreach $missingtid (@arrMissingTids) {
print "$missingtid\n";
}
This works well, returning the values I want to be in the array:
500000246,500000235,500000185,500000237,500000227,500000252
However, when I pass this to a subroutine and include it in a variable name, it does not provide the list as written above, but rather just the number 1. Code for that is below:
myqry(@arrMissingTids);
sub myqry($) {
my $missingtids = @_;
$sql = "select
i.tid i_tid, i.name i_name
from
instrument i
where i.tid in ($missingtids)";
print "$sql/n";
}
Print $sql returns the following:
Select i.tid i_tid, i.name i_name from instrument i where i.tid in (1)
When I want it to return the following:
Select i.tid i_tid, i.name i_name from instrument i where i.tid in (500000246,500000235,500000185,500000237,500000227,500000252)
Thanks in advance for any pointers in the right direction!
The problem is right here:
my $missingtids = @_;
You're calling the array @_
in scalar context. That means that you're assigning to $missingtids
the number of elements in @_
. One way to work around this would be to pass an array reference:
sub myqry {
my $missingtids_ref = shift;
my @missingtids=@$missingtids_ref;
$sql = "select
i.tid i_tid, i.name i_name
from
instrument i
where i.tid in (" . join(",",@missingtids) . ")";
print "$sql/n";
}
For more information, take a look at perldoc perlref
and perldoc perldata
.