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

Hi, This code prints the field names in the forms of a page. It prints two names, 1 of them is static and one that changes all the time. The first one is the one i want, the one that changes all the time. The other i do not need. I need to store the first name in a variable so i can implement it in other parts of the code. I've been struggling with this for a while. Please, any help is appreciated. Code:
use WWW::Mechanize; my $mech = WWW::Mechanize->new( autocheck => 0); my $fieldname = 0; #login $url="http://@ARGV[0]"; $mech->get( $url ); my $count = 1; for my $form ($mech->forms) { print "form $count fields are:\n"; $count; for ($form->param) { # printf "%s - %s\n", $_, $form->value($_); #print $form->value($_); # print %s; } # print "\n"; }

Replies are listed 'Best First'.
Re: $mech help
by Corion (Patriarch) on Sep 24, 2015 at 09:35 UTC

    So, what prevents you from not using the name that you know, and instead using the name that you don't know?

      The fields are names of two login boxes. The one that doesnt change is for one of the fields, so i can enter the details into that one fine. But the one that changes I cant. Becuase the fieldname changes all the time.

        The fields are names of two login boxes. The one that doesnt change is for one of the fields, so i can enter the details into that one fine. But the one that changes I cant. Becuase the fieldname changes all the time.

        Think about it, you know one name, and the other name you don't know .... and you can get a list of names ... hmmm, a list of all the field names, and foreknowledge of a known fieldname .... hmmm ... what to do?

Re: $mech help
by 1nickt (Canon) on Sep 24, 2015 at 21:34 UTC

    As corion said, this has not much to do with WWW::Mechanize. It has somewhat to do with the Perl language, and lots to do with how you think about your problem.

    Define your problem without thinking about code.

    In a list of things, you know the name of one thing that you wish to skip, and you don't know the name of another thing that you wish to keep. So you need to go through the list, throw away the thing you don't want, and keep whatever is left. Right?

    You may be looking for next.

    foreach my $thing ( @things ) { next if $thing eq $known_junk; # exact string comparison; change a +s needed print "$thing\n"; # whatever's not junk }
    Hope this helps!

    The way forward always starts with a minimal test.
Re: $mech help
by Anonymous Monk on Sep 24, 2015 at 09:40 UTC
    Have you read perlintro? It talks about storing stuff in variables
      Its not that simple. Its not two seperate prints, it prints both names in one print so how do i split them into two variables?

        Then your first step should be to identify the "one print" and split it up into two separate print statements. For example,

        print %s

        could be changed into a loop over the hash keys:

        for my $key (keys %s) { print "Key: %key, Value: $s{ $key }\n"; };
        length and substr, maybe?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        What line is that?