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

First of this is my first real attempt into data manipulation so please bear with me. I have a script that so far takes some data from a form using get and manipulates it to produce the output of the data in the way that I want so far.

What I want to do next is take one of they key/values and merge it to the other key/values that I have outputing. Basically I want the following output

output1 = on
output2 = on
output3 = on
announcement = test

to show up as follows:

output1 = test
output2 = test
output3 = test

I know how to manipuate the date to remove =on, but my issue is what is the best way and how do I take the announcement = test and then merge it to the output1 key. Below is my script so far. Thanks in advance for your help.

#!/usr/bin/perl # Read in text $method = $ENV{'REQUEST_METHOD'}; if ($method eq "GET") { $buffer= $ENV{'QUERY_STRING'}; }else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); } # Split information into name/value pairs my @pairs = split (/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; $FORM{$name} = $value; } print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print "<title>CGI Program</title>"; print "</head>"; print "<body>"; foreach $name (sort keys(%FORM)) { print "$name = $FORM{$name}<BR>\n"; } print "</body>"; print "</html>"; 1;

Replies are listed 'Best First'.
Re: Manipulating key/value output
by eff_i_g (Curate) on Jan 21, 2011 at 18:16 UTC
    Merge or replace?

    A simple example:
    use warnings; use strict; use Data::Dumper; my %hash = ( output1 => 'on', output2 => 'on', output3 => 'on', announcement => 'test', ); $hash{$_} = $hash{announcement} for keys %hash; delete $hash{announcement}; print Dumper(\%hash);

      Thanks for the Reply. Not quite what I was looking for though. Basically I want to take the output1 entry and merge it with test while stripping out the = on and anouncement =(if that makes sense)

      Also not sure if this applies or not, but my variables can change. Sometimes I will have output1, sometimes output2 and output3, sometimes outpout 1 and output4 (can go up to 40 at least). It will vary. The only constant will be announcement = 'whatever the announcement is'

      The thing that I am wondering now is if I should have output = set as a constant instead of output1, output2, etc.. Meaning env = <this will change> and announcement = <this will change> and then manipulating from there... That might work better maybee??

Re: Manipulating key/value output
by Anonyrnous Monk (Hermit) on Jan 21, 2011 at 18:31 UTC

    As an aside (not related to your merging question), you could use CGI, or CGI::Simple for the query parameter splitting... E.g.

    use CGI::Simple; my $q = CGI::Simple->new(); my %FORM = $q->Vars;

    Or, with immediate input:

    my $q = CGI::Simple->new( 'output1=on&output2=on&output3=on&announceme +nt=test' ); my %FORM = $q->Vars; print "$_ = $FORM{$_}\n" for sort keys %FORM; __END__ announcement = test output1 = on output2 = on output3 = on