Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi.

I need a more efficient way of doing the task I am working on.
fields => { 'field1' => 'value', 'field2' => 'value', 'checkfield' => '1, 2, 3' }
The above code fails in WWW::Mechanize because you can't call a field and give it multiple values- I've tried that thinking it would work. I need a way to make checkfield work with multiple values so more than one checkbox is selected at once OR dynamically make the necessary checkfield fields to make it work. Yes, I posted this question a few days ago but I put more thought into this and I can get a solution to work but it's not clean or efficient.

In short: I have an array full of values that I need to inject into checkfield. I never know how many or what they are until runtime so a method of injecting multiple values at once ( 'checkfield' => '1, 2, 3' ) would be perfect! But since I couldn't get that to work I did..
@checks = (1, 3, 5); #just inserting some values my $cnt = 0; foreach (@checks) { $cnt++; } if ($cnt == 1) { fields => { 'field1' => 'value', 'field2' => 'value', 'checkfield' => '1' } } elsif ($cnt == 2) { fields => { 'field1' => 'value', 'field2' => 'value', 'checkfield' => '1', 'checkfield' => '3' } } elsif ($cnt == 3) { fields => { 'field1' => 'value', 'field2' => 'value', 'checkfield' => '1', 'checkfield' => '3', 'checkfield' => '5' } }
Like I said, this way works if I use array values but it's very ugly. Can someone help me make a more efficient solution?

Replies are listed 'Best First'.
Re: help make code cleaner
by Corion (Patriarch) on Mar 23, 2011 at 18:42 UTC

    I highly doubt that your approach works at all. Have you checked that your approach works?

    Data structures can easily be checked using Data::Dumper:

    use strict; use Data::Dumper; print "Check 1\n"; print Dumper { 'field1' => 'value', 'field2' => 'value', 'checkfield' => '1' }; print "Check 2\n"; print Dumper { 'field1' => 'value', 'field2' => 'value', 'checkfield' => '1', 'checkfield' => '3' } ; print "Check 3\n"; print Dumper { 'field1' => 'value', 'field2' => 'value', 'checkfield' => '1', 'checkfield' => '3', 'checkfield' => '5' }

    If you compare the output with the code, you will see that your approach does not work and never will work, because the API you are trying to use does not allow duplicate elements.

Re: help make code cleaner
by jethro (Monsignor) on Mar 23, 2011 at 18:57 UTC

    Seem to me that

    foreach (@checks) { $m->tick('checkfield',$_); }

    should work.

Re: help make code cleaner
by Anonymous Monk on Mar 23, 2011 at 19:24 UTC