Bookmarks

Yahoo Gmail Google Facebook Delicious Twitter Reddit Stumpleupon Myspace Digg

Search queries

www.webdp.net, Event 9 IIS log failed to write entry, wwwxxx jeffs, Catastrophic failure Unexpected method call sequence. 0x8000ffff (-2147418113)., ksh lock a file, [unixODBC][Driver Manager]Driver's SQLAllocHandle on SQL_HANDLE_DBC failed, sed: -e expression #1, char 1: unterminated address regex, procmail + change subject, w2ksp4.exe download, /proc/kallsyms format

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