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

... and then using them in calculations... Hello I wonder if someone can advise me. I've searched and searched but cannot find an answer to this. Maybe I am asking the wrong question or it may not be possible. I am receiving some variables from a web form into my script in the form $in{'REPORTID_X'} $in{'COST_X'} where X corresponds to the record number being received. I will know how many records I will receive but the number is not fixed (usually between 30 and 60 so not huge) so I wanted to write a while {} statement to write the incoming data to a file. Something like this:
$linecounter = 1; $records = in{'records'}; OPEN FILE while ($linecounter <= $records) { $thiscost = in{'COST_$linecounter'} * 100; print FILE in{'REPORTID_$linecounter'}; # I know this doesn't work but + it is what I need print FILE $thiscost; $linecounter++; } CLOSE FILE
Can anyone help? Many thanks in advance. Paul

Replies are listed 'Best First'.
Re: Concatenating numeric variables...
by kennethk (Abbot) on Apr 29, 2009 at 22:59 UTC
    Your context is a little vague, but I think your issue is with string interpolation. If you change from single to double quotes, that will tell the interpreter to interpolate any variables encountered, i.e.

    print FILE in{"REPORTID_$linecounter"};

    You could also get the same result using the concatenation operator (.), i.e.

    print FILE in{'REPORTID_' . $linecounter};

Re: Concatenating numeric variables...
by jwkrahn (Abbot) on Apr 29, 2009 at 23:51 UTC

    You want something like this:

    open FILE, '>', 'some_file' or die "Cannot open 'some_file' $!"; for my $line_counter ( 1 .. $in{ records } ) { print FILE $in{ "REPORTID_$line_counter" }, $in{ "COST_$line_count +er" } * 100; } close FILE;
      That's absolutely brilliant thanks - all I needed was to changes the single quotes to doubles and it worked perfectly. A little knowledge... Thanks again and all the best, Paul
Re: Concatenating numeric variables...
by hilitai (Monk) on Apr 29, 2009 at 23:06 UTC
    Like this, you mean? What you are trying to do is not completely clear to me from your description.
    #!/usr/bin/perl -w use strict; # create some dummy data my %in; $in{"COST_1"} = 20; $in{"COST_2"} = 30; $in{"REPORTID_1"} = 3232; $in{"REPORTID_2"} = 3483; $in{"records"} = 2; my $linecounter = 1; my $records = $in{'records'}; open(FILE, ">outfile") or die "outfile: $!"; while ($linecounter <= $records) { my $thiscost = $in{"COST_$linecounter"} * 100; print FILE $in{"REPORTID_$linecounter"}, " "; print FILE $thiscost, "\n"; $linecounter++; } close FILE;
    >cat outfile
    3232 2000
    3483 3000
Re: Concatenating numeric variables...
by Anonymous Monk on Apr 29, 2009 at 22:19 UTC
    use strict; use warnings; my %in; print in{'records'}; Unquoted string "in" may clash with future reserved word at - line 4. __END__ Name "main::in" used only once: possible typo at - line 4. Odd number of elements in anonymous hash at - line 4. print() on unopened filehandle in at - line 4.
    You want $in{records} and $in{"REPORTID_$linecounter"} ... perlintro
      Had a play around with this and couldn't make sense of it in relation to what I am looking to achieve - my bad and limited experience.