in reply to Re^2: Problems around Param (CGI->param or CGI->multi_param instead of CGI->Vars )
in thread Problems around Param

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
  • Comment on Re^3: Problems around Param (CGI->param or CGI->multi_param instead of CGI->Vars )
  • Download Code

Replies are listed 'Best First'.
Re^4: Problems around Param (CGI->param or CGI->multi_param instead of CGI->Vars )
by sauravrout (Novice) on Aug 26, 2015 at 09:54 UTC
    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;
      Please provide Dumper($q) from your action page
        name1 NAname10 N/Aname100 NAname100_drp N/Aname101 N/Aname101_drp N/Aname102 N/Aname102_drp N/Aname103 N/Aname103_drp N/Aname104 N/Aname104_drp N/Aname105
        where name<number> and name<number>_drp are the keys and N/A values.
      #!perl use warnings; use strict; use CGI; open FH, '>','/tmp/hash.dat' or die "Can not open hash.dat : $!"; my $q = new CGI; for my $k ( sort $q->param ) { if ($k =~ /(.+)_drp$/){ print FH join'|', $q->param( $1 ),$q->param( $k )."\n"; } } close FH;
      poj
        Thanks poj. I am getting desired result. But the issue I am facing is the inputs are not coming in the order in which they are inserted to the hash. I know hash doesn't store values in order, but is there any way to achieve this ? Note: I don't have Tie::IxHash; installed on my system and I am not authorized to install this.