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

Re^3: Nested Data Structures for Dummies?

by GrandFather (Sage)
on Apr 15, 2010 at 01:12 UTC ( #834807=note: print w/replies, xml ) Need Help??

in reply to Re^2: Nested Data Structures for Dummies?
in thread Nested Data Structures for Dummies?

The key thing to remember is that you can only store a scalar as a data structure value. So in the context of an array each element is a scalar - the element's value may reference something more interesting like another array, but the value is a scalar.

You may find it helps to assign the reference to a temporary variable with a suitable name even in trivial cases so that you make it clear to yourself that what you are dealing with is a reference. Only the very innermost element can be a non-reference scalar value (a string or number for example).

It may help to show us some of the code and the data structures that give you grief so we have a concrete example to work with that applies to what you are using.

True laziness is hard work
  • Comment on Re^3: Nested Data Structures for Dummies?

Replies are listed 'Best First'.
Re^4: Nested Data Structures for Dummies?
by jedikaiti (Hermit) on Apr 15, 2010 at 02:50 UTC
    How about this?
    %hash = { 'some item' = > { 'source' => 'a source', 'conversions' => [ { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2' }, { 'conversion 3' => 'value 3', 'conversion 4' => 'value 4' 'conversion 5' => 'value 5' } ], 'whatever' => 'eh' }, 'another item' = > { 'source' => 'another source', 'conversions' => [ { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2' } ], 'whatever' => 'eh' }, 'one more item' = > { 'source' => 'another source', 'whatever' => 'eh' }, }
    Swiss Army Nerd

      So what do you want to do with it? I'd be inclined to turn it into an object then write a few accessor methods to pull out interesting fragments. Consider:

      use strict; use warnings; my %hash = ( 'some item' => { 'source' => 'a source', 'conversions' => { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2', 'conversion 3' => 'value 3', 'conversion 4' => 'value 4', 'conversion 5' => 'value 5', }, 'whatever' => 'eh', }, 'another item' => { 'source' => 'another source', 'conversions' => { 'conversion 1' => 'value 1', 'conversion 2' => 'value 2', }, 'whatever' => 'eh', }, 'one more item' => { 'source' => 'another source', 'whatever' => 'eh', }, ); my $obj = bless {data => \%hash}; for my $item ($obj->getItems ()) { print "$item->{name}: $item->{source}, $item->{whatever}\n"; } my %conversions = $obj->getConversions ('some item'); print "$_ => $conversions{$_}\n" for sort keys %conversions; sub getItems { my ($self) = @_; my $data = $self->{data}; return map {{name => $_, %{$data->{$_}}}} keys %$data; } sub getItem { my ($self, $itemName) = @_; return if ! exists $self->{data}{$itemName}; return $self->{data}{$itemName}; } sub getConversions { my ($self, $itemName) = @_; return if ! exists $self->{data}{$itemName}; my $item = $self->{data}{$itemName}; return if ! exists $item->{conversions}; return %{$item->{conversions}}; }


      some item: a source, eh another item: another source, eh one more item: another source, eh conversion 1 => value 1 conversion 2 => value 2 conversion 3 => value 3 conversion 4 => value 4 conversion 5 => value 5

      Such an approach is worth doing if your data structure has a consistent structure and you need to perform a bunch of queries against it. If the data is not consistently structured then it's going to be endless hard work no matter what because you simply have to deal with special cases and that is always a source of pain.

      Note that I fixed a bunch of syntax errors in the structure you gave so I presume that your sample doesn't map on to anything real in any sense.

      True laziness is hard work

        Yea, it's a very generalized form of the actual structure I have. The part that's giving me a headache at the moment is getting the conversions out, one at a time.

        Going to go through your code right now. Thank you!

        Swiss Army Nerd

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://834807]
[marioroy]: LA, yeah. than there's no reason why it cannot work inside qx. But chatting is hard in PM. I cannot see the code now.
[shmem]: Lady_Aleena: sometimes a quick glance isn't enough.
[Lady_Aleena]: What?! Can't stat ~/Music: No such file or directory? The ~/ doesn't expand?
[Lady_Aleena]: And the env variable $HOME doesn't work either. !@#$
[shmem]: no, that's a shell feature. Use $ENV{HOME}
[marioroy]: my $ans = qx{ find \$HOME -name "*.pl" -exec ls -l {} \\; };

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-04-23 21:00 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (432 votes). Check out past polls.