Saving a Form to a file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nmeliasp
    New Member
    • Jul 2008
    • 5

    #1

    Saving a Form to a file

    Hi i am trying to write a perl script to save a form to a file. Can anyone help?
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Sure, but we have to know things like:
    1. what you have tried thus far
    2. what errors you are seeing
    3. what is not working


    If you can post your code (surrounded by code tags please) and provide us the other information, we will do our best to help you.

    Regards,

    Jeff

    Comment

    • nmeliasp
      New Member
      • Jul 2008
      • 5

      #3
      i was able to save it to a file but now i just want to read in the file and just print out the data on a new line for each element. here is the code: i want the output to look exactly like how it is saved in the file
      Code:
      #!/usr/bin/perl
      
      use strict;
      use CGI qw(:standard);
      use CGI qw(:cgi);
      
      
      my $data_file = "/tmp/results.txt";
      my $configuration;
      my $config_ID;
      my $cds_ID ;
      
      open (DAT, $data_file) || die ("could not open file");
      
      my @raw_data = <DAT>;
      close(DAT);
      
      
      print "Content-type: text/html \n\n";
      print "<html><body>";
      	
      
      
      print "(@raw_data)";
      
      
      	
      
      print "</body></html>";
      *************** *****data from file*********** *************

      txtCfgId=test
      txtCfguser=test
      txtEmailAdr=
      txtPager=test
      txtSrvOU=test
      txtProject=rhga
      pnlServerID=eag a
      txtSrvrIP=egar
      txtSrvrGW=agae
      txtSrvrMask=eag a
      lstTimeZone=US% 2FCentral
      txtDNSDom=aega
      txtDNSHold1=efa r
      txtDNSHold2=erg ar
      txtDNSHold3=

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        nmeliasp,

        First, the code tags that I asked you to use to surround your code were not optional. They are required for the forums and your not putting them in requires a moderator to add them for you. Please add them to your code next time, as I requested earlier!

        As for your issue, the topic of your post seems to be misleading. It is saying that you want to save a form to a file yet in your latest post, you are wanting to display the contents of a file on the screen, as it appears in the file.

        When you run what you have, how does the output look?

        Personally, in place of this line:

        Code:
        print "(@raw_data)";
        I would write it as so, but this is just me:

        Code:
        foreach my $line (@raw_data) {
            chomp($line);
            print("$line\n");
        }
        I didn't test it, but that should print out the file, line by line, making it look on the screen as it did in the file.

        Regards,

        Jeff

        Comment

        • nmeliasp
          New Member
          • Jul 2008
          • 5

          #5
          this is the output of the code in an internet browser which is what i want the output to be in:

          VTI-GROUP=0 txtCfgId=test txtCfguser=test 1 txtEmailAdr=nme liasp%40yahoo.c om txtPager=test txtSrvOU=test txtProject=rhga pnlServerID=eag a txtSrvrIP=egar txtSrvrGW=agae txtSrvrMask=eag a lstTimeZone=US% 2FCentral txtDNSDom=aega txtDNSHold1=efa r txtDNSHold2=erg ar txtDNSHold3= Submit=Submit =

          Comment

          • nmeliasp
            New Member
            • Jul 2008
            • 5

            #6
            it was the same when i tried that piece of code you suggested

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by nmeliasp
              it was the same when i tried that piece of code you suggested
              He he, I didn't even take that into account, sorry, long day. Try this and see if it works:

              Code:
              foreach my $line (@raw_data) {
                  chomp($line);
                  print("<p>$line</p>");
              }
              Let me know if that works.

              Regards,

              Jeff

              Comment

              • KevinADC
                Recognized Expert Specialist
                • Jan 2007
                • 4092

                #8
                Originally posted by nmeliasp
                this is the output of the code in an internet browser which is what i want the output to be in:

                VTI-GROUP=0 txtCfgId=test txtCfguser=test 1 txtEmailAdr=nme liasp%40yahoo.c om txtPager=test txtSrvOU=test txtProject=rhga pnlServerID=eag a txtSrvrIP=egar txtSrvrGW=agae txtSrvrMask=eag a lstTimeZone=US% 2FCentral txtDNSDom=aega txtDNSHold1=efa r txtDNSHold2=erg ar txtDNSHold3= Submit=Submit =
                Output in a browser requires HTML markup to display properly. Jeff posted a simple example but this is not the HTML forum. If you need to learn the basics of HTML find an HTML tutorial or read the CGI.pm modules documentation for generating HTML output.


                Jeff,

                this is not a good example:

                Code:
                foreach my $line (@raw_data) {
                   chomp($line);
                   print("$line\n");
                }

                FIrst it makes little sense to chomp() the input only to add a newline to print the output. But the bigger issue is the use of parenthesis around the argument to the print command. That is really almost never needed, although there are times, when it is, like this:

                Code:
                print (split(//))[0],"\n";
                but if you do that it will print nothing, it will in fact throw a syntax error . You have to use some odd syntax to make it work:

                Code:
                print ((split(//))[0]),"\n";
                or:

                Code:
                print +(split(//))[0],"\n";
                But in this example the newline will not be printed:

                Code:
                print ((split(//))[0]),"\n";
                because the print function expects a list, and the parenthesis create a list, so after the last parenthesis the list ends and the newline is ignored. You have to put the newline inside the parenthesis to make it part of the list. In this example it will print the newline:

                Code:
                print +(split(//))[0],"\n";
                But that is rather obscure syntax and to the unsuspecting perl coder it will look like it should not even work. Hell, I don't even understand why it works but its documented in the print functions manpage.

                The best practice is to avoid using parenthesis around the arguments of the print function unless it is really necessary.

                Damn, I drank too much coffee this morning.

                Comment

                • eWish
                  Recognized Expert Contributor
                  • Jul 2007
                  • 973

                  #9
                  Originally posted by KevinADC
                  Damn, I drank too much coffee this morning.
                  Is this becoming a problem for you?

                  --Kevin

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by eWish
                    Is this becoming a problem for you?

                    --Kevin
                    Maybe ............... ...........

                    Comment

                    • nmeliasp
                      New Member
                      • Jul 2008
                      • 5

                      #11
                      yes that worked!!! Thank you

                      Comment

                      Working...