Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
All:
I am currently working on my first OO project and would like to be able to do something like $record->Print; 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.

For complete details on the OO project, you can take a look at this link or you can just readmore for the output format I am looking for:

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=LRe +gion 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=Li +mbic_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

What I have currently looks something like this:

sub Print { my $hashref = shift; my ($key) = keys %{$hashref}; # @Parameters truncated for posting purposes only my @parameters = ('key','type','flags','Alias-2','Zip Code','Build +ing','Owner') foreach my $parm (@parameters) { next unless ($hashref->{$key}->{$parm}); my $value; if ($parm eq 'key') { $value = $key; } else { $value = $hashref->{$key}->{$parm}; } if (length($value) >= 62) { printf("%-16s: %s\n\t%s\n",$parm,substr($value,0,62),subst +r($value,62)); } else { printf("%-16s: %s\n",$parm,$value); } } }

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?

Thanks in advance - L~R


In reply to Printing a hash in a specific order? by Limbic~Region

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (12)
    As of 2014-08-22 19:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (163 votes), past polls