For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > June 2007 > Alternatives to highly nested hashes









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Alternatives to highly nested hashes
Mathew Snyder

2007-06-20, 7:59 am

I'm probably going to be doing some work with data that would, based on what I
already know of Perl, require me to create something along the lines of a
HoHoHoHoHoHoHoHoH...not exactly representative but you get the idea.

Is there a way to handle data with a lot of attributes other than creating a
hash like that?

Mathew
--
Keep up with me and what I'm up to: http://theillien.blogspot.com
Rob Dixon

2007-06-20, 7:59 am

Mathew Snyder wrote:
>
> I'm probably going to be doing some work with data that would, based on what I
> already know of Perl, require me to create something along the lines of a
> HoHoHoHoHoHoHoHoH...not exactly representative but you get the idea.
>
> Is there a way to handle data with a lot of attributes other than creating a
> hash like that?


Data with a lot of attributes in general generates hashes with many keys, not
highly-nested ones. Only highly-nested data does that! You are going to have
to describe better what it is you are processing for us to help you.

Rob
Mathew Snyder

2007-06-20, 7:59 am

It looks like an object is what I want. Am I correct? Suppose I need to work
with a bit of data that actually has 11 attributes. This would be an object of
another type. However, I need to manipulate pieces of it differently. So I'm
guessing I would create an object thusly:

sub objectname {
my %hashOfAttribs {
attrib1 => undef,
attrib2 => undef,
attrib3 => undef
}
}

I would then create an instance of that object

my $instance = new objectname();

I'm not certain though, how to populate the elements. would it actually be
my $instance = new objectname(attrib1 => value, attrib2 => value, attrib3 =>
value)? Or would I create the instance as above and then populate it by some
other means? For instance
$instance->hashOfAttribs {
attrib1 => value,
attrib2 => value,
attrib3 => value
};

Am I at least on the right track?

Mathew
Keep up with me and what I'm up to: http://theillien.blogspot.com
Mumia W.

2007-06-20, 7:59 am

On 06/20/2007 05:40 AM, Mathew Snyder wrote:
> It looks like an object is what I want. Am I correct?


As always, it depends.

> Suppose I need to work
> with a bit of data that actually has 11 attributes. This would be an object of
> another type. However, I need to manipulate pieces of it differently. So I'm
> guessing I would create an object thusly:
>
> sub objectname {
> my %hashOfAttribs {
> attrib1 => undef,
> attrib2 => undef,
> attrib3 => undef
> }
> }
>


No.

> I would then create an instance of that object
>
> my $instance = new objectname();
>
> I'm not certain though, how to populate the elements. would it actually be
> my $instance = new objectname(attrib1 => value, attrib2 => value, attrib3 =>
> value)? Or would I create the instance as above and then populate it by some
> other means? For instance
> $instance->hashOfAttribs {
> attrib1 => value,
> attrib2 => value,
> attrib3 => value
> };
>
> Am I at least on the right track?
>


Not really. First, it hasn't been conclusively established that you need
objects. You haven't described the data and what you want to do with it.

Second, objects are created using the methods described in "perldoc
perlboot," "perldoc perltoot" and "perldoc perltooc."

However, you can simplify things considerably by using Class::Struct, e.g.:

use Class::Struct ObjectName => [
attrib1 => '$',
attrib2 => '$',
attrib3 => '$',
children => '$',
];

...

my $instance = ObjectName->new(
attrib1 => '10',
attrib2 => [1, 20, 1943],
attrib3 => 'Hello',
children => []);



Rob Dixon

2007-06-20, 7:59 am

Mathew Snyder wrote:
> It looks like an object is what I want. Am I correct? Suppose I need to work
> with a bit of data that actually has 11 attributes. This would be an object of
> another type. However, I need to manipulate pieces of it differently. So I'm
> guessing I would create an object thusly:
>
> sub objectname {
> my %hashOfAttribs {
> attrib1 => undef,
> attrib2 => undef,
> attrib3 => undef
> }
> }
>
> I would then create an instance of that object
>
> my $instance = new objectname();
>
> I'm not certain though, how to populate the elements. would it actually be
> my $instance = new objectname(attrib1 => value, attrib2 => value, attrib3 =>
> value)? Or would I create the instance as above and then populate it by some
> other means? For instance
> $instance->hashOfAttribs {
> attrib1 => value,
> attrib2 => value,
> attrib3 => value
> };
>
> Am I at least on the right track?


Well, sort of. Objects are simply intelligent data structures - structures with
code as well as data that know how to perform operations on themselves. So you
still have to decide on your basic data structure first, and we still need to
know more about what the data is that you're trying to represent before we can
help!

What you've written above is pretty much correct expect that Perl classes (types
of objects) are packages not subroutines. But first lets be sure you really need
to create objects.

Rob


Brad Baxter

2007-06-21, 9:58 pm

On Jun 20, 7:33 am, rob.di...@350.com (Rob Dixon) wrote:
> Well, sort of. Objects are simply intelligent data structures - structures with
> code as well as data that know how to perform operations on themselves.


Not to put too fine a point on it, but early on when I was learning
OOP, I often read descriptions of objects that were similar to this
one, i.e., that objects "contained" code and could "perform operations
on themselves".

To my mind, this is a poor way to express things and, at least for me,
can lead to confusion. After faltering with Java for a while, it was
not until I learned how Perl handles OOP that the whole zoo of
concepts finally sank into my (admittedly less than razor sharp)
brain. In my opinion, a better and more accurate way to express it
would be something like:

Objects are data structures that are associated with code designed to
perform operations on them.

I have never seen an object perform an operation on itself. :-) I
have seen many methods perform operations on objects. I have seen
objects inform the system calling the method where that system may
find the proper code for that object.

So objects out in the wild are data structures that (usually) do not
have code "in" them. They must be fed to a method of their class,
which is where the code is. The objects DO have the information that
tells, say, Perl, in which class it should start looking for the
method.

Regards,

--
Brad

Uri Guttman

2007-06-21, 9:58 pm

>>>>> "BB" == Brad Baxter <baxter.brad@gmail.com> writes:

BB> Objects are data structures that are associated with code designed to
BB> perform operations on them.

BB> I have never seen an object perform an operation on itself. :-) I
BB> have seen many methods perform operations on objects. I have seen
BB> objects inform the system calling the method where that system may
BB> find the proper code for that object.

ever seen objects made from code refs? they could be just one code ref
and they can modify themselves if they are closures.

BB> So objects out in the wild are data structures that (usually) do not
BB> have code "in" them. They must be fed to a method of their class,
BB> which is where the code is. The objects DO have the information that
BB> tells, say, Perl, in which class it should start looking for the
BB> method.

and with code refs/closures as the object, the method IS the object
too. :)

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com