I'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 = 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 = $#{ $sorted->[ $i ] };

#store the new reference to the $data record at the top of $sorted's stack
  $sorted->[ $i ]->[ $stack ] = \$data->[ $i ]->{ $cols[ $i ] };

#reference to place in $sorted so that it may be undefined later if necessary
  $data->[ $i ]->{ $cols[ $i ] }->[ 1 ] = \$sorted->[ $i ]->[ $stack ];

 }
}

#then, you could just loop through sorted. bypassing:
# sort { $data->[ $i ]->{ $a } <=> $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 <rob.di...@gmx.com>
Date: Thu, Aug 18, 2011 at 17:37
Subject: Re: self referential arrays/hashes ?
To: beginners@perl.org
Cc: Zak <b.zaryc...@gmail.com>


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-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/

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


Reply via email to