self referential arrays/hashes ? [sort]
am 19.08.2011 01:34:18 von Shawn WilsonI'm wondering (as i faced this a week ago or so), how much memory does
this take up (over the original data) ? and whether it's worth it (ie,
does this save me anything over a sort)? and is there a better way to
do this (if it doesn't take up much more memory and does save over a
sort can this be made more readable and is there a better way
altogether)?
so, if i have an array of matches:
my $data;
my $sorted;
while( <> ) {
my @cols =3D split /,/, $_;
for my $i ( 0 .. $#cols ) {
#counter for the unique element
$data->[ $i ]->{ $cols[ $i ] }->[ 0 ]++;
#undefine the array element in sorted if a $data reference was
previously defined
undef $sorted->[ $i ]->{ \$data->[ $i ]->{ $cols[ $i ] }->[ 1 ] } if
$data->[ $i ]->{ $cols[ $i ] }->[ 1 ];
#elements in $sorted
my $stack =3D $#{ $sorted->[ $i ] };
#store the new reference to the $data record at the top of $sorted's stack
$sorted->[ $i ]->[ $stack ] =3D \$data->[ $i ]->{ $cols[ $i ] };
#reference to place in $sorted so that it may be undefined later if necessa=
ry
$data->[ $i ]->{ $cols[ $i ] }->[ 1 ] =3D \$sorted->[ $i ]->[ $stack ];
}
}
#then, you could just loop through sorted. bypassing:
# sort { $data->[ $i ]->{ $a } <=3D> $data->[ $i ]->{ $b }
# } keys %{ $data->[ $i ] }
# with something like
for my $i ( 0 .. $#{ $sorted } } ) {
foreach my $j ( 0 .. $#{ $sorted->[ $i ] } ) {
print "column ". $i . ":" . $sorted->[ $i ]->[ $j ]->[ 1 ] . " had "
.. $sorted->[ $i ]->[ $j ]->[ 0 ] . " duplicates\n" if( $sorted->[ $i
]->[ $j ] );
}
}
---------- Forwarded message ----------
From: Rob Dixon
Date: Thu, Aug 18, 2011 at 17:37
Subject: Re: self referential arrays/hashes ?
To: beginners@perl.org
Cc: Zak
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=3D(A, B, C, D, $array[0], E, F)
>
> fly?
You can write
=A0my @array;
=A0@array=3D('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/
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/