simple hash of arrays, I cant see the trees for the forest

Discussion in 'Perl Misc' started by bpatton, Aug 24, 2006.

  1. bpatton

    bpatton Guest

    I'm trying to get something on the order of

    $VAR1 = {
    'cell' => {
    '1A' => {
    'pass' => {
    'ACTIVE' => {
    [ 0 , 0 , 0 , 240 , 240 , 240 , ... ] ,
    [ 1 , 2 , 3 , 4 , 5 , 6 , ... ] ,
    },
    },
    'fail'=> {
    'ACTIVE'=>{
    ....


    This shoudl be very easy to do, but I've been working on my AC and had
    a beer on an empty
    stomach.
    Here's some sample sode that soes not work.
    #!/usr/local/bin/perl
    use strict;
    use warnings;

    use Data::Dumper; $Data::Dumper::Indent = 1;
    my $data;
    my $x = 0;
    my $y = 0;
    my $dis = 240;
    my $rule = '1A';
    my $layer = 'ACTIVE';
    $data->{cell}->{$rule}->{pass}->{$layer} =
    ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    $x = $dis * 3;
    $y = $dis * 3;
    $data->{cell}->{$rule}->{pass}->{$layer} =
    ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    $dis = int($dis / 2);
    $data->{cell}->{$rule}->{fail}->{$layer} =
    ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    $x = $dis * 3;
    $y = $dis * 3;
    $data->{cell}->{$rule}->{fail}->{$layer} =
    ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    print Dumper($data);
    bpatton, Aug 24, 2006
    #1
    1. Advertising

  2. bpatton

    Xicheng Jia Guest

    bpatton wrote:
    > I'm trying to get something on the order of
    >
    > $VAR1 = {
    > 'cell' => {
    > '1A' => {
    > 'pass' => {
    > 'ACTIVE' => {
    > [ 0 , 0 , 0 , 240 , 240 , 240 , ... ] ,
    > [ 1 , 2 , 3 , 4 , 5 , 6 , ... ] ,
    > },
    > },
    > 'fail'=> {
    > 'ACTIVE'=>{
    > ...
    >
    >
    > This shoudl be very easy to do, but I've been working on my AC and had
    > a beer on an empty
    > stomach.
    > Here's some sample sode that soes not work.
    > #!/usr/local/bin/perl
    > use strict;
    > use warnings;
    >
    > use Data::Dumper; $Data::Dumper::Indent = 1;
    > my $data;
    > my $x = 0;
    > my $y = 0;
    > my $dis = 240;
    > my $rule = '1A';
    > my $layer = 'ACTIVE';
    > $data->{cell}->{$rule}->{pass}->{$layer} =
    > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > $x = $dis * 3;
    > $y = $dis * 3;
    > $data->{cell}->{$rule}->{pass}->{$layer} =
    > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > $dis = int($dis / 2);
    > $data->{cell}->{$rule}->{fail}->{$layer} =
    > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > $x = $dis * 3;
    > $y = $dis * 3;
    > $data->{cell}->{$rule}->{fail}->{$layer} =
    > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > print Dumper($data);


    you may want to change some braces into brackets, like:

    $data->{cell}->{$rule}->{pass}->{$layer} =
    [ $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y ];

    Good luck,
    Xicheng
    Xicheng Jia, Aug 24, 2006
    #2
    1. Advertising

  3. bpatton

    Xicheng Jia Guest

    Xicheng Jia wrote:
    > bpatton wrote:
    > > I'm trying to get something on the order of
    > >
    > > $VAR1 = {
    > > 'cell' => {
    > > '1A' => {
    > > 'pass' => {
    > > 'ACTIVE' => {
    > > [ 0 , 0 , 0 , 240 , 240 , 240 , ... ] ,
    > > [ 1 , 2 , 3 , 4 , 5 , 6 , ... ] ,
    > > },
    > > },
    > > 'fail'=> {
    > > 'ACTIVE'=>{
    > > ...
    > >
    > >
    > > This shoudl be very easy to do, but I've been working on my AC and had
    > > a beer on an empty
    > > stomach.
    > > Here's some sample sode that soes not work.
    > > #!/usr/local/bin/perl
    > > use strict;
    > > use warnings;
    > >
    > > use Data::Dumper; $Data::Dumper::Indent = 1;
    > > my $data;
    > > my $x = 0;
    > > my $y = 0;
    > > my $dis = 240;
    > > my $rule = '1A';
    > > my $layer = 'ACTIVE';
    > > $data->{cell}->{$rule}->{pass}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > $x = $dis * 3;
    > > $y = $dis * 3;
    > > $data->{cell}->{$rule}->{pass}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > $dis = int($dis / 2);
    > > $data->{cell}->{$rule}->{fail}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > $x = $dis * 3;
    > > $y = $dis * 3;
    > > $data->{cell}->{$rule}->{fail}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > print Dumper($data);

    >
    > you may want to change some braces into brackets, like:
    >
    > $data->{cell}->{$rule}->{pass}->{$layer} =
    > [ $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y ];


    s/braces/parentheses/;

    --
    XC
    Xicheng Jia, Aug 24, 2006
    #3
  4. bpatton

    bpatton Guest

    That did it , thanks :)

    Xicheng Jia wrote:
    > bpatton wrote:
    > > I'm trying to get something on the order of
    > >
    > > $VAR1 = {
    > > 'cell' => {
    > > '1A' => {
    > > 'pass' => {
    > > 'ACTIVE' => {
    > > [ 0 , 0 , 0 , 240 , 240 , 240 , ... ] ,
    > > [ 1 , 2 , 3 , 4 , 5 , 6 , ... ] ,
    > > },
    > > },
    > > 'fail'=> {
    > > 'ACTIVE'=>{
    > > ...
    > >
    > >
    > > This shoudl be very easy to do, but I've been working on my AC and had
    > > a beer on an empty
    > > stomach.
    > > Here's some sample sode that soes not work.
    > > #!/usr/local/bin/perl
    > > use strict;
    > > use warnings;
    > >
    > > use Data::Dumper; $Data::Dumper::Indent = 1;
    > > my $data;
    > > my $x = 0;
    > > my $y = 0;
    > > my $dis = 240;
    > > my $rule = '1A';
    > > my $layer = 'ACTIVE';
    > > $data->{cell}->{$rule}->{pass}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > $x = $dis * 3;
    > > $y = $dis * 3;
    > > $data->{cell}->{$rule}->{pass}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > $dis = int($dis / 2);
    > > $data->{cell}->{$rule}->{fail}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > $x = $dis * 3;
    > > $y = $dis * 3;
    > > $data->{cell}->{$rule}->{fail}->{$layer} =
    > > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);
    > > print Dumper($data);

    >
    > you may want to change some braces into brackets, like:
    >
    > $data->{cell}->{$rule}->{pass}->{$layer} =
    > [ $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y ];
    >
    > Good luck,
    > Xicheng
    bpatton, Aug 24, 2006
    #4
  5. bpatton

    Uri Guttman Guest

    >>>>> "b" == bpatton <> writes:

    b> That did it , thanks :)

    regardless of the fix, your code leave much to be desired. perhaps you
    should drink a better beer on an empty stomach?

    >> > my $data;
    >> > my $x = 0;
    >> > my $y = 0;
    >> > my $dis = 240;
    >> > my $rule = '1A';
    >> > my $layer = 'ACTIVE';
    >> > $data->{cell}->{$rule}->{pass}->{$layer} =
    >> > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);


    you don't need -> between sets of [] or {}.

    >> > $data->{cell}->{$rule}->{pass}->{$layer} =
    >> > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);


    all that redundancy is slow and very ugly. get a ref to the lowest level
    that you are munging and use that:

    my $rule_ref = \$data->{cell}{$rule} ;

    my $coords = [$x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y] ;

    $rule_ref->{pass}{$layer} = $coords ;

    >> > $dis = int($dis / 2);
    >> > $data->{cell}->{$rule}->{fail}->{$layer} =
    >> > ( $x,$y , $x,$y+$dis , $dis,$dis , $dis,$y , $x,$y);


    $rule_ref->{fail}{$layer} = $coords ;

    i did that as it seems you are using the same coord stuff. if not,
    create a new anon ref with the values you want. there is definitely
    redundancy there that can be removed but i won't say exactly what it is
    as i don't know your real data structure needs. my gut feeling is that
    you are overdesigning this and it is more complex than you need.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Uri Guttman, Aug 24, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kamilche

    Python 'Lets Me See The Forest'

    Kamilche, Jun 4, 2004, in forum: Python
    Replies:
    7
    Views:
    294
    James Moughan
    Jun 8, 2004
  2. News
    Replies:
    5
    Views:
    524
    Dennis Lee Bieber
    Apr 14, 2006
  3. Steve Kershaw
    Replies:
    1
    Views:
    338
    Brennan Stehling
    Sep 26, 2006
  4. Nagaraj
    Replies:
    1
    Views:
    823
    Lionel B
    Mar 1, 2007
  5. flebber
    Replies:
    5
    Views:
    411
    Jean-Paul Calderone
    Dec 26, 2010
Loading...

Share This Page