Good day Monks, Here's my problem.
I have a text file with content
name=Document1.doc type=Word author=aAuthor name=Document1.doc type=Excel author=bAuthor name=Document1.doc type=Text author=cAuthor name=Document1.doc type=Excel author=dAuthor name=Document1.doc type=Word author=eAuthor
My problem is to sort this file and write to another (first based on the key "type" and then on the key "author") so that the output file looks like :
name=Document1.doc type=Excel author=bAuthor name=Document1.doc type=Excel author=dAuthor name=Document1.doc type=Text author=cAuthor name=Document1.doc type=Word author=aAuthor name=Document1.doc type=Word author=eAuthor
What I have tried is to write a function which accepts one key name and sort the file based on that key. Again the function will be invoked with the second key. But when I invoked the function with second key, the output happened to be sorted in second key alone(the first level sorting has gone which is obvious). Is there any way with which I can do this in one stretch.?
Any suggestions welcome.
#$field is the position of the element (sort key) in the #line open INPUT , $input || die $input.$! ; while (<INPUT>) { @fields = split(" ",$_) ; push @{$hash{$fields[$field]}}, $_ ; $hash_keys{$fields[$field]} = $fields[$field] unless exists $h +ash_keys{$fields[1]} ; } for $element (sort keys %hash_keys) { open OUTPUT, ">>$output" || $output.$! ; print OUTPUT @{$hash{$element}} ; close OUTPUT ; } close INPUT ;
Many thanks for your time..

In reply to Sorting with multiple keys by Mandrake

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.