moving structures

Discussion in 'Perl Misc' started by erehwon, Feb 9, 2004.

  1. erehwon

    erehwon Guest

    hi,
    I'm trying to rearrange some lists, but am having problems(!) I'm quite
    new to perl...

    #some vectors
    $a = [( 1,2,3)];
    $b = [( 3,2,3)];
    $c = [( 4,5,6)];

    # lists of vectors - each list starting out containing one vector
    $data{$i++} = [($a)];
    $data{$i++} = [($b)];
    $data{$i++} = [($c)];

    now I want to merge $data{0} and $data{1} so I do:

    $data{0} = [(@$data{0},@$data{1})];
    $data{1} = undef;

    but this doesn't work. It should effectively make the lists appear as
    though I'd set them up like this in the first place:
    $data{$i++} = [($a,$b)];
    $data{$i++} = [($c)];

    (my print routine looks like this:)

    sub printdata
    {
    my $t = shift;
    my $c;
    foreach $c (@$t) {
    print "( $$c[0], $$c[1],$$c[2] )";
    }
    print "\n";
    }

    and will print with:
    foreach $i (sort keys $data )
    {
    printdata $data{$i};
    }

    can anyone tell me what I'm doing wrong when trying to merge the lists?
    thanks,
    JJ
     
    erehwon, Feb 9, 2004
    #1
    1. Advertising

  2. erehwon

    erehwon Guest

    ahh.. it's a problem with precedence I believe, seeing as the following
    works:
    $l = $data{0};
    $r = $data{1};
    $data{0} = [( @$l, @$r )];

    But this is daft!... how do I do this in one line? :eek:)
    thanks,
    JJ


    "erehwon" <> wrote in message
    news:c06ki0$ad4$...
    > hi,
    > I'm trying to rearrange some lists, but am having problems(!) I'm quite
    > new to perl...
    >
    > #some vectors
    > $a = [( 1,2,3)];
    > $b = [( 3,2,3)];
    > $c = [( 4,5,6)];
    >
    > # lists of vectors - each list starting out containing one vector
    > $data{$i++} = [($a)];
    > $data{$i++} = [($b)];
    > $data{$i++} = [($c)];
    >
    > now I want to merge $data{0} and $data{1} so I do:
    >
    > $data{0} = [(@$data{0},@$data{1})];
    > $data{1} = undef;
    >
    > but this doesn't work. It should effectively make the lists appear as
    > though I'd set them up like this in the first place:
    > $data{$i++} = [($a,$b)];
    > $data{$i++} = [($c)];
    >
    > (my print routine looks like this:)
    >
    > sub printdata
    > {
    > my $t = shift;
    > my $c;
    > foreach $c (@$t) {
    > print "( $$c[0], $$c[1],$$c[2] )";
    > }
    > print "\n";
    > }
    >
    > and will print with:
    > foreach $i (sort keys $data )
    > {
    > printdata $data{$i};
    > }
    >
    > can anyone tell me what I'm doing wrong when trying to merge the lists?
    > thanks,
    > JJ
    >
    >
    >
     
    erehwon, Feb 9, 2004
    #2
    1. Advertising

  3. [posted & mailed]

    On Mon, 9 Feb 2004, erehwon wrote:

    >$a = [( 1,2,3)];
    >$b = [( 3,2,3)];
    >$c = [( 4,5,6)];
    >
    ># lists of vectors - each list starting out containing one vector
    >$data{$i++} = [($a)];
    >$data{$i++} = [($b)];
    >$data{$i++} = [($c)];
    >
    >now I want to merge $data{0} and $data{1} so I do:
    >
    >$data{0} = [(@$data{0},@$data{1})];
    >$data{1} = undef;


    The @ in @$data{0} is binding to $data, not to $data{0}.

    $data{0} = [ @{ $data{0} }, @{ $data{1} } ];
    delete $data{$1};

    Or, more succintly:

    push @{ $data{0} }, @{ delete $data{1} };

    I think.

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
     
    Jeff 'japhy' Pinyan, Feb 9, 2004
    #3
  4. In article <c06ki0$ad4$>,
    erehwon <> wrote:
    : I'm trying to rearrange some lists, but am having problems(!) I'm quite
    :new to perl...

    :#some vectors
    :$a = [( 1,2,3)];
    :$b = [( 3,2,3)];
    :$c = [( 4,5,6)];

    :# lists of vectors - each list starting out containing one vector
    :$data{$i++} = [($a)];
    :$data{$i++} = [($b)];
    :$data{$i++} = [($c)];

    That isn't a list of vectors -- that is a hash of vectors.

    :now I want to merge $data{0} and $data{1} so I do:

    :$data{0} = [(@$data{0},@$data{1})];
    :$data{1} = undef;

    $data{0} = [@{$data{0}},@{$data{1}}];
    --
    Will you ask your master if he wants to join my court at Camelot?!
     
    Walter Roberson, Feb 9, 2004
    #4
  5. erehwon

    erehwon Guest

    > erehwon <> wrote:
    > : I'm trying to rearrange some lists, but am having problems(!) I'm quite
    > :new to perl...
    >
    > :#some vectors
    > :$a = [( 1,2,3)];
    > :$b = [( 3,2,3)];
    > :$c = [( 4,5,6)];
    >
    > :# lists of vectors - each list starting out containing one vector
    > :$data{$i++} = [($a)];
    > :$data{$i++} = [($b)];
    > :$data{$i++} = [($c)];
    >
    > That isn't a list of vectors -- that is a hash of vectors.


    Hmm.. I thought it was a hash of references to lists of references of lists
    of scalars.

    > :now I want to merge $data{0} and $data{1} so I do:
    >
    > :$data{0} = [(@$data{0},@$data{1})];
    > :$data{1} = undef;
    >
    > $data{0} = [@{$data{0}},@{$data{1}}];


    many thanks,
    JJ
     
    erehwon, Feb 9, 2004
    #5
  6. erehwon

    erehwon Guest

    thanks Jeff!


    "Jeff 'japhy' Pinyan" <> wrote in message
    news:p...
    > [posted & mailed]
    >
    > On Mon, 9 Feb 2004, erehwon wrote:
    >
    > >$a = [( 1,2,3)];
    > >$b = [( 3,2,3)];
    > >$c = [( 4,5,6)];
    > >
    > ># lists of vectors - each list starting out containing one vector
    > >$data{$i++} = [($a)];
    > >$data{$i++} = [($b)];
    > >$data{$i++} = [($c)];
    > >
    > >now I want to merge $data{0} and $data{1} so I do:
    > >
    > >$data{0} = [(@$data{0},@$data{1})];
    > >$data{1} = undef;

    >
    > The @ in @$data{0} is binding to $data, not to $data{0}.
    >
    > $data{0} = [ @{ $data{0} }, @{ $data{1} } ];
    > delete $data{$1};
    >
    > Or, more succintly:
    >
    > push @{ $data{0} }, @{ delete $data{1} };
    >
    > I think.
    >
    > --
    > Jeff Pinyan RPI Acacia Brother #734 2003 Rush

    Chairman
    > "And I vos head of Gestapo for ten | Michael Palin (as Heinrich

    Bimmler)
    > years. Ah! Five years! Nein! No! | in: The North Minehead

    Bye-Election
    > Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
    >
     
    erehwon, Feb 9, 2004
    #6
    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. tweak
    Replies:
    14
    Views:
    2,818
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    753
    Emmanuel Delahaye
    Sep 24, 2005
  3. linkswanted
    Replies:
    0
    Views:
    1,270
    linkswanted
    Dec 21, 2007
  4. linkswanted
    Replies:
    0
    Views:
    1,601
    linkswanted
    Jan 6, 2008
  5. linkswanted
    Replies:
    0
    Views:
    468
    linkswanted
    Jan 23, 2008
Loading...

Share This Page