Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

ReadParse and Hash Value

by maruti (Initiate)
on Nov 07, 2014 at 04:45 UTC ( [id://1106448]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,I am begginer to perl, I am trying to read values from form using ReadParse() function in Hash (%in), I am not getting elements as order I submit in form, I Want get in same order as I submit in form, please give me solution. Thanks.

Replies are listed 'Best First'.
Re: ReadParse and Hash Value
by boftx (Deacon) on Nov 07, 2014 at 04:50 UTC

    You can not know the order of hash elements by definition. This is a security feature in Perl. The best you can do is do sort(keys(%hash)) to impose order on them. On the order hand, you have full control over the order of elements in an array.

    You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.
Re: ReadParse and Hash Value
by GrandFather (Saint) on Nov 07, 2014 at 06:34 UTC

    If you show us the code you are having trouble with we may be able to give a little more help.

    If you tell us why you think you need the form elements in order we may be able to help even more. Generally the more pertinent information you give us the better we can help.

    Perl is the programming world's equivalent of English

      I have created from, I want to write the value from the form to the file in the same order which the from submits. I trying to write simple module in webmin to get some values from web browser and write into a file

      #!/usr/bin/perl require './mymodule-lib.pl'; if ( $ENV{'REQUEST_METHOD'} eq 'POST') { &ReadParse(); local $ref =\%in; &write_file($config{'info'}, $ref ); } &read_file($config{'info'}, \%read, ); print &ui_print_header(undef, "Configuration Information", "", undef, +1, 1, 0); #Start of form to get inputs print &ui_form_start("index.cgi", "post" ); + print &ui_table_start("Configurations", undef, 2, ["width=30%"] ); print &ui_table_row("Bandwidth", &ui_textbox( "bandwidth",$read{'bandw +idth'} , 80, 0, 40, ) ); print &ui_table_row("Distance", &ui_textbox( "distance",$read{'distanc +e'}, 80, 0, 40, ) ); print &ui_table_row("Size", &ui_textbox( "size",$read{'size'} , 80, 0, + 40, ) ); print &ui_table_row("Active",&ui_yesno_radio("active", $read{'active'} + ? 1 : 0 , 1 )); print &ui_table_end(); print &ui_form_end([ [ undef, "submit" ] ]); print &ui_print_footer("/", $text{'index'});

      First

      I have created from, I want to write the value from the form to the file in the same order which the from submits. I trying to write simple module in webmin to get some values from web browser and write into a file

      #!/usr/bin/perl require './mymodule-lib.pl'; if ( $ENV{'REQUEST_METHOD'} eq 'POST') { &ReadParse(); local $ref =\%in; &write_file($config{'info'}, $ref ); } &read_file($config{'info'}, \%read, ); print &ui_print_header(undef, "Configuration Information", "", undef, +1, 1, 0); #Start of form to get inputs print &ui_form_start("index.cgi", "post" ); + print &ui_table_start("Configurations", undef, 2, ["width=30%"] ); print &ui_table_row("Bandwidth", &ui_textbox( "bandwidth",$read{'bandw +idth'} , 80, 0, 40, ) ); print &ui_table_row("Distance", &ui_textbox( "distance",$read{'distanc +e'}, 80, 0, 40, ) ); print &ui_table_row("Size", &ui_textbox( "size",$read{'size'} , 80, 0, + 40, ) ); print &ui_table_row("Active",&ui_yesno_radio("active", $read{'active'} + ? 1 : 0 , 1 )); print &ui_table_end(); print &ui_form_end([ [ undef, "submit" ] ]); print &ui_print_footer("/", $text{'index'});
Re: ReadParse and Hash Value
by roboticus (Chancellor) on Nov 07, 2014 at 12:33 UTC

    maruti:

    As has been mentioned, if you care about the order, then a hash may be the wrong data structure to use. But if you're in control of the form as well, then the order isn't important--since you built the form, you know the correct order. So just grab the data items from the hash in the order you want to print them:

    for my $field_name (qw( bandwidth distance size active )) { my $value = $$ref{$field_name}; print $FH "$field_name, $value\n"; }

    So you could modify your write_file routine to do it that way.

    If you're using write_file for multiple forms, then you could modify write_file to accept a list of field names so you can specify the order, kinda like this:

    sub write_file { my ($cinfo, $data, $fields) = @_; . . . if (!defined $fields) { # caller didn't specify field order, so we need a list # of data items, so get 'em from the hash $fields = [ keys %$data ]; } for my $field_name (@$fields) { my $value = $$ref{$field_name}; print $FH "$field_name, $value\n"; } . . . } . . . write_file($config{'info'}, $ref, [qw(bandwidth distance size active)] + );

    This change lets you pass in the order you want to write_file, and if you don't specify the order, it will print them the same as it does now (i.e., whatever order the hash chooses). This lets you change the write_file routine without breaking it for everyone else.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: ReadParse and Hash Value
by Anonymous Monk on Nov 07, 2014 at 07:50 UTC

    Did you write the code you posted?

    Don't use ReadParse , see Re: Manually add parameters in perl cgi ( no ReadParse no CGI->Vars

    Also if you got CGI.pm , use its API, no looking at %ENV :)

    #!/usr/bin/perl -- #~ ## #~ ## #~ ## #~ ## #~ ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr - +ce -nibc -i=4 -pt=0 "-nsak=*" #~ ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -o +pr -ce -nibc -i=4 -pt=0 "-nsak=*" #~ ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while " - +otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use CGI (); use CGI::Carp qw( fatalsToBrowser ); Main( @ARGV ); exit( 0 ); sub Main { my( $configref ) = require './mymodule-lib.pl'; my $q = CGI->new; if( $q->request_method eq 'POST' ) { return print SaveFormRedirect( $configref->{info}, $q ); } else { return print ShowStuff( $configref, $q ); } } ## end sub Main sub SaveFormRedirect { my( $conf, $q ) = @_; write_file( $conf->{'info'}, $q ); return $q->redirect( UrlFor( $q, 'index.cgi' ) ); } ## end sub SaveFormRedirect sub UrlFor { my( $q, $modepath ) = @_; my $url = $q->url( -absolute => 1, -path => 1, ); return $url . '?' . $modepath; } ## end sub UrlFor sub ShowStuff { my( $conf, $q ) = @_; return join '', ui_print_header( undef, "Configuration Information", "", undef, +1, 1, 0 ); ui_form_start( UrlFor( $q, "index.cgi" ), "post" ), ui_table_start( "Configurations", undef, 2, ["width=30%"] ), ui_table_row( "Bandwidth", ui_textbox( "bandwidth", scalar $q->param( 'bandwidth' ), 80, +0, 40, ) ), ui_table_row( "Distance", ui_textbox( "distance", scalar $q->param( 'distance' ), 80, 0, + 40, ) ), ui_table_row( "Size", ui_textbox( "size", scalar $q->param( 'size' ), 80, 0, + 40, ) ), ui_table_row( "Active", ui_yesno_radio( "active", scalar $q->param( 'active' ) ? 1 : 0 +, 1 ) ), ui_table_end(), ui_form_end( [ [ undef, "submit" ] ] ), ui_print_footer( "/", scalar $q->param( 'index' ) ), ; } ## end sub ShowStuff __END__

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1106448]
Approved by boftx
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-09-09 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.