×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to group items in list

How to group items in list

How to group items in list

(OP)
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

RE: How to group items in list

%menu;
open(FILE, "dat.txt");
@lines = <FILE>;
close(FILE);

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

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

this should work.

adam@aauser.com

RE: How to group items in list

Try something like this:

open(FILE,"yourfile");
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 "$category\n";
  my $selection;
  foreach $selection (keys(%DATA{$category}))
  {
    print "$selection, $DATA{$category}{$selection}\n";
  }
}


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: How to group items in list

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

RE: How to group items in list

(OP)
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 ")
$DATA"
syntax error at menu2.cgi line 12, near "} ="
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 ")
$DATA"
syntax error at menu2.cgi line 12, near "} ="
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 12, near "]}"
(Did you mean $ or @ instead of %?)
Can't use subscript on private hash at menu2.cgi line 20, near "$category}"
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near "})"
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 20, near "$category}"
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near "})"
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 20, near "$category}"
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near "})"
Execution of menu2.cgi aborted due to compilation errors.
Can't use subscript on private hash at menu2.cgi line 20, near "$category}"
(Did you mean $ or @ instead of %?)
Type of arg 1 to keys must be hash (not hash elem) at menu2.cgi line 20, near "})"
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

RE: How to group items in list

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,"yourfile");
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 "$category\n";
  foreach my $selection (keys($DATA{$category}))
  {
    print "$selection, $DATA{$category}{$selection}\n";
  }
}

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 "$selection, $desc\n";  Good luck!


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: How to group items in list

if you are using mine from the web, you must add a

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

to the beginning.

adam@aauser.com

RE: How to group items in list

(OP)
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.

RE: How to group items in list

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 "foreach $key (sort(keys(%hash)))".  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.
http://www.oac-design.com

RE: How to group items in list

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close