moving structures

E

erehwon

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
 
E

erehwon

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
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

$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.
 
W

Walter Roberson

: 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}}];
 
E

erehwon

erehwon said:
: 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
 
E

erehwon

thanks Jeff!


Jeff 'japhy' Pinyan said:
[posted & mailed]

$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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top