self referential arrays/hashes ?

self referential arrays/hashes ?

am 18.08.2011 16:51:04 von Zak

can you reference elements of an array/hash from within that array/
hash for example would

@array=(A, B, C, D, $array[0], E, F)

fly?

Zak


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: self referential arrays/hashes ?

am 18.08.2011 21:58:49 von rvtol+usenet

On 2011-08-18 16:51, Zak wrote:

> can you reference elements of an array/hash from within that array/
> hash for example would
>
> @array=(A, B, C, D, $array[0], E, F)
>
> fly?

See Data::Alias and Array::RefElem.

perl -Mstrict -MData::Dumper -MData::Alias -wle '

my @data = qw(A B C D A E F A B B A);

for my $i ( 0 .. $#data ) {
for my $j ( $i + 1 .. $#data ) {
next if $data[$j] ne $data[$i];
alias $data[$j] = $data[$i];
}
}

$data[0] = "X";
print "@data";

print Dumper( \@data );
'

X B C D X E F X B B X

$VAR1 = [
'X',
'B',
'C',
'D',
${\$VAR1->[0]},
'E',
'F',
${\$VAR1->[0]},
${\$VAR1->[1]},
${\$VAR1->[1]},
${\$VAR1->[0]}
];

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: self referential arrays/hashes ?

am 18.08.2011 23:37:44 von Rob Dixon

On 18/08/2011 15:51, Zak wrote:
>
> can you reference elements of an array/hash from within that array/
> hash for example would
>
> @array=(A, B, C, D, $array[0], E, F)
>
> fly?

You can write

my @array;
@array=('A', 'B', 'C', 'D', \$array[0], 'E', 'F');

What are you trying to do?

Rob


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: self referential arrays/hashes ?

am 19.08.2011 00:03:53 von Jim Gibson

On 8/18/11 Thu Aug 18, 2011 7:51 AM, "Zak"
scribbled:

> can you reference elements of an array/hash from within that array/
> hash for example would
>
> @array=(A, B, C, D, $array[0], E, F)
>
> fly?

Try it out. See what you get.

The above will not set the fifth element of @array to 'A', however, if that
is what you are trying to do. What will actually happens depends upon the
state of @array when this statement is executed.

The left-hand side of the assignment statement is a list, which will be
created as the first part of the statement execution. The list will then be
assigned to the array variable @array. The value of the fifth element of the
list will be assigned from the current value of the fifth element of @array.
If this is the first time @array has been assigned a value, then that
element will be null. If, in addition, you have 'use strict;' and have not
declared @array previous to this statement, you will get a compilation error
("Global symbol "@array" requires explicit package name at ...").

Do this instead:

my @array = qw( A B C D X E F );
$array[4] = $array[0];



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: self referential arrays/hashes ?

am 19.08.2011 00:41:16 von Shawn H Corey

On 11-08-18 10:51 AM, Zak wrote:
> @array=(A, B, C, D, $array[0], E, F)

This is the same as:

{
my @temporary = @array;
@array = ( A, B, C, D, $temporary[0], E, F );
}


--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

"Make something worthwhile." -- Dear Hunter

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: self referential arrays/hashes ?

am 19.08.2011 07:26:15 von rvtol+usenet

On 2011-08-18 21:58, Dr.Ruud wrote:
> On 2011-08-18 16:51, Zak wrote:

>> can you reference elements of an array/hash from within that array/
>> hash for example would
>> @array=(A, B, C, D, $array[0], E, F)
>> fly?
>
> See Data::Alias and Array::RefElem.
>
> perl -Mstrict -MData::Dumper -MData::Alias -wle '
>
> my @data = qw(A B C D A E F A B B A);
>
> for my $i ( 0 .. $#data ) {

Better: for my $i ( 0 .. $#data - 1 ) {

> for my $j ( $i + 1 .. $#data ) {
> next if $data[$j] ne $data[$i];
> alias $data[$j] = $data[$i];
> }
> }
>
> $data[0] = "X";
> print "@data";
>
> print Dumper( \@data );
> '


--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/