开发者

Why does this subroutine work if I type out its arguments literally, but not if I give the arguments in the form of a variable?

I am using a perl package (Biomart), that includes a subroutine called addFilter(). That subroutine needs a couple of arguments, including one that needs to be of the format "nr:nr:nr"

If I use the s开发者_JAVA百科ubroutine as follows, it works fine:

$query->addFilter("chromosomal_region", ["1:1108138:1108138","1:1110294:1110294"]);

However, if I use it like this, it does not work:

my $string = '"1:1108138:1108138","1:1110294:1110294","1:1125105:1125105"';
$query->addFilter("chromosomal_region", ['$string']);

Since there are tens of thousands of those arguments that I construct in a for loop, I really need the second way to work... What could be causing this? I hope someone can help me out, many thanks in advance!


Because you seem to be trying to write in a language that's not Perl. '"this","that","another"' isn't an array, it's a string. And '$string' doesn't interpolate or include $string in any way because it uses single quotes. It just produces a string that starts with a dollar sign and ends with "string".

Something more like what you intend would be:

my @things = ("1:1108138:1108138","1:1110294:1110294","1:1125105:1125105");
$query->addFilter("chromosomal_region", \@things);

-or-

$query->addFilter("chromosomal_region", [ @things ] );

And to build it up dynamically, you can simply do push @things, $value in a loop or whatever you need.


'$string' is literally "$string"; the variable isn't replaced with its contents. Lose the single quotes.

Of course, it's unlikely passing a reference to an array consisting of a single comma-separated string with quotes embedded in it is going to do the same thing as passing a reference to an array of strings.

Try something like:

my $ref = ["1:1108138:1108138","1:1110294:1110294"];
$query->addFilter("chromosomal_region", $ref);


I agree with hobbs...if you want to take many inputs like that, you can use a for loop and an array like this (provided you are taking inputs from STDIN):

for ($line = <STDIN> && $line ne "end\n")
{   
    chomp($line);
    push @values,$line;
}                                                                                              

It takes data and puts in values array. You have to indicate the end of data by "end".

And for your error, what others said was right. Perl's variable interpolation works only for variables in double quotes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜