Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Storing simple Value Pairs

by raybies (Chaplain)
on Oct 19, 2011 at 18:39 UTC ( #932460=perlquestion: print w/replies, xml ) Need Help??
raybies has asked for the wisdom of the Perl Monks concerning the following question:

Suppose I want an array of value pairs, suppose for the sake of arguments, vertices, like X,Y (though, they don't have to be numeric types, I'm thinking generic pairs). I want to keep the list of vertices in a list.

What's the best way to store/organize them in perl?

It seems so wasteful to use an array of hashes (key/value) for a single pair, with key names like X and Y.

%pair = ( X => 12, Y => 10); #then keep in a list like this... $pairlist[0] = \%pair;

And using the first (X) value as a key wouldn't work if you needed to repeat the value.

%pair = (12 => 10); # no list required, but then (12,10) and then (12,5) wouldn't work...
Of course, a simple array of arrays would work...
@pair = (12,10); #with a list assigned like... $pairlist[0] = \@pair;

I guess I'm curious what y'all do?

I mean, besides making an object, which is a pain for something this simple, especially if I don't supplement my Perl with something like Moose, which I don't... (well maybe it's not a pain for you... but I'm pretty weak there...) ;)

Replies are listed 'Best First'.
Re: Storing simple Value Pairs
by BrowserUk (Pope) on Oct 19, 2011 at 18:48 UTC

    I see no disadvantages with an AoA:

    #! perl -slw use strict; use constant { X=>0, Y=>1 }; my @pairs = map[ int( rand 1000 ), int( rand 1000 ) ], 1 .. 1000; print join ' : ', $pairs[ 999 ][X], $pairs[ 999 ][Y]; print join ' : ', @{ $pairs[ 500 ] }; __END__ C:\test>junk22 935 : 575 669 : 471

    Do you?

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Storing simple Value Pairs
by ikegami (Pope) on Oct 19, 2011 at 18:45 UTC

    It's only wasteful if you need the memory for something else. The AoA sounds right, although I'd write it as

    $pairlist[0] = [ 12, 10 ];
Re: Storing simple Value Pairs
by Marshall (Abbot) on Oct 19, 2011 at 18:54 UTC
    What's the best way to store/organize them in perl?

    That depends upon how you want to access the data. Looks to me like you pretty much understand the options. The 2-D AoA is a logical choice if you just need a sequential list of coordinates.

    The AoH is also commonly done (your option 1). "Wasteful" or not is a subjective thing. For something simple this can take less storage than you might think. This is similar to a C array of struct.

    In your option (2), use HoA (hash of array) instead of a simple hash if you want to represent more than one point with the same X value. I would make all of the hash entries pointers to array (not do some as simple values if only one) just to make things more "regular" and eliminate special case.

Re: Storing simple Value Pairs
by jeffa (Bishop) on Oct 19, 2011 at 18:43 UTC

    I do not feel that using a hash here is wasteful whatsoever, so i would comfortably use a hash to store these values. They are called X and Y, after all, and not 0 and 1. I certainly would have no aversion to using a Moose object either.

    has X => ( is => 'rw' ); has Y => ( is => 'rw' );


    (the triplet paradiddle with high-hat)
      ...perhaps if Moose were core, I wouldn't have a problem with it either, but at this point, installation is quite labor intensive (network airgap), and an assortment of firewalls hurdles...
Re: Storing simple Value Pairs
by pvaldes (Chaplain) on Oct 19, 2011 at 18:44 UTC

    what about a simple list? i.e.:

     my @list = ('23/34', '34/56', '45/01');

    is trivial to convert this to the original pair with split, sort the list, etc

    foreach $pair(@list){ my ($x,$y) = split /\//, $pair, 2; print $y," is paired with ", $x, "\n"; } __END__ 34 is paired with 23 56 is paired with 34 01 is paired with 45
Re: Storing simple Value Pairs
by raybies (Chaplain) on Oct 19, 2011 at 19:02 UTC
    Thanks to all Monks! Lots of interesting possibilities suggested. I guess sometimes I get a bit wrapped around nesting too many perl structs, but I like how many suggestions you've given to how to organize it better. As always I bow to your enlightenment. :)

      Hi, just joined minutes ago. I am trying join tab-delimited files into a single file/table

      Example, say I have these 4 files/tables:

      ID value (table1)

      Aa 22

      Bb 28

      Cc 32

      Dd 50

      ID value (table2)

      Aa 34

      Cc 112

      Dd 77

      Ee 89

      Kk 124

      ID value (table3)

      Bb 75

      Cc 91

      Dd 132

      ID value (table4)

      Aa 66

      Cc 94

      Ee 213

      Gg 250

      The output after joining should look like this:

      ID value1 value2 value3 value4

      Aa 22 34 0 66

      Bb 28 0 75 0

      Cc 32 112 91 94

      Dd 50 77 132 0

      Ee 0 89 0 213

      Gg 0 0 0 250

      Kk 0 124 0 0

      My best effort:

      #usr/bin/perl! use strict; #I opened all files (containg the tables) one by one; is #there a way +I can open all files at once? open(FILEH1, "<table1.txt"); while (my $file = <FILEH1>){ chomp $file; my @file1 = split('\t', $file); #to pick IDs and #values => $fil +e1[0] and $file1[1] } open(FILEH2, "<table2.txt"); #and continued to tables 3 and 4. #And then I tried to collect the items which is where I got #stuck.
        Your post has nothing to do with a reply to the original post (OP).

        I suggest that you start a new question.

        I see that you do understand how to use <code>...</code> tags, so format this critter a bit better.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://932460]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-09-21 23:35 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (254 votes). Check out past polls.