Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

078274121, info0a ip, should prodicers of software_based services be held liable or not liable for economic injuries, should producers of soft ware based services such as ATMs be held liable for economic injuries suffered when their systems fail?, nisc wwwxxx, wwwxxx0cm, should producers of software-based services, such as atms, be held liable for economic injuries suffered when their systems fail?, wwwxxx0cm, www.webdp.net, Event 9 IIS log failed to write entry

Links

XODOX
Impressum

#1: self referential arrays/hashes ? [sort]

Posted on 2011-08-19 01:34:18 by Shawn Wilson

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 =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 <rob.dixon@gmx.com>
Date: Thu, Aug 18, 2011 at 17:37
Subject: Re: self referential arrays/hashes ?
To: beginners@perl.org
Cc: Zak <b.zarychta@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=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/

Report this message