Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

First I'd suggest using composite objects. It appears that you're not yet using multidimensional arrays/hashes, and I think that's part of the problem. Going by the variable names, I think you could improve things a good deal. First, here's what I'm thinking your variables mean:

my $alias_line_count = 0; # Number of aliases I have my @alias_label_array = (); my @alias_value_array = (); my @officer_label_array = (); my @officer_value_array = (); my $officer_count = 0; # Number of officers I have my @ap_label_array = (); my @ap_value_array = (); my $ap_line_count = 0; # Number of APs I have my $ap_person_count = -1; # Eh? I don't know...

If that's the case, then it looks like you're probably using code like this:

# Print the data for officer 'Joe' for my $i (0 .. $officer_count-1) { if ($officer_label_array[$i] eq 'Joe') { print "Joe: $officer_value_array[$i]\n"; } }

In perl, you can ask a hash or array how many elements it has, so you don't need to store the size in a separate value. So we could convert it to something like this:

for my $i (0 .. $#officer_label_array) { if ($officer_label_array[$i] eq 'Joe') { print "Joe: $officer_value_array[$i]\n"; } }

If you use a multidimensional array, though, you could simplify things. Here, we'll use a second dimension on your array where element 0 is the name, and element 1 is the value:

for my $i (0 .. $#officer_label_value_array) { if ($officer_label_value_array[$i][0] eq 'Joe') { print "Joe: $officer_label_value_array[$i][1]\n"; } }

One nice thing about multidimensional arrays like this is that frequently you can solve the problem *without* using the row number. Instead you can just loop through the array:

for my $array_reference (@officer_label_value_array) { my ($name, $value) = @$array_reference; print "$name: $value\n" if $name eq 'Joe'; }

Of course, if you're actually just looping through arrays to find things, you'd be better served with a hash, as you can simply access each value by name:

my $name = 'Joe'; print "$name: $officer_hash{$name}\n" if exists $officer_hash{$name};

Finally, I don't really see the point of having suffixes like "_array", "_hash", etc., so I think I'd simplify things like:

my %aliases = { # Name Title Joe => 'Prez', Bob => 'Veep' }; my %officers = { # Name Salary Prez => 100000, Veep => 85000 }; # Who's Joe, and how much money does he make? my $title = $aliases{Joe}; my $salary = $officers{$title}; print "Joe's title is $title, and he earns $salary yearly.\n"; # How many officers do we have, anyway? print "We have ", scalar(keys %officers), " officers.\n";

Frequently, a few well-named global hashes are fine for simple, small programs. But when your programs grow and you need to start passing things around, it's pretty simple when you use better data structures.

.... Ack! I'm late for work. I'll try to continue a little later...


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

In reply to Re: data structure question by roboticus
in thread data structure question by Anonymous Monk

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (5)
    As of 2018-03-18 16:29 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (230 votes). Check out past polls.