poj gave you some real code for some faked up data, but quite likely it doesn't work for the data you have and it doesn't actually solve the problem you have either. You have actually been given most of the pieces you need and I bet you don't see how they fit together.

A hash is the right thing to solve part of the problem - it lets you group types of things together. An array is right for a different part of the problem - it lets you have a list of things of the same type. So the data structure you need is a two level hash of arrays.

The top level of the hash is keyed by shape (polygon, rectangle, ...). The values for the top level hash are references to second level hashes keyed by class (abc, def, ...). The values for the second level hashes are references to arrays. Ignoring how you parse the data to generate the data structure, you end up with something that looks like:

my %hash = ( polygon => { abc => ["0, 0, 10, 0, 12, 2", "10, 10, 20, 10, 22, 12",], def => ["3, 5, 3, 8, 7, 10"], }, rectangle => { abc => ["-10, -10, 20, 20"], def => ["22, 33, 44, 44", "1, 1, -3, -6"], } );

which can be printed out using:

for my $shape (sort keys %hash) { for my $class (sort keys %{$hash{$shape}}) { print <<LINE for @{$hash{$shape}{$class}}; <$shape class ="$class" coordinates="$_"> LINE } }

Since you've not given anything representing your real data you will have to figure out how to generate the hash from your data for yourself. However the print line above gives the syntax for accessing the array you need to add entries to for a given class and shape.

Perl is the programming world's equivalent of English

In reply to Re: Creation of Hash necessary? by GrandFather
in thread Creation of Hash necessary? by sandorado

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.