Tanspose rows to columns

2 views
Skip to first unread message

jimbo...@my-deja.com

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
Hi,

I have been trying to convert this:

4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|

into this:

<PRO>
1.1
1.2
1.3
</PRO>
<DATA>
4 6 0
3 2 0
7 3 0
0 4 0
2 6 0
0 3 0
2 6 0
0 8 0
2 6 0
1 1 0
1 2 0
0 5 0
2 3 0
3 7 5
0 2 3
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
</DATA>


Thanks,

Jim

Sent via Deja.com http://www.deja.com/
Before you buy.

Vincent Murphy

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
>>>>> "jim" == jimbob4334 <jimbo...@my-deja.com> writes:

jim> Hi,
jim> I have been trying to convert this:

jim> 4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
jim> 6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
jim> 0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|

jim> into this:

jim> <PRO>
jim> 1.1
jim> 1.2
jim> 1.3
jim> </PRO>
jim> <DATA>
jim> 4 6 0
jim> 3 2 0
jim> 7 3 0
jim> 0 4 0
jim> 2 6 0
jim> 0 3 0
jim> 2 6 0
jim> 0 8 0
jim> 2 6 0
jim> 1 1 0
jim> 1 2 0
jim> 0 5 0
jim> 2 3 0
jim> 3 7 5
jim> 0 2 3
jim> 0 0 0
jim> 0 0 0
jim> 0 0 0
jim> 0 0 0
jim> 0 0 0
jim> 0 0 0
jim> 0 0 0
jim> 0 0 0
jim> 0 0 0
jim> </DATA>

There may be slick way of doing this with a matrices module. Look at
the _Perl Algorithms_ book for more information.

Here is something that may help you:

#! /usr/local/bin/perl -w

use strict;
my @rows = ();
while( <DATA> ) {
s/\|$//; # strip off last pipe.
chomp;
push(@rows,[ split '\|' ]);
}

print "<PRO>\n", map "$_->[-1]\n" => @rows;
print '</PRO>', "\n";

# I assume, maybe incorrectly, all rows have equal number of columns.
my $row_length = @{ $rows[0] };
print "<DATA>\n";
for ( my $i = 0; $i < @{ $rows[0] } - 1; $i++ ) {
foreach my $row ( @rows ) {
print $row->[$i], "\t";
}
print "\n";
}
print "</DATA>\n";

__DATA__


4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|


--Vinny

Brad Baxter

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
On Thu, 4 May 2000 jimbo...@my-deja.com wrote:
> Hi,

>
> I have been trying to convert this:
>
> 4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
> 6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
> 0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|
>
> into this:
>
> <PRO>
> 1.1
> 1.2
> 1.3
> </PRO>
> <DATA>
> 4 6 0
> 3 2 0
> 7 3 0
> 0 4 0
> 2 6 0
> 0 3 0
> 2 6 0
> 0 8 0
> 2 6 0
> 1 1 0
> 1 2 0
> 0 5 0
> 2 3 0
> 3 7 5
> 0 2 3

> 0 0 0
> 0 0 0
> 0 0 0
> 0 0 0
> 0 0 0

> 0 0 0
> 0 0 0
> 0 0 0
> 0 0 0
> </DATA>

And it looks like you succeeded. Since you're probably asking how one
might do this with perl, what have you tried so far?

--
Brad


jimbo...@my-deja.com

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
I was able to get it working using this:

#Generation of a LIST OF LISTS
# reading from file
@LoL = ();
while ( <DATA_FILE> )
{
push @LoL, [ split(/\|/) ];
}
print FILE_OUT "\n<PROJECT_DEFS>\n";
print "Input file: $file \n";
for $i ( 0 .. $#LoL )
{
for $j ( 0 .. $#{$LoL[$i]} )
{

if ($LoL[$i][$j] =~ /\d+\.\d+/)
{
print FILE_OUT "$LoL[$i][$j]\n";
}
else
{
if ($LoL[$i][$j] == "")
{
$LoL[$i][$j] = 0;
}
print TEMP_FILE "$LoL[$i][$j]\t";
}
last if ($LoL[$i][$j] =~ /\d+\.\d+/);
}
print TEMP_FILE "\n";
}
print FILE_OUT "TOTAL";
print FILE_OUT "</PROJECT_DEFS>\n";
print FILE_OUT "<DATA_TABLE>\n";
close (TEMP_FILE);
open (DATA, "$temp") || die "$file_out cannot be opened for
reading\n";
@d=();
@input=();
while (<DATA>)
{
for (0 .. scalar(@d = split) - 1) #scalar(@d)
gets the number of elements in array
{ $input[$_][$. - 1] = $d[$_] }; #$. is the
current input line of the last filehandle read
}
print FILE_OUT map { "@$_\n" } @input; #map applies the
expression to the array
print FILE_OUT "</DATA_TABLE>\n";
close (DATA); #closes file and resets $. to zero
close (FILE_OUT);
#system ("cp -p $outputfile $outputfile2");

In article <xjgr9bi...@gamora.us.cybertrust.com>,

> 4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
> 6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
> 0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|
>

> --Vinny

Tad McClellan

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
On Thu, 04 May 2000 14:39:38 GMT, jimbo...@my-deja.com <jimbo...@my-deja.com> wrote:

>I have been trying to convert this:

^^^^^^^^^^^

Where is your code?

If you post it, maybe we could help you fix it.

[ snip data, repeated in code below ]

>into this:
>
><PRO>
>1.1
>1.2
>1.3
></PRO>
><DATA>

>4 6 0
>3 2 0
>7 3 0


--------------------------------------
#!/usr/bin/perl -w
use strict;

my @data;

print "<PRO>\n";
while (<DATA>) {
chomp;
my @f = split /\|/;
print pop @f, "\n"; # last field

foreach my $i ( 0..$#f ) { # accumulate other fields
push @{$data[$i]}, $f[$i];
}
}
print "</PRO>\n<DATA>\n";

foreach my $ra_ref ( @data ) {
printf "%-8d", $_ foreach @$ra_ref;


print "\n";
}
print "</DATA>\n";


__DATA__
4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|

--------------------------------------


--
Tad McClellan SGML Consulting
ta...@metronet.com Perl programming
Fort Worth, Texas

Craig Berry

unread,
May 4, 2000, 3:00:00 AM5/4/00
to
jimbo...@my-deja.com wrote:
: I have been trying to convert this:
:
: 4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|

: 6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
: 0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|
:
: into this:

:
: <PRO>
: 1.1
: 1.2
: 1.3
: </PRO>
: <DATA>
: 4 6 0
: 3 2 0
[snip]
: 0 0 0
: 0 0 0
: </DATA>

#!/usr/bin/perl -w
# transpose - demo of data matrix tranposing for clpm
# Craig Berry (20000504)

use strict;

my @rows;

while (<DATA>) {
my @cols = split /\|/;
$#cols--; # Ditch empty field following final pipe.
push @rows, [ @cols ];
}

print "<PRO>\n",
map({ pop(@$_) . "\n" } @rows),
"</PRO>\n<DATA>\n";

print join("\t", map { shift @$_ } @rows), "\n" while @{$rows[0]};

print "</DATA>\n";

__DATA__
4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|


--
| Craig Berry - cbe...@cinenet.net
--*-- http://www.cinenet.net/users/cberry/home.html
| "The road of Excess leads to the Palace
of Wisdom" - William Blake

Bart Lateur

unread,
May 7, 2000, 3:00:00 AM5/7/00
to
jimbo...@my-deja.com wrote:

>I have been trying to convert this:
>
>4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
>6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
>0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|
>
>into this:
>
><PRO>
>1.1
>1.2
>1.3
></PRO>
><DATA>
>4 6 0
>3 2 0

>7 3 0
...
></DATA>

my($columns, @row, @header) = 0;
while(<DATA>) {
chomp;
my @data = split /\|/;
push @row, \@data;
push @header, pop @data;
$columns = @data if @data > $columns;
}

local ($\,$,) = ("\n","\n");
print '<PRO>', @header, '</PRO>', '<DATA>';
$, = "\t";
for my $i (0 .. $columns-1) {
print map { $_->[$i] } @row;
}
print '</DATA>'


__DATA__
4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|


But where is the challenge in that!

So I've been thinking about a single function that indeed transposes an
array of arrays.

So, assuming that @row and $columns are filled as with the above
function:

@transposed = map { my $i = $_; [ map { $_->[$i] } @row ] }
0 .. $columns-1;

{
#check:
local($", $\) = "\t";
print "@$_\n" foreach @transposed;
}

--
Bart.

Abigail

unread,
May 10, 2000, 3:00:00 AM5/10/00
to
On Thu, 04 May 2000 14:39:38 GMT,
jimbo...@my-deja.com <jimbo...@my-deja.com> wrote:
++ Hi,
++
++ I have been trying to convert this:
++
++ 4|3|7|0|2|0|2|0|2|1|1|0|2|3|0|0|0|0|0|0|0|0|0|0|1.1|
++ 6|2|3|4|6|3|6|8|6|1|2|5|3|7|2|0|0|0|0|0|0|0|0|0|1.2|
++ 0|0|0|0|0|0|0|0|0|0|0|0|0|5|3|0|0|0|0|0|0|0|0|0|1.3|
++


#!/opt/perl/bin/perl -w

use strict;

my @data = map {chomp; [split /\|/]} <DATA>;
{local $, = "\n"; print "<PRO>", (map {pop @$_} @data), "</PRO>$,";}
{local ($,, $\) = ("\t", "\n"); print "<DATA>"; map {my $i = $_;
print map {$_ -> [$i]} @data} 0 .. $#{$data [0]}; print "</DATA>";}

Xah

unread,
May 15, 2000, 3:00:00 AM5/15/00
to

bart....@skynet.be (Bart Lateur) wrote:
> But where is the challenge in that!
>
> So I've been thinking about a single function that indeed transposes an
> array of arrays.
>
> So, assuming that @row and $columns are filled as with the above
> function:
>
> @transposed = map { my $i = $_; [ map { $_->[$i] } @row ] }
> 0 .. $columns-1;
>
> {
> #check:
> local($", $\) = "\t";
> print "@$_\n" foreach @transposed;
> }

and that's so impressive for a Perl programer.
Now witness a functional programer:

--
Transpose

Transpose(tree) returns a result that is the given tree with
the first two levels transposed. e.g. Transpose(
[[1,2,3],['a','b','c']] ) returns [[1,'a'],[2,'b'],[3,'c']].

Transpose(tree, permutationList) transposes the tree
according to permutationList of the form [n1,n2,...,nm],
where each n_i is a unique positive integer from 1 to m.
Transpose($tree) is equivalent to Transpose($tree, [2,1] ).

Transpose essentially restructures a tree into a different
shape. Here are some explanations, examples follows at the
end. First, we'll use a simple example to illustrate.
Suppose we have $tree = [[1,2,3],['a','b','c']] and
$permutationList = [2,1]. The elements at level two of the
tree have these position indexes:

element position index
1 [0,0]
2 [0,1]
3 [0,2]
'a' [1,0]
'b' [1,1]
'c' [1,2]

Since the length of $permutationList is two, thus Transpose
will reshape the tree at level two. For each node, Transpose
will apply the given permutation to its position index.
Here's the result:

element position index
1 [0,0]
2 [1,0]
3 [2,0]
'a' [0,1]
'b' [1,1]
'c' [2,1]

Transpose then construct a tree by this new element/index
pair. The result is [[1,'a'],[2,'b'],[3,'c']]. Because
transposition with permutation [2,1] is common (matrix
computations), thus it is the default behavior for Transpose
with just one argument.

The given tree needs to be a rectangular array only up to
the level m, where m is the length of the specified
permutation. For example, suppose we have $tree =
[[1,2,3],[$anotherTree,'b','c']] and $permutationList =
[2,1]. It all works just as before, except that the element
'a' is substituted by $anotherTree. For example,
Transpose([[ 1,2,3],[ ['m','n'] ,'b','c']], [2,1]) returns
[[1,['m','n']],[2,'b'],[3,'c']].

Longer permutations also works similarly. For example,
suppose

$tree = [ [['x1','x2'],['y1','y2'],['z1','z2']], [
['a1','a2'] ,['b1','b2'], ['c1','c2']]]; $permutationList =
[3,1,2];

Since the length of $permutationList is 3, thus looking at
level 3 of the tree we have

element position index
before / after permute by [3,1,2]
'x1' [0,0,0] [0,0,0]
'x2' [0,0,1] [1,0,0]
'y1' [0,1,0] [0,0,1]
'y2' [0,1,1] [1,0,1]
'z1' [0,2,0] [0,0,2]
'z2' [0,2,1] [1,0,2]
'a1' [1,0,0] [0,1,0]
'a2' [1,0,1] [1,1,0]
'b1' [1,1,0] [0,1,1]
'b2' [1,1,1] [1,1,1]
'c1' [1,2,0] [0,1,2]
'c2' [1,2,1] [1,1,2]

Therefore, the result is
[[['x1','y1','z1'],['a1','b1','c1']],[['x2','y2','z2'],['a2'
,'b2','c2']]].

There are some implied restrictions on the input to
Transpose: 1. The second argument must be a permutation of a
range of m numbers that starts with one. 2. The tree must be
a rectangular array up to level m. That is,
Length(Dimensions($tree)) >= m.

A property of Transpose: If $tree is a rectangular array,
then the following is always true.
Permute(Dimensions($tree), $permutationList) ==
Dimensions(Transpose($tree, $permutationList))

If $tree is a rectangular array up to level m only, then the
left and right hand side agrees up to the first m numbers.

Example:

my $tree =
[
[ ['x1','x2'], ['y1','y2'], ['z1','z2']],
[ ['a1','a2'], ['b1','b2'], ['c1','c2']]
];

Transpose( $tree, [1, 2, 3]);
# returns
[[['x1','x2'],['y1','y2'],['z1','z2']],[['a1','a2'],['b1','b2'],['c1','c2']]
]

Transpose( $tree, [1, 3, 2]);
# returns
[[['x1','y1','z1'],['x2','y2','z2']],[['a1','b1','c1'],['a2','b2','c2']]]

Transpose( $tree, [2, 1, 3]);
# returns
[[['x1','x2'],['a1','a2']],[['y1','y2'],['b1','b2']],[['z1','z2'],['c1','c2'
]]]

Transpose( $tree, [2, 3, 1]);
# returns
[[['x1','a1'],['x2','a2']],[['y1','b1'],['y2','b2']],[['z1','c1'],['z2','c2'
]]]

Transpose( $tree, [3, 1, 2]);
# returns
[[['x1','y1','z1'],['a1','b1','c1']],[['x2','y2','z2'],['a2','b2','c2']]]

Transpose( $tree, [3, 2, 1]);
# returns
[[['x1','a1'],['y1','b1'],['z1','c1']],[['x2','a2'],['y2','b2'],['z2','c2']]
]

Transpose( $tree, [2, 1]);
# returns
[[['x1','x2'],['a1','a2']],[['y1','y2'],['b1','b2']],[['z1','z2'],['c1','c2'
]]]

--------------------

code downloadable at

http://xahlee.org/PerlMathematica_dir/perlMathematica.html

In functional languages, such generalized function as above are a dime a
dozen, and people don't wet their pants over it.

Xah
x...@xahlee.org
http://xahlee.org/PageTwo_dir/more.html
"The three principle virtues of Perl programers: mundaneness, sloppiness,
and fatuousness."


Christian Treczoks

unread,
May 16, 2000, 3:00:00 AM5/16/00
to
Xah schrieb:

> and that's so impressive for a Perl programer.
> Now witness a functional programer:
Oh, yes.. Functional programming - I remember a guy at our
university who was hot on functional programming. He showed me how
easy it is to print a list of primes: just define a list on numbers
with the typical properties of primes: can be divivded only by
1 and the number itself. Nice little one-liner in Miranda.

And the he started the program on the biggest sparc we had back
in ye olden times, and, behold, it died after 5 minutes (out of
memory) and listed the first ~20 primes :-)

I never touched functional programming again...

Randal L. Schwartz

unread,
May 16, 2000, 3:00:00 AM5/16/00
to
>>>>> "Christian" == Christian Treczoks <c.tre...@ndh.net> writes:

Christian> And the he started the program on the biggest sparc we had back
Christian> in ye olden times, and, behold, it died after 5 minutes (out of
Christian> memory) and listed the first ~20 primes :-)

Christian> I never touched functional programming again...

The difference between theory and practice in theory is much less
than the difference between theory and practice in practice.

For me, give me a *practical* language. I may not always be up on the
latest cool parsing techniques (for that I bow to people like Damian
Conway for giving me Very Cool Tools) or always completely understand
how memory allocations for closures work, but I can finish the task
with Perl long before people that are still trying to get their Java
program to compile.

OK, so I'm not quite *that* good. But I hope you get the point. I
gave a talk at Yale University a few weeks back on "The P in Perl
stands for Practical", and made jabs at Java with James Gosling
sitting in the front row. :)

One student asked me "doesn't Perl's lack of strong typing make it
harder to get your programs debugged?" to which I replied a simple
"No". I must've shocked his poor little world apart... he was visibly
hurt! :) I then went on to explain that with Perl's very high-level
constructs, I can keep enough of the routine in my head that I'm not
putting bugs in in the first place, whereas with C or C++ or Java, I
seem to keep having to write more of the code myself with more chances
to insert errors as well. (Who uses a debugger? :)

print "Just another Perl hacker,"

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Michael Carman

unread,
May 16, 2000, 3:00:00 AM5/16/00
to
Xah wrote:
>
> and that's so impressive for a Perl programer.
> Now witness a functional programer:
>
[Snip of ~150 lines of module POD.]

Where's the Perl?

Oh, *there* it is... and blech! It builds a string for eval() every time
it's called. That ought to make it run nice & slow. Was that the only
way you could find to do it?

> In functional languages, such generalized function as above are a
> dime a dozen, and people don't wet their pants over it.

Who's getting all excited? No, Bart's function is not as general and
flexible. Who cares? For the purpose stated, it is cleaner (less code
required) and more efficient. You don't need a sledgehammer to drive in
a finishing nail.



> Xah
> x...@xahlee.org
> http://xahlee.org/PageTwo_dir/more.html
> "The three principle virtues of Perl programers: mundaneness,
> sloppiness, and fatuousness."

Please lead your .sig with '-- ' so that newsreaders can trim it. BTW,
Xah, if you despise Perl so much (a) why program in it? and (b) why post
here? Have a taste for masochism?

-mjc

Tom Briles

unread,
May 16, 2000, 3:00:00 AM5/16/00
to
Michael Carman wrote:

>
> Xah wrote:
> >
> > and that's so impressive for a Perl programer.
> > Now witness a functional programer:
> >
> [Snip of ~150 lines of module POD.]
>
> Where's the Perl?
>
> Oh, *there* it is... and blech! It builds a string for eval() every time
> it's called. That ought to make it run nice & slow.

I got a chuckle when I read his recognition of assistance from that
"donkey" Larry Rosler. :)

- Tom

Bart Lateur

unread,
May 16, 2000, 3:00:00 AM5/16/00
to
Xah wrote:

>> @transposed = map { my $i = $_; [ map { $_->[$i] } @row ] }
>> 0 .. $columns-1;

>and that's so impressive for a Perl programer.


>Now witness a functional programer:

Ok, let's be pedantic. Mine *is* in pure functional coding style.
Functional programming is about eliminating side effects, no global
variables, etc. and in short: transforming data using nothing but
functions.

Wanting a more generic function is a nice ideal, but just don't call it
"functional programming". It's a whole different kettle o' fish.

--
Bart.

Michael Carman

unread,
May 22, 2000, 3:00:00 AM5/22/00
to
Xah wrote:
>
> > It builds a string for eval() every time it's called. [...]

> > Was that the only way you could find to do it?
>
> Yes, and if you could show me it without eval [...]

If it's truly needed then that's fine -- it's just an efficiency hit.
Getting around it is an interesting challenge, and there's probably a a
way, though it might require some comprimises in the level of arbitrary
complexity you're trying to support. I don't mean not supporting some
functionality; I'm thinking more along the lines of the DFT vs. the FFT;
have more efficient handling of special/common cases, and save the big
gun of eval() for the really hairy stuff if there's no other way.

At any rate, I don't have the time to play with it at present. Perhaps
someone else will take you up on your offer.

> The perl folks of beady eyes and unix weenies of lose mouths,

I've chided you about this attitude before. Can't you make an argument
without going out of your way to insult people?

> have this peculiar propensity toward trifles. You see them everywhere
> getting extremely upset about newsgroup post formats, [...]

It's called nettiquette. Manners are important on Usenet, as they are in
life, though this seems to be lost on you. (See previous comment.)
Sharing the rules with the uninitiated is a Good Thing. Postings with
mangled line-wrapping, WaReZ style, or other oddities are hard to read
and distract from the content. Personal modes of expression are fine,
but this forum is intended to be a community, not just a bunch of
individuals. That's what the conventions are for.

> In subjects they take an interest in, they are oblivious to what is
> really important. They are excited by speedier implementation, but
> little interest in better algorithm complexity.

I don't see why you feel that increasing complexity is intrinsically a
good thing. Do you get in your car and drive if you're only going next
door?

> > BTW, Xah, if you despise Perl so much [...] why program in it?
>
> There are things in this world that we do not get to choose,

Yes, but this isn't one of them.

-mjc

Ilmari Karonen

unread,
May 22, 2000, 3:00:00 AM5/22/00
to
In article <B54DE01A.A158%x...@xahlee.org>, Xah wrote:
>> Oh, *there* it is... and blech! It builds a string for eval() every time
>> it's called. That ought to make it run nice & slow.
>
>>Was that the only way you could find to do it?
>
>Yes, and if you could show me it without eval i would sprawl and bark like a
>dog for your entertainment. I'll show that, how one of us, is utterly
>willing but not able. Honest. I'll give you Perl folks 1 week.

The whole module, or just the transpose part? I'm not going to wade
through all the 70kb of code to remove eval()s, but that single one
isn't hard to eliminate - in fact, I don't see why it should be there
in the first place, when an auxiliary function is so much easier:

# warning: untested code
sub _subnode ($@) {
my ($node, @indices) = @_;
$node = $node[shift @indices] while @indices;
return \$node;
}

sub _transposeFullArgs ($$) {
my $ref_tree = $_[0];
my $ref_perm = $_[1];

my $transposeLevel = scalar @$ref_perm;
my $ref_indexSet = [grep {scalar @$_ == $transposeLevel} @{ _completeIndexSet([map {$_ = $_->[0];} @{ _treeToIndexSet($ref_tree)}]) }];

my $ref_resultTree;
foreach my $ref_index (@$ref_indexSet) {
# warning: untested code
${_subnode($ref_resultTree, @{Permute($ref_index,$ref_perm)})} = ${_subnode($ref_tree, @$ref_index)};
}
return $ref_resultTree;
}

But that still seems like an inefficient way to do it, since it has to
descend the tree to get each leaf, just like your original. I think a
recursive approach would be more efficient for large trees.

--
Ilmari Karonen - http://www.sci.fi/~iltzu/
Please ignore Godzilla and its pseudonyms - do not feed the troll.


Xah

unread,
May 22, 2000, 3:00:00 AM5/22/00
to
I wrote:
>> Yes, and if you could show me it without eval i would sprawl and bark like a
>> dog for your entertainment. I'll show that, how one of us, is utterly
>> willing but not able. Honest. I'll give you Perl folks 1 week.

Ilmari Karonen <il...@sci.invalid> wrote:
> The whole module, or just the transpose part?

Just the Transpose function.

> # warning: untested code
>...

Do not equivocate. I'm not impressed by half-assed showoffs nor asked for
ideas of alternative approaches at this moment.

What i asked is simple: write the _stand along_ Transpose function as i have
spec'ed it out in my earlier message. (or on my website
http://xahlee.org/PerlMathematica_dir/perlMathematica.html
)

You may filch my algorithm/methodology, but not copy/rely from my
subroutines. If anyone can do it without using 'eval', then i'd be
_extremely_ impressed.

Unix weenies say: "where is the code?"

Here's a chance for you perlers of beady eyes, to put words where your mouth
is. You have 1 lush week to show off. Of course, if this challenging
challenge degenerates into a chat on possibilities or other inanities, you
show the world how beady Perler's eyes are. Braggarts & mongers come
forward. This is the time to defend your perl-perl-land!

Xah
x...@xahlee.org
http://xahlee.org/PageTwo_dir/more.html


Uri Guttman

unread,
May 23, 2000, 3:00:00 AM5/23/00
to
>>>>> "X" == Xah <x...@xahlee.org> writes:

X> is. You have 1 lush week to show off. Of course, if this
X> challenging challenge degenerates into a chat on possibilities or
X> other inanities, you show the world how beady Perler's eyes
X> are. Braggarts & mongers come forward. This is the time to defend
X> your perl-perl-land!

how about, i don't give a shit about your transpose. it is a purely
algorithmic problem solved in many languages. use apl already and go
away. if you hate perl so much, why bother posting here? i don't go
flaming in python?

now shoo!!

uri

--
Uri Guttman --------- u...@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page ----------- http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net ---------- http://www.northernlight.com

Andrew Johnson

unread,
May 23, 2000, 3:00:00 AM5/23/00
to
In article <B54F38DA.A2B0%x...@xahlee.org>,
Xah <x...@xahlee.org> wrote:
[snip]

>
> Here's a chance for you perlers of beady eyes, to put words where your mouth
> is. You have 1 lush week to show off. Of course, if this challenging
> challenge degenerates into a chat on possibilities or other inanities, you
> show the world how beady Perler's eyes are. Braggarts & mongers come
> forward. This is the time to defend your perl-perl-land!

Your challenge is a chimera. What kind of challenge is this? One
person asked if you must eval() to do your transpose() function, and
now suddenly your challenge to Perl community is that we must arrive
at an equivelant to your code without using eval()? And if no one
does then what -- this is somehow a shot against the Perl language?
or the Perl community? (or just those of us who might have beady
eyes?). I submit that you have already solved your problem using Perl
(however sucky you feel the language is) -- your attempt to get
others to improve your Perl algorithm under the guise of a challenge
against Perl or the Perl community is, in a word, pathetic.

andrew

--
Andrew L. Johnson http://members.home.net/andrew-johnson/epwp.html
It may be that your sole purpose in life is simply to
serve as a warning to others.

Elaine Ashton

unread,
May 23, 2000, 3:00:00 AM5/23/00
to
in article x74s7qd...@home.sysarch.com, Uri Guttman at u...@sysarch.com
quoth:

> now shoo!!

/me starts tapping her watch and whistling 'anchors aweigh'....now I just
have to find that submarine captain.

:)

e.


Ilmari Karonen

unread,
May 23, 2000, 3:00:00 AM5/23/00
to
In article <B54F38DA.A2B0%x...@xahlee.org>, Xah wrote:
>What i asked is simple: write the _stand along_ Transpose function as i have
>spec'ed it out in my earlier message. (or on my website
>http://xahlee.org/PerlMathematica_dir/perlMathematica.html
>
>You may filch my algorithm/methodology, but not copy/rely from my
>subroutines. If anyone can do it without using 'eval', then i'd be
>_extremely_ impressed.

Hmmph - it appears the target has moved. Normally I wouldn't have
bothered, but I'd already done what you're now asking for right after
posting my previous suggestion.

This is tested code, and you can test it yourself by pasting it into a
file and running it. Don't include my signature unless you wish to
transpose it too..


#!/usr/bin/perl -w
use strict;

sub _transpose {
my ($tree, $perm, $new, @pos) = @_;
if (@pos < @$perm) {
my $i = 0;
_transpose($_, $perm, $new, @pos, $i++) for @$tree;
} else {
$new = \$$new->[$_] for map $pos[$_ - 1] => @$perm;
$$new = $tree;
}
}

sub Transpose {
my ($tree, $perm) = @_;
$perm ||= [2,1];
_transpose($tree, $perm, \my $new);
return $new;
}

# test 3-level transpose:
my @matrix = map [map [split//] => split] => <DATA>;
print join(" " => map join("" => @$_) => @$_), "\n" for @matrix;
print "--\n";
print join(" " => map join("" => @$_) => @$_), "\n" for @{Transpose(\@matrix, [3,1,2])};

__DATA__
abc def ghi
jkl mno pqr
stu vwx yz.

Michael Carman

unread,
May 23, 2000, 3:00:00 AM5/23/00
to
Xah wrote:
>
> You may filch my algorithm/methodology, but not copy/rely from my
> subroutines. If anyone can do it without using 'eval', then i'd be
> _extremely_ impressed.

Why not? My question, and your challenge (as originally stated) simply
concerned the necessity of using eval(). As far as I'm concerned, Ilmari
has fulfilled that. Why not test his code to ensure that it has the same
functionality and run some benchmarks on it to see how the performance
compares?

At any rate it doesn't really matter, as Ilmari has already done what
you're now asking for, too. Will you be gracious enough to admit it?

-mjc

P.S. Lest anyone be misled: I am not concerned about proving that anyone
here is a better/worse programmer than anyone else. The collective
community here is far better than any individual. I just want to see
problems solved in the best possible manner.

Ilmari Karonen

unread,
May 24, 2000, 3:00:00 AM5/24/00
to
In article <392AC8E5...@home.com>, Michael Carman wrote:
>Why not? My question, and your challenge (as originally stated) simply
>concerned the necessity of using eval(). As far as I'm concerned, Ilmari
>has fulfilled that. Why not test his code to ensure that it has the same
>functionality and run some benchmarks on it to see how the performance
>compares?

Actually, I'm willing to assume that he may have done just that before
responding. The warnings about "untested code" were indeed needed,
since the _subnode function contained a silly reference bug. It's
rather obvious once you know it's there, but if he just pasted the
code in, tried it and observed that it didn't work, his response may
have been based on that.

Moral of the story: always test code before posting.


The WebDragon

unread,
May 26, 2000, 3:00:00 AM5/26/00
to
In article <9590804...@itz.pp.sci.fi>, Ilmari Karonen
<usene...@itz.pp.sci.fi> wrote:

| Hmmph - it appears the target has moved. Normally I wouldn't have
| bothered, but I'd already done what you're now asking for right after
| posting my previous suggestion.
|
| This is tested code, and you can test it yourself by pasting it into a

^^^^^^^^^^^^^^^^^^^

*cough* see below

# syntax error, near ") for "
File 'Untitled'; Line 8
# Missing $ on loop variable.
File 'Untitled'; Line 10

--
send mail to mactech (at) webdragon (dot) net instead of the above address.
this is to prevent spamming. e-mail reply-to's have been altered
to prevent scan software from extracting my address for the purpose
of spamming me, which I hate with a passion bordering on obsession.

Ilmari Karonen

unread,
May 26, 2000, 3:00:00 AM5/26/00
to
In article <8glce1$7gn$4...@216.155.32.218>, The WebDragon wrote:
># syntax error, near ") for "
>File 'Untitled'; Line 8
># Missing $ on loop variable.
>File 'Untitled'; Line 10

Upgrade your perl. It works on 5.005 just fine. Or just feed the
script through this one-liner:

perl -pi -e 's/(\S.*) for (.*);/for ($2) {$1}/'

Xah

unread,
May 27, 2000, 3:00:00 AM5/27/00
to
Dear Urine Gutterman,

you wrote


> how about, i don't give a shit about your transpose. it is a purely
> algorithmic problem solved in many languages. use apl already and go
> away. if you hate perl so much, why bother posting here? i don't go
> flaming in python?

Do you know why Perl newsgroups are filled with moronic kiddies and lame
questions, and the other half with faq pointers and pettiquette quarrels?
That is due, in large part, to bovine herd of your class, who have no sight
nor grace, but drawn by idiom trivia and inconsequential bitchings.

> it is a purely
>algorithmic problem solved in many languages. use apl already and go
>away.

"Solved" in many languages? Actually, the (generalized) transpose function
is not to be found in any (functional) language i'm aware of except
Mathematica, and i have not seen its concept (as spec'ed out in Mathematica
or my pod) discussed in any algorithm book except perhaps originated in
theory of Tensors. Please correct me if i'm wrong. It is difficult to find a
thing or two worth learning in your posts. Larry Wall's airy writings, are
at least a recognized art form called casuistry.

> if you hate perl so much, why bother posting here? i don't go
>flaming in python?

Original ideas are seeds of progress. Larry Wall has things to say against
the computing world, and I have things to say against him and his Perl. You
don't go flaming in python?? That's because, your idiomatic dogma drivel are
worthless everywhere else.

Xah
x...@xahlee.org
http://xahlee.org/PageTwo_dir/more.html


Xah

unread,
May 27, 2000, 3:00:00 AM5/27/00
to
Dear Ilmari Karonen <il...@sci.invalid> ,

you wrote:
> #!/usr/bin/perl -w
> use strict;
>
> sub _transpose {
> my ($tree, $perm, $new, @pos) = @_;
> if (@pos < @$perm) {
> my $i = 0;
> _transpose($_, $perm, $new, @pos, $i++) for @$tree;
> } else {
> $new = \$$new->[$_] for map $pos[$_ - 1] => @$perm;
> $$new = $tree;
> }
> }
>
> sub Transpose {
> my ($tree, $perm) = @_;
> $perm ||= [2,1];
> _transpose($tree, $perm, \my $new);
> return $new;
> }


Now that's respectful code. Terse, meet the spec, and without eval.

Some people in the Perl crowd, are non-trivial programers. I have to admit,
that i do come away very impressed at times in perl.misc. Do you like Perl?
and why do you like it? Do you have expertise in non-imperative languages?

--
           ___
       .-'`   `'-.
   _,.'.===   ===.'.,_
  / /  .___. .___.  \ \
 / /   ( o ) ( o )   \ \                                            _
: /|    '-'___'-'    |\ ;                                          (_)
| |`\_,.-'`   `"-.,_/'| |                                          /|
| |  \             /  | |                                         /\;
| |   \           /   | | _                              ___     /\/
| |    \   __    /\   | |' `\-.-.-.-.-.-.-.-.-.-.-.-.-./`   `"-,/\/ 
| |     \ (__)  /\ `-'| |    `\ \ \ \ \ \ \ \ \ \ \ \ \`\       \/
| |      \-...-/  `-,_| |      \`\ \ \ \ \ \ \ \ \ \ \ \ \       \
| |       '---'    /  | |       | | | | | | | | | | | | | |       |
| |               |   | |       | | | | | | | | | | | | | |       |
\_/               |   \_/       | | | | | | | | | | | | | | .--.  ;
                  |       .--.  | | | | | | | | | | | | | | |  | /
                   \      |  | / / / / / / / / / / / / / /  |  |/
               jgs |`-.___|  |/-'-'-'-'-'-'-'-'-'-'-'-'-'`--|  |
            ,.-----'~~;   |  |                  (_(_(______)|  |
           (_(_(_______)  |  |                        ,-----`~~~\
                    ,-----`~~~\                      (_(_(_______)
                   (_(_(_______)

Woouf, woouf, woOUF!
(Xah barking like a dog in admiration.)

(ascii art from
http://www.geocities.com/SoHo/7373/toystory.htm#slinkydog
)

Now i need to study the code... (will write to you personally afterwards...)

Xah
x...@xahlee.org
http://xahlee.org/PageTwo_dir/more.html


Xah

unread,
May 27, 2000, 3:00:00 AM5/27/00
to
Ilmari Karonen <il...@sci.invalid>,

would you care to post an explanation of your implementation?

I'm interested in:

* an overview of the algorithm/method.

* what does constructs like '\my $new' mean? Also, lines like


$new = \$$new->[$_] for map $pos[$_ - 1] => @$perm;

will take me some time to figure out. (haven't seen the use like
'\$$new->[$_] and 'for' in such way.)

thanks.

Xah
x...@xahlee.org
http://xahlee.org/PageTwo_dir/more.html


The WebDragon

unread,
May 27, 2000, 3:00:00 AM5/27/00
to
In article <9593337...@itz.pp.sci.fi>, Ilmari Karonen
<usene...@itz.pp.sci.fi> wrote:

| In article <8glce1$7gn$4...@216.155.32.218>, The WebDragon wrote:
| ># syntax error, near ") for "
| >File 'Untitled'; Line 8
| ># Missing $ on loop variable.
| >File 'Untitled'; Line 10
|
| Upgrade your perl. It works on 5.005 just fine. Or just feed the
| script through this one-liner:
|
| perl -pi -e 's/(\S.*) for (.*);/for ($2) {$1}/'

still waiting for the next release of perl to appear for my platform.

do add in a require 5.005 next time.

Ilmari Karonen

unread,
May 28, 2000, 3:00:00 AM5/28/00
to
In article <B55510F1.A685%x...@xahlee.org>, Xah wrote:
>Ilmari Karonen <il...@sci.invalid>,
>would you care to post an explanation of your implementation?
>
>* an overview of the algorithm/method.

It's really rather simple. It recursively descends the first N levels
of the tree, keeping track of the branch taken at each level. When it
gets to the specified depth, it permutates the collected list of
branches and descends the new tree according to the permutated list.


>* what does constructs like '\my $new' mean? Also, lines like

That declares a lexical scalar and returns a reference to it.


>$new = \$$new->[$_] for map $pos[$_ - 1] => @$perm;

The single-statement for(each) loops were introduced in perl 5.005.
For older perls, the above line could equivalently be written as:

for (map $pos[$_ - 1] => @$perm) { $new = \$$new->[$_] }

On the left of the "for" is a typical Perl idiom for walking a linked
list, with the twist that we choose a branch after each step. The
\$$new->[$_] evaluates as \(${${$new}}[$_]) - that is, it dereferences
$new twice to get an array, selects element number $_, and returns a
reference to it.

The right hand side of the "for" permutates the @pos array. I don't
think there's anything unusual there, except the stylistic use of the
"fat comma" after the first parameter to map(). That's just a habit.

Reply all
Reply to author
Forward
0 new messages