Yes, hashes have no concept of order. Arrays do. The larger
issue of your problem leads me to believe that hashes are
the better data structure.
You choice then is to sort the keys of the hash. You foreach
loop ( which I write as a for loop - they are identical, so
do not be confused ) would look something like:
for my $key ( sort keys %title ) {
which will sort the keys in asciibetical order. To sort the keys
on some criteria other than asciibetical, try something like
this ( I hope I remember how fetch_all_hash works ):
# This will sort by title, then by topic id
sub by_course_and_title {
return $title{$a}{il2e002_course_title_x} cmp title{$b}{il2e002_cour
+se_title_x} ||
$title{$a}{il2e002_topic_id_k} cmp title{$b}{il2e002_topic_id
+_k};
}
for my $key ( sort &by_course_and_title keys %title ) {
In answer to your other question, how to step through an array,
it is simply
for my $foo ( @foo ) {
# Do something interesting
}
See perldoc -f sort to understand a bit more of the possibilities
of the sort command - it is quite potent.
mikfire
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.