Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
P is for Practical
 
PerlMonks  

Re: data structure question

by roboticus (Canon)
on Aug 30, 2013 at 12:29 UTC ( #1051617=note: print w/ replies, xml ) Need Help??


in reply to data structure question

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...

...roboticus

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


Comment on Re: data structure question
Select or Download Code
Re^2: data structure question
by Anonymous Monk on Aug 30, 2013 at 12:37 UTC
    thanks roboticus! Actually my code (with the exception of generally letting an array or hash tell how big it is), looks pretty similar to what you suggest. I am using multi-dimensional arrays and hashes to good effect. My programs work well; my question was posited on the idea that they don't look very 'Perl-ish'.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1051617]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-04-20 00:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls