M
Michael Press
Hello. I am reading in a line of permutation cycles,
preparatory to multiplying them. Want to parse the line
into tokens for linear scanning: permutation elements
and parentheses. Also want to initialize a hash whose
key element pairs are permutation elements. The initial
state of the hash is to be the identity map. I thought
I could make this code more tight or efficient, but
failed; particularly the map <- grep pipe. Will someone
comment on this code? Thanks for listening.
________________CUT_______________
#! /usr/bin/perl -w
sub pm;
while (<DATA>) { pm $_ }
sub pm
{
my $z;
my @line;
my %p;
# Parse the cycles, and initialize the permutation map.
$z = shift (@_);
@line = $z =~ m/(\w+|[()])/g;
%p = map { $_ => $_ } grep { m/\w+/} @line;
printf ("%s\n", join ':', @line), "\n";
while (( $key, $value) = each (%p)) { printf "%7s%7s\n", $key, $value}
}
__END__
(99)(0)(3)(1 4 2 6)(5 10 7)(8 9 )(1 2 4 8 9 3 6 )(5 7)(0 2 )(1 5 99)(3 10)(4 7 8 )(6)(9)
________________CUT________________
preparatory to multiplying them. Want to parse the line
into tokens for linear scanning: permutation elements
and parentheses. Also want to initialize a hash whose
key element pairs are permutation elements. The initial
state of the hash is to be the identity map. I thought
I could make this code more tight or efficient, but
failed; particularly the map <- grep pipe. Will someone
comment on this code? Thanks for listening.
________________CUT_______________
#! /usr/bin/perl -w
sub pm;
while (<DATA>) { pm $_ }
sub pm
{
my $z;
my @line;
my %p;
# Parse the cycles, and initialize the permutation map.
$z = shift (@_);
@line = $z =~ m/(\w+|[()])/g;
%p = map { $_ => $_ } grep { m/\w+/} @line;
printf ("%s\n", join ':', @line), "\n";
while (( $key, $value) = each (%p)) { printf "%7s%7s\n", $key, $value}
}
__END__
(99)(0)(3)(1 4 2 6)(5 10 7)(8 9 )(1 2 4 8 9 3 6 )(5 7)(0 2 )(1 5 99)(3 10)(4 7 8 )(6)(9)
________________CUT________________