In article <rSjUf.238$%H.103@clgrps13>,
"John W. Krahn" <> wrote:
> Michael Press wrote:
> > 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 $_ }
>
> Put the code inside the while loop instead of calling a subroutine.
No. This is example code.
I use the subroutine like this:
my $alpha = "(99)(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22)";
my $beta = "(99)(0)(3 6 12 1 2 4 8 16 9 18 13)(15 7 14 5 10 20 17 11 22 21 19)";
my $gamma = "(99 0)(1 22)(2 11)(3 15)(4 17)(5 9)(6 19)(7 13)(8 20)(10 16)(12 21)(14 1

";
my $delta = "(99)(0)(3)(15)(1 18 4 2 6)(5 21 20 10 7)(8 16 13 9 12)(11 19 22 14 17)";
my $x;
print "(delta alpha^11)^5 has shape 1^6 3^6\n";
$x = ($delta . $alpha x 11) x 5;
pm $x;
print "\n";
print "beta = alpha^5 gamma alpha^5 gamma alpha^14 gamma alpha^18 \n";
$x = $alpha x 5 . $gamma . $alpha x 5 . $gamma . $alpha x 14 . $gamma . $alpha x 18;
pm $x;
$x = $beta;
pm $x;
print "\n";
....
> > sub pm
> > {
> > my $z;
> > my @line;
> > my %p;
>
> Define and declare the variables at the same time.
Yes.
> > # Parse the cycles, and initialize the permutation map.
> > $z = shift (@_);
> > @line = $z =~ m/(\w+|[()])/g;
>
> You don't need the capturing parentheses in the pattern.
Dang. Where is the specification for this? Examples in
Programming Perl use capturing parentheses.
> > %p = map { $_ => $_ } grep { m/\w+/} @line;
> >
> > printf ("%s\n", join ':', @line), "\n";
>
> Use print instead of printf. The ', "\n"' at the end is not doing anything so
> why is it there?
Cut and paste error when editing for submission to this forum.
It runs, but I did not see the problem.
> > 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)
>
> sub pm {
> my %p = map { /\w/ ? ( $_ => $_ ) : () } my @line = $_[0] =~ /\w+|[()]/g;
>
> print join( ':', @line ), "\n";
>
> while ( my ( $key, $value ) = each %p ) {
> printf "%7s%7s\n", $key, $value;
> }
> }
Thank you. Use of $_[0] noted; `each %p' noted.
I still do not use `()

' enough.
--
Michael Press