in reply to Problems around Param

param or multi_param is what you should use

use CGI; my $q = CGI->new('1=1;8=8;9=1;9=2;9=3;9=4;9=5;9=6;9=7;9=8;9=9;99=99;a= +a;b=b;Z=Z;'); for my $k ( sort $q->param ) { print join'|', $k, $q->multi_param( $k ), "\n"; } print "##" x 3, "\n"; for my $k ( sort $q->param ) { my $v = $q->param( $k ); print join'|', $k, $v, "\n"; } __END__ 1|1| 8|8| 9|1|2|3|4|5|6|7|8|9| 99|99| Z|Z| a|a| b|b| ###### 1|1| 8|8| 9|1| 99|99| Z|Z| a|a| b|b|
  • Comment on Re: Problems around Param (CGI->param or CGI->multi_param instead of CGI->Vars )
  • Download Code

Replies are listed 'Best First'.
Re^2: Problems around Param (CGI->param or CGI->multi_param instead of CGI->Vars )
by sauravrout (Novice) on Aug 26, 2015 at 06:45 UTC
    I am not looking in this format. For each row, I have two fields, comment(textfield) and cause(dropdown). So for each row I need like
    comment1|cause1 comment2|cause2 comment3|cause3
    In the above method you are printing Key/Value combination, but I need two values from foreach statement. Is is possible to do it using param function? or Any other way to achieve it ?

      I'm guessing from your script the parameters are paired by the names 'element' and 'element_drp'

      #!perl use warnings; use strict; use CGI; my $string = join ';',('elem1=comment1','elem1_drp=cause1', 'elem2=comment2','elem2_drp=cause2', 'elem3=comment3','elem3_drp=cause3'); my $q = CGI->new($string); for my $k ( sort $q->param ) { if ($k =~ /(.+)_drp$/){ print join'|', $q->param( $1 ),$q->param( $k )."\n"; } }
      poj
        This somewhat I need, but the values are coming from the cgi form. those comment1 and cause1 are some demo values. am collecting actual strings from the form and assigning them to the hash.

        Now while printing I don't need the names 'element' and 'element_drp', rather their values passed through the form.

        I hope I am more clear with my question this time. some more code snippet:

        sub first_process { $data_dir="/data_dir/saurav/data"; open(F1,"<$data_dir/comment_job.config") || die "Cannot open f +ile1:$!\n"; open(F2,"<$data_dir/comments_saurav.dat") || die "Cannot open +file2:$!\n"; while(<F1>){ chomp; chomp(my $f2=<F2>); push (@comment, $_ . "|" . $f2 . "\n"); } } print $q->start_form( -name => 'main', -method => 'POST', -action => 'comment_saurav.cgi', ); # Part which is printing the table print $q->start_table; &first_process; foreach (@comment) { #array starts here ($jobname, $element, $comment)= split /\|/; my $element_drop=$element . "_drp"; print $q->Tr( $q->td($jobname), $q->td( $q->textfield(-name => $element, -size => 50, -default=> $ +comment) ), $q->td( $q->popup_menu(-name => $element_drop, -values => ['N/A',' +Infra Issue','UpStream Delay','Teradata Issue', 'AbInitio Issue','Oth +ers'], -default => 'N/A', -labels => \%cause_opt ) ) ); }#array ends here #---------------End of table-------------------------------- print $q->end_table;
        Below is my current action page:
        my $q = new CGI; my $params = $q->Vars; my %params = $q->Vars; my @hash_array_keys=keys %params; my @hash_array_values=values %params; open FILEHANDLE, ">/tmp/hash.dat" or die "Can not open hash.dat"; $, = "\n"; for my $k (sort @hash_array_keys) { print FILEHANDLE $k, $params{$k}; } #print FILEHANDLE values %params; close FILEHANDLE;

      In the above method you are printing Key/Value combination, but I need two values from foreach statement. Is is possible to do it using param function? or Any other way to achieve it ?

      Is answer by poj what you want?

      If it isn't, you're going to have to be more specific, for example, provide a CGI->new('stringhere') that accurately represents the input you have