maruti:

As has been mentioned, if you care about the order, then a hash may be the wrong data structure to use. But if you're in control of the form as well, then the order isn't important--since you built the form, you know the correct order. So just grab the data items from the hash in the order you want to print them:

for my $field_name (qw( bandwidth distance size active )) { my $value = $$ref{$field_name}; print $FH "$field_name, $value\n"; }

So you could modify your write_file routine to do it that way.

If you're using write_file for multiple forms, then you could modify write_file to accept a list of field names so you can specify the order, kinda like this:

sub write_file { my ($cinfo, $data, $fields) = @_; . . . if (!defined $fields) { # caller didn't specify field order, so we need a list # of data items, so get 'em from the hash $fields = [ keys %$data ]; } for my $field_name (@$fields) { my $value = $$ref{$field_name}; print $FH "$field_name, $value\n"; } . . . } . . . write_file($config{'info'}, $ref, [qw(bandwidth distance size active)] + );

This change lets you pass in the order you want to write_file, and if you don't specify the order, it will print them the same as it does now (i.e., whatever order the hash chooses). This lets you change the write_file routine without breaking it for everyone else.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: ReadParse and Hash Value by roboticus
in thread ReadParse and Hash Value by maruti

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.