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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to group items in list

Status
Not open for further replies.

moegal

Technical User
Jan 8, 2001
5
US
Hi:
How do I display the following information so it is grouped. My file has the following tab delimited data.

type name description
salad Cobb Very nice salad with choice fixings
entree Chicken Boneless, Skinless roasted to perfection
salad Chef Topped with everything from eggs to ham
entree Steak Filet mignon cooked just the way you want it

The output should look like:

Salad
Cobb, Very nice salad with choice fixings
Chef, Topped with everything from eggs to ham

Entree
Chicken, Boneless, Skinless roasted to perfection
Steak, Filet Mignon cooked just the way you want it

Any ideas?

Thanks in advance
marty
marty@moegal.com
 
%menu;
open(FILE, "dat.txt");
@lines = <FILE>;
close(FILE);

foreach $line (@lines)
{
($type, $name, $desc) = split(&quot;\t&quot;, $line);
$menu{$type} .= &quot;$name\t$desc!!&quot;;

}

while(($key, $value) = each %menu)
{
print &quot;$key\n&quot;;
@stuff = split(&quot;!!&quot;, $value);
foreach(@stuff)
{
print &quot;$_&quot;;
}
print &quot;\n&quot;;
}

this should work. adam@aauser.com
 
Try something like this:

open(FILE,&quot;yourfile&quot;);
my @FILE = <FILE>;
my %DATA;

for (my $x=0; $x<@FILE; $x++)
{
@FILE[$x] = split(/\t/,$FILE[$x])
$DATA{$FILE[$x][0]}{$FILE[$x][1]} = $FILE[$x][2];
}

my $category;
foreach $category (keys(%DATA))
{
print &quot;$category\n&quot;;
my $selection;
foreach $selection (keys(%DATA{$category}))
{
print &quot;$selection, $DATA{$category}{$selection}\n&quot;;
}
}
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
don't know why, but, i get syntax errors at:

foreach $selection (keys(%DATA{$category}))

even after fixing your missing ; at:


@FILE[$x] = split(/\t/,$FILE[$x])

adam@aauser.com
 
Adam and Tom:
Hi! Thanks for responding to my request. I cannot get either solution to work.

On your solution Tom I get the following:

Scalar found where operator expected at menu2.cgi line 12, at end of line
(Missing semicolon on previous line?)
syntax error at menu2.cgi line 12, near &quot;)
$DATA&quot;
syntax error at menu2.cgi line 12, near &quot;} =&quot;
Execution of menu2.cgi aborted due to compilation errors.
Scalar found where operator expected at menu2.cgi line 12, at end of line
(Missing semicolon on previous line?)
syntax error at menu2.cgi line 12, near &quot;)
$DATA&quot;
syntax error at menu2.cgi line 12, near &quot;} =&quot;
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 12, near &quot;]}&quot;
(Did you mean $ or @ instead of %?)
Can't use subscript on private hash at menu2.cgi line 20, near &quot;$category}&quot;
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near &quot;})&quot;
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 20, near &quot;$category}&quot;
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near &quot;})&quot;
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 20, near &quot;$category}&quot;
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near &quot;})&quot;
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 20, near &quot;$category}&quot;
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near &quot;})&quot;
Execution of menu2.cgi aborted due to compilation errors.

Adam,
I get the following with yours:

malformed header from script. Bad header=Entree: /data1/hm/moegal.com/cgi-bin/menu2.cgi

Any Ideas?

Thanks, Marty
 
Sorry, that was meant to be a rough sketch, not copied line for line and then interpreted. Let me see if I can patch the errors:

open(FILE,&quot;yourfile&quot;);
my @FILE = <FILE>;
close(FILE);
my %DATA;

for (my $x=0; $x<@FILE; $x++)
{
@FILE[$x] = split(/\t/,$FILE[$x]);
$DATA{$FILE[$x][0]}{$FILE[$x][1]} = $FILE[$x][2];
}

foreach my $category (keys(%DATA))
{
print &quot;$category\n&quot;;
foreach my $selection (keys($DATA{$category}))
{
print &quot;$selection, $DATA{$category}{$selection}\n&quot;;
}
}

I think that should be OK. Feel free to try to fix the syntax yourself if you have a problem! $DATA{$category} should evaluate as a hash, though I thought %DATA{$category} was correct too. If it works but doesn't print quite right, try setting $desc=$DATA{$category}{$selection} and then print &quot;$selection, $desc\n&quot;; Good luck!
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
if you are using mine from the web, you must add a

print &quot;Content-type: text/html\r\n\r\n&quot;;

to the beginning. adam@aauser.com
 
Tom and Adam,
Thanks. I could not get yours going Tom but I appreciate the help.

Andy, I got yours working and am working on the formatting. Another issue I have is the sort order. How can I set the Type as a sort order but not alphabetically. Salads appears before Entrees but Cheeses are last for this particular customer. Can I make another field in the database to hold the sort order and sort on this first in the script? Then call the script to list the data. Should I just sort after opening and closing the file, and if so what do I do with the sort field? Can I just ignore it?

Finally, The output for the name and description is in the form of the default variable $_. Can I split the name and description into seperate variables so I can apply different formatting to each of them. My final script will have 4 seperate fields for each line item and each with its own formatting.

I really appreciate your help, and I am learning quite a bit.
 
Well, my code was supposed to make it easier to sort, format, etc. It's a little more elegant, IMHO. But I don't have time to run it through the interpreter and debug it, so do what you will with it. I hope it gives you some ideas though.

You can sort by doing &quot;foreach $key (sort(keys(%hash)))&quot;. But that will do alphanumeric ordering. If you just want to print in a particular order, then just do it in your order instead of using the while loop. Of course you can split any string/array/whatever however you want. Use the above examples as a precedent of how to do that. You shouldn't need others to write your code for you.
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
split is easy. This is a little verbose in order to make the flow easy to follow.

<psuedo-code> sort-of
a list is = split (/on tabs/, the value in $_);
<real code>
($field1,$field2) = split(/\t/,$_);

more concisely,
@fields = split /\t/;

If your items are space delimited, then replace the '\t' with a space, or whatever delimiter works. If splitting won't do what you want, some simple pattern matching will get there easily. Just ask if you need an example.

'hope this helps....




keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top