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


Setting a select value in a txt file

Posted on 1999-07-12
Medium Priority
Last Modified: 2010-03-04

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

This is what is in this file:

Dutch gulder|Fl.|1.14||
German Mark|DM.|1.00|x|

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

$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/">
<OPTION>Dutch gulder
<OPTION selected>German Mark
<INPUT TYPE="submit">submit

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
Question by:weversbv
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

Accepted Solution

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.


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>) {
    my ($currency, $valuesign, $correction, $active) = split '\|';

        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'),
        radio_group('-name' => 'cur',
                    '-values' => [ sort keys %Currencies ],
                    '-default' => $activecur,
} 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->{active} ? 'x' : '')),
        close CUR;

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

Author Comment

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)


Author Comment

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!!
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!


Expert Comment

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'),
          popup_menu('-name' => 'cur',
                     '-values' => \@cur_values,
                     '-default' => $activecur,
                     '-labels' => \%cur_labels);
          submit, end_form, end_html;
} else {

Author Comment

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!!

Expert Comment

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

   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;

Author Comment

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:

Global symbol "cur_values" requires explicit package name at /data1/ line 38.
Global symbol "cur_labels" requires explicit package name at /data1/ line 40.

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

What is wrong?


Author Comment

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:


your form script (only the form part.

$html footer.

Expert Comment

ID: 1214072
> Global symbol "cur_values" requires explicit package name at
> /data1/ line 38.
> Global symbol "cur_labels" requires explicit package name at
> /data1/ 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;

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