<?xml version="1.0" encoding="windows-1252"?>
<node id="243290" title="Printing a hash in a specific order?" created="2003-03-15 09:04:34" updated="2005-07-27 05:40:59">
<type id="115">
perlquestion</type>
<author id="180961">
Limbic~Region</author>
<data>
<field name="doctext">
All:
&lt;br&gt;
I am currently working on my first OO project and would like to be able to do something like &lt;CODE&gt;$record-&gt;Print;&lt;/CODE&gt; and have it correctly output the record in the proper format.  Unfortunately, the object is stored in a hash - and we know that the hash typically comes out differently than the order it went in.
&lt;p&gt;
For complete details on the OO project, you can take a look at [238963|this] link or you can just readmore for the output format I am looking for:
&lt;br&gt;
&lt;readmore&gt;
&lt;CODE&gt;key             : /C=US/A=BOGUS/P=ABC+DEF/O=CONN/OU=VALUE1/S=Region/G=Limbi
       c/I=_/
type            : UR
flags           : DIRM ADMINM ADIC ADIM 
Alias-2         : wL_Region
Alias-3         : Limbic Region
Alias-4         : Limbic._.Region@nowhere.com
Alias-5         : Limbic._.Region
Alias-6         : Limbic.Region@nowhere.com
Alias-7         : ORG
Alias-8         : CMP
Alias-10        : O=ORG/OU=Some Big Division/CN=Limbic _. Region
Alias-11        : Region
Alias-12        : Region, Limbic _
Alias-14        : Limbic _. Region at WT-CONN
Alias-15        : ex:/o=BLANK/ou=ORG/cn=Recipients/cn=Mailboxes/cn=LRegion
Alias-16        : WT:Limbic_Region
Alias-17        : SMTP:Limbic._.Region@nowhere.com
Alias-18        : /o=A.B.C.D./ou=Vermont Ave/cn=Recipients/cn=wt/cn=Limbic_Regi
       on
Full Name       : Region, Limbic _.
Post Office     : WT-CONN
Description     : 999-555-1212 Some Big Company
Tel             : 999-555-1212
Dept            : Some Big Division
Location        : EMWS
Address         : 123 nowhere street
City            : everywhere
State           : MD
Zip Code        : 20874
Building        : BLAH
Building-Code   : MD-ABC
owner           : CONN
&lt;/CODE&gt;
&lt;/readmore&gt;
&lt;p&gt;
What I have currently looks something like this:
&lt;br&gt;
&lt;CODE&gt;
sub Print {
    my $hashref = shift;
    my ($key) = keys %{$hashref};
    # @Parameters truncated for posting purposes only
    my @parameters = ('key','type','flags','Alias-2','Zip Code','Building','Owner')
    foreach my $parm (@parameters) {    
        next unless ($hashref-&gt;{$key}-&gt;{$parm});
        my $value;
        if ($parm eq 'key') {
            $value = $key;
        }
        else {
            $value = $hashref-&gt;{$key}-&gt;{$parm};
        } 
        if (length($value) &gt;= 62) {
            printf("%-16s: %s\n\t%s\n",$parm,substr($value,0,62),substr($value,62));
        }
        else {
            printf("%-16s: %s\n",$parm,$value);
        }
    }
}
&lt;/CODE&gt;
&lt;p&gt;
My question - is there a better/smarter way to do this?  I guess what I am asking is there a generic way to template hash output that I am unaware of?  Since this is my first OO project, I really didn't know what I was getting into.  I think if I were to start over, I would make my object a complex array instead of hash.  Is that also the wrong approach?
&lt;p&gt;
Thanks in advance - [Limbic~Region|L~R]
</field>
</data>
</node>
