1

I'm a novice. I've got this..

%categories = (
    'Announcements' => ['Auctions', 'Bands-Seeking-Members', 'Boutiques', 'Charity'],  
    'Appliances' => ['Dishwashers', 'Fireplaces/Wood-Burning-Stoves', 'Microwaves'],  
    'Auto-Parts-and-Accessories' => ['Auto-Accessories', 'Car-Audio-and-Video],  
    'Baby' => ['Baby-Clothing', 'Backpacks-and-Carriers', 'Changing', 'Cribs-and-Playpens'],  
    'Books-and-Media' => ['Blu-ray-Discs', 'Books:-Children'],  
    'Clothing-and-Apparel' => ['Boys-Clothing', 'Boys-Shoes', 'Costumes']  
);

I need it to print this..

• Announcements  
 -Auctions  
 -Bands-Seeking-Members  
 -Boutiques  
 -Charity  
•Appliances etc  

This is the code so far..

while( my ($cat,$subcat) = each %categories) {      
    print qq~• $cat
~;  
    print qq~- $subcat
~;  
}  

It's printing this..

• Clothing-and-Apparel  
- ARRAY(0x1e959c0)  
• Announcements  
- ARRAY(0x1e95590)  
• Auto-Parts-and-Accessories  
- ARRAY(0x1e95740)  
1
  • 1
    The %categories data structure is missing a closing quote (') after Car-Audio-and-Video. Commented Apr 13, 2012 at 2:36

1 Answer 1

4

I believe the following should do what you want:

while(my ($cat, $subcats) = each %categories) {
    print "• $cat\n";

    foreach my $subcat (@$subcats) {
        print "- $subcat\n";
    }
}

I have added a for loop to iterate over the contents of your subcategory arrays.

If you need the categories and subcategories sorted alphabetically, try this:

foreach my $cat (sort keys %categories) {
    print "• $cat\n";

    my $subcategories = $categories{$cat};

    foreach my $subcat (sort @$subcategories) {
        print "- $subcat\n";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.