Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2 Parts - Replacing listA with listB and grouping results 1

Status
Not open for further replies.

Waraq

Programmer
May 8, 2001
26
US
PART1:
I have a script that is taking values from a form and then emailing the results. At present, the results that are getting mailed are tagged by the form field ID (i.e. ada) but I would like it to be converted to the full name (i.e. Adabas). I use a sort to put the fields in a consistent order, but without doing a continual
foreach $key (sort(keys(%FORM))) {
if ($key eq 'naturaladabase') {
print MAIL "Natural Adabase (MF) \t\t\t";
} elsif ($key eq 'asset') {
print MAIL "Asset Management (BK) \t\t\t";
} ...
}


how would I convert to the correct items?

PART2:
How could I group the items in different areas (hence the MF and BK in parenthesis)?

Thanks.

 
You can make a hash whose keys are the form ids and the values are the actual name.

Unfortunately, I'm really not sure what you are asking... maybe an example would help.
 
The field names on the form are abbreviations (i.e. field1, field2, field3,...) and when I send the email to the person, I want to change the abbreviations to the full field name (i.e. Field Name 1, Field Name 2, Field Name 3,...) so basically I want to replace the fieldname abbreviation list with the fieldname fullname list.

Hope this lends a little insight.

Thanks.
 
Why not create a hash with the key as the fieldname abbreviation, and the value as the full fieldname. Then you would use it the same way you use the form hash. For example:
Code:
%FullNames = (
   "naturaladabase" => "Natural Adabase (MF)",
   "asset" => "Asset Management (BK)",
...
);
...
print MAIL $FullNames{$key},"\t\t\t";
...
That would solve the field names problem. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Isn't that what I said, without the example? Of course at the time I wasn't really sure if that was what he wanted. And examples are always good.
 
Thanks a lot, that worked, but how would I do step 2 now?
 
That's a little trickier. Any idea how many groups you're going to need, or is that going to change over time? If you have a relatively small number of groups that isn't likely to change much, you might be able to use a hash for each group containing the key/value pairs. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Tracy,

The pairs are designated by the value in the () at the end of the name if that makes it any easier and it is a set number.

 
I think what I'd do is create a hash containing the groups as keys, with their values references to anonymous hashes containing the key/value pairs for that group. It sounds complicated, but it's not terribly so:
Code:
%FullNames = (
   "naturaladabase" => "Natural Adabase (MF)",
   "asset" => "Asset Management (BK)",
...
);
...
foreach $key (keys(%FORM)) {
   $fullname = $FullNames{$key};
   # match the group and extract it
   ($group) = $fullname =~ /.*?\((.*?)\).*?/;
   # if a group hash doesn't exist, create it
   if ( !defined $Groups{$group} ) {
      # create a ref to an anonymous hash
      $Groups{$group} = { };
   }
   # add the key/value to the Group hash
   $Groups{$group}{$key} = $FORM{$key};
} # end foreach
...
# Now print the keys and values, sorted by group
foreach $group (sort(keys($Groups))) {
   print "Group $group\n";
   foreach $key (sort(keys($Groups{$group}))) {
      print MAIL $FullNames{$key}."\t\t\t";
   }
}
...
Does that make sense? I haven't tested it.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Tracey,

It makes sense, thanks but I am getting 2 errors when I do a compile:

Type of arg 1 to keys must be hash (not scalar deref) at academy_surveypost.cgi line 146, near "$Groups)"

Type of arg 1 to keys must be hash (not hash elem) at academy_surveypost.cgi line 148, near "})"

I think they are at the print:
# Now print the keys and values, sorted by group
foreach $group (sort(keys($Groups))) {
print "Group $group\n";
foreach $key (sort(keys($Groups{$group}))) {
print MAIL $FullNames{$key}."\t\t\t";
}
}

any ideas?
 
Tracey,

I got the errors fixed, I was confusing hashes and scalars.... but now the problem that I am having is that I can't print the values in the categories...only the categories are printing.
 
OOPS. Told you I hadn't tested the code. That first line you got the error on should be:
Code:
foreach $group (sort(keys(%Groups))) {
I think the other error line should be something like:
Code:
    foreach $key (sort(keys(%{$Groups{$group}}))) {
but I'm not real sure about that last one.

I also notice that I'm not printing the group name to the MAIL file like I am the other print line. And I didn't see anywhere in your original posting where you actually used the VALUES from the form, just the keys. If you want to get the values too, you'll need to use:
Code:
# this is the value for a given key in a group
$Groups{$group}{$key}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top