2

I still struggle with the most basic Perl syntax and googling operators is near impossible unless you know what you don't know so you can use the right terms. So 2 questions, 1) what is the right syntax and 2) what are the terms I would have used to find the answer?

The syntax -- I have a list of something (hashes?):

my @list = [ "foo"=> "bar", "foo"=>"orange"];

I need to declare the list and then add each item individually (filling will be done in a loop and other methods), but cannot seem to find the right syntax:

my @list = [];
# add foo=bar
# add foo=orange

The end goal is to post a form that unfortunately uses duplicate keys via LWP::UserAgent and the $ua->post( $url, \@form ) method. I can get it to work declaring the list and all contents in one go, but can't seem to find the right syntax for splitting it up and building the contents incrementally.

4

1 に答える 1

4

答えを見つけるために、perldoc (コンピューター上のプログラムまたはWeb サイトのいずれか) を使用したことでしょう。

my @list = [ "foo"=> "bar", "foo"=>"orange"];

リストはありませんが、配列があります ( と呼ばれます@list)。fooこの配列には、4 つの文字列を含む (別の) 配列への参照である 1 つの要素がbarありfooますorange。私はこれがあなたが望むものだとは思わない:

my @list = ("foo" => "bar", "foo" => "orange");

可能性が高いようです (これは、追加のネストされた配列なしで、4 つの文字列を直接含む配列です)。

これを手続き的に構築するには、次の操作を実行できます。

my @list;
push @list, "foo";
push @list, "bar";
push @list, "foo";
push @list, "orange";

また:

my @list;
push @list, "foo" => "bar";
push @list, "foo" => "orange";

これに関連する perldoc ページは、、、perldoc perldataおよびperldoc perlreftutですperldoc -f push

于 2012-12-24T03:03:02.680 に答える