Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting a select value in a txt file

Posted on 1999-07-12
9
Medium Priority
?
150 Views
Last Modified: 2010-03-04
Hello,

This is what I want:
I have a file called:
file.txt

This is what is in this file:

currency|valuesign|correction|active
Dutch gulder|Fl.|1.14||
German Mark|DM.|1.00|x|
Dollar|$|0.80||

I want now two things:
First:
I need to set 2 things:
$sign and
$correctvalue

$sign must be set to the valuesign of the line with is active!
In this example $sign would be "DM."
The same thing has to be done with the value $correctvalue.
In this example it would be $correctvalue = "1.00";

The second thing I want to do is the make a little form
with a selection option with all the currency options of the
file.txt file. The option to be selected is the value where
the active sign "x" is set in the file.txt file.

Something like this:

<FORM ACTION="/cgi-bin/test.pl">
<SELECT NAME="cur">
<OPTION>Dutch gulder
<OPTION selected>German Mark
<OPTION>Dollar
</SELECT>
<INPUT TYPE="submit">submit
</FORM>

What I now want is that when I choose an option out of the options
the chossen value will be set in the file.txt file.
For example when I choose "Dutch gulders" and then submit it, the
active value for Dutch guders will be set to "x" and the active value
for German Marks will become nothing.

Is this possible?

greetings Michel Weegerink
0
Comment
Question by:weversbv
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 5

Accepted Solution

by:
prakashk021799 earned 320 total points
ID: 1214064
Here's the script which handles both your concerns. The file name is hard-coded in the script. You might want to change that appropriately. Also, better error-checking will be needed.

#!/usr/bin/perl

use strict;
use CGI qw(:standard);

print header, start_html('Currency Selector');

my (%Currencies, $activecur, $sign, $correctvalue);

# read the file
open CUR, "<file.txt" or die "can't open file.txt; $!\n";
while (<CUR>) {
    chomp;
    my ($currency, $valuesign, $correction, $active) = split '\|';

    next
        if $. == 1;  # skip if first line

    $Currencies{$currency}{valuesign} = $valuesign;
    $Currencies{$currency}{correction} = $correction;
    if ($active eq 'x') {
        $sign = $valuesign;
        $correctvalue = $correction;
        $activecur = $currency;

        $Currencies{$currency}{active} = 1;
    } else {
        $Currencies{$currency}{active} = 0;
    }
}
close CUR;

unless (param('cur')) {

    # display the form

    print h1('Select currency'),
        start_form,
        radio_group('-name' => 'cur',
                    '-values' => [ sort keys %Currencies ],
                    '-default' => $activecur,
                    -linebreak=>'true'),
        submit,
        end_form,
        end_html;
} else {
    # currency selected, change it in the file

    my $cur = param('cur');
    unless ($activecur eq $cur) {
        $Currencies{$activecur}{active} = 0;
        $Currencies{$cur}{active} = 1;

        # write back to the file

        rename 'file.txt', 'file.txt.bak' or warn "can't backup file.txt\n";

        open CUR, ">file.txt";
        print CUR "currency|valuesign|correction|active\n";
        for (sort keys %Currencies) {
            my $cur_data = $Currencies{$_};
            print CUR join '|', ($_, $cur_data->{valuesign},
                                 $cur_data->{correction},
                                 ($cur_data->{active} ? 'x' : '')),
                      "\n";
        }
        close CUR;
    }

    print h1('Active Currency Changed'),
          strong($cur) . " is changed to active.",
          end_html;
}
0
 

Author Comment

by:weversbv
ID: 1214065
The script works but I have a few questions:
first you use radio buttons.
Is it possible to use a select group?
Second how do I implement it in a script.
I would want to use something like this:

Cururrency: (here the selectbox) (here the submit button)


0
 

Author Comment

by:weversbv
ID: 1214066
Is it possible to do the above without the

use CGI qw(:standard); code

Using a simple form.

I can not edit this code into my script!!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:prakashk021799
ID: 1214067
Sure you can use select group.

Change the code like this (I am only showing the relevant part here):

unless (param('cur')) {

    # display the form

    my @cur_values = sort keys %Currencies;
    my %cur_labels = map { $_ => $_; } @cur_values;

    print h1('Select currency'),
          start_form,
          popup_menu('-name' => 'cur',
                     '-values' => \@cur_values,
                     '-default' => $activecur,
                     '-labels' => \%cur_labels);
          submit, end_form, end_html;
} else {
....
}
0
 

Author Comment

by:weversbv
ID: 1214068
Is it possible to make an form that has no header.

I only want the part from <FORM> to </FORM>
so I can edit this into my script!!
0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 1214069
> Is it possible to make an form that has no header.
Just do not print the header.

Remove:
   h1('Select currency'),
from the print statement.

The statement becomes:

    print start_form,
             popup_menu('-name' => 'cur',
                            '-values' => \@cur_values,
                            '-default' => $activecur,
                            '-labels' => \%cur_labels);
             submit, end_form, end_html;
0
 

Author Comment

by:weversbv
ID: 1214070
When I use this code:

 print start_form,
             popup_menu('-name' => 'cur',
                            '-values' => \@cur_values,
                            '-default' => $activecur,
                            '-labels' => \%cur_labels);
             submit, end_form, end_html;

I get the following error:

Errors:
Global symbol "cur_values" requires explicit package name at /data1/hypermart.net/mmcw/cgi-bin/test/currency.cgi line 38.
Global symbol "cur_labels" requires explicit package name at /data1/hypermart.net/mmcw/cgi-bin/test/currency.cgi line 40.

Line 38 = '-values' => \@cur_values,
Line 40 = '-labels' => \%cur_labels);

What is wrong?

0
 

Author Comment

by:weversbv
ID: 1214071
A question I have is taht this script makes a HTML page himself.
I want to edit this form script part to a existing html page script.

I have something like this:

$htmlheader

your form script (only the form part.

$html footer.
0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 1214072
> Global symbol "cur_values" requires explicit package name at
> /data1/hypermart.net/mmcw/cgi-bin/test/currency.cgi line 38.
> Global symbol "cur_labels" requires explicit package name at
> /data1/hypermart.net/mmcw/cgi-bin/test/currency.cgi line 40.

Did you decalre @cur_values and %cur_labels as 'my' variables? Refer to my earlier comment on July 13 1999 - 06:52AM PDT.

> A question I have is taht this script makes a HTML page himself.
> I want to edit this form script part to a existing html page script.

If you don't need something printed, just remove them from the print statement. If you don't want anything printed, but instead saved in variables, just assign the concatenated values to the variable or print the values to the varaible.

For example, if you want to capture the form part in a variable, you can do:

$form = popup_menu('-name' => 'cur',
                                   '-values' => \@cur_values,
                                   '-default' => $activecur,
                                   '-labels' => \%cur_labels) .
                    submit . end_form;

Notice the use of '.' (the concatenation operator) in the above statement;
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question