[Date Prev][Date Next][Thread Prev][Thread Next]
[Search]
[Date Index]
[Thread Index]
RE: [FWP] From "The Art of Perl", Ch. 1
Quoth Jeff Pinyan [mailto:jeffp@crusoe.net]:
:
: I'm looking for some idioms for dealing with arrays, hashes,
: and unnamed
: lists. For more information on "The Art of Perl", visit:
:
: http://magneton-59.dynamic.rpi.edu/book/
:
: Submissions I incorporate will be acknowledged.
Please don't. :-)
: Operating on Lists
: ------------------
:
: array-to-hash
:
: while (($k,$v) = each %{{ @list }}){ ... }
: while ($i < @list){ ($k,$v) = @list[$i++,$i++]; ... }
# obvious
%hash = @array;
# Some silly ways to get similar results (build %hash from @array)
# Silly idiom no. 1
for($i = $[; $i <= $#array; $i++)
{
($i % 2) and $hash{$array[$i-1]} = $array[$i];
}
# Corollary to silliy idiom no. 1
for($i = $[; $i <= $#array; $i++)
{
!($i % 2) and $hash{$array[$i]} = $array[$i+1];
}
# Silly idiom no. 2
@hash{ grep{$i++;$i % 2} @array } = grep{$j++;not $j % 2} @array;
# Silly idiom no. 3
for($i = $[; $i <= $#array; $i+=2)
{
$hash{$array[$i]} = $array[$i+1];
}
# Silly idiom no. 4
%hash = map{ $i++; ($i % 2) and ($k => $_); $k = $_ } @array;
# Lastly, do it real stupid and hose @array
while(@array)
{
$hash{pop @array} = pop @array;
}
That's all the fun I can stand for today; my ride is here.
Eli
________________________
Eli Evans
Text Preparation Manager
Libronix Corp.
mailto:eli@libronix.com
http://www.libronix.com
Ec 12:12; Jn 21:22
==== Want to unsubscribe from Fun With Perl? Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
==== unsubscribe