1
votes

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.

1

1 Answers

3
votes

You would have used perldoc (either the program on your computer or the website) to find answers.

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

You don't have a list, you have an array (called @list). This array has one element, which is a reference to (another) array containing four strings: foo, bar, foo, orange. I doubt this is what you want:

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

seems more likely (this is an array containing four strings directly, without the additional nested array).

To build this up procedurally, you can do the following:

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

or:

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

Relevant perldoc pages for this would be perldoc perldata, perldoc perlreftut, and perldoc -f push.