in reply to ReadParse and Hash Value
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.
|
|---|