[25678] in Perl-Users-Digest
Perl-Users Digest, Issue: 7919 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Mar 29 13:17:25 2005
Date: Tue, 29 Mar 2005 10:17:19 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 29 Mar 2005 Volume: 10 Number: 7919
Today's topics:
Re: slicing syntax question <clarke@n_o_s_p_a_m_hyperformix.com>
Re: slicing syntax question <tadmc@augustmail.com>
Re: slicing syntax question xhoster@gmail.com
Re: slicing syntax question xhoster@gmail.com
Re: sorting matrixes <xah@xahlee.org>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 25 Mar 2005 08:31:46 -0600
From: "AC" <clarke@n_o_s_p_a_m_hyperformix.com>
Subject: Re: slicing syntax question
Message-Id: <424420b2$0$3713$39cecf19@news.twtelecom.net>
<robic0@yahoo.com> wrote in message
news:1111732795.625462.276760@f14g2000cwb.googlegroups.com...
> "I really want to use fewer lines in my code ......"
>
> Why is that... do you think fewer lines makes your code run more
> effeciently.
> You may think its terrible to make a mistake in front of syntax gods,
> but I
> assure you that content, clarity and logic ... in any language, is much
> more important.
>
Understood. I was actually thinking that there would be an idiom so that
clarity would be assumed.
Allan
------------------------------
Date: Fri, 25 Mar 2005 08:48:52 -0600
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: slicing syntax question
Message-Id: <slrnd4896k.ba1.tadmc@magna.augustmail.com>
robic0@yahoo.com <robic0@yahoo.com> wrote:
> Why is that... do you think fewer lines makes your code run more
> effeciently.
Do you think that questions end with a question mark?
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 25 Mar 2005 17:33:57 GMT
From: xhoster@gmail.com
Subject: Re: slicing syntax question
Message-Id: <20050325123357.611$sZ@newsreader.com>
"AC" <clarke@n_o_s_p_a_m_hyperformix.com> wrote:
> Why does this fail with a syntax error
>
> my @items = qw(a b c d e);
> my @hash{@items} = 0..$#items;
>
> but this does not?
>
> my @items = qw(a b c d e);
> @hash{@items} = 0..$#items;
Because one has a syntax error and the other does not. It would be nice
if perl allowed you to declare a hash while using it in a slice,
but sadly it does not.
>
> I really want to use fewer lines in my code and I'd hate to use one line
> to declare the hash and one line to initialize it.
newlines are optional almost everywhere in Perl.
my %hash; @hash{@items}= 0..$#items;
Cheers,
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 25 Mar 2005 17:47:38 GMT
From: xhoster@gmail.com
Subject: Re: slicing syntax question
Message-Id: <20050325124738.572$vA@newsreader.com>
"robic0@yahoo.com" <robic0@yahoo.com> wrote:
> "I really want to use fewer lines in my code ......"
>
> Why is that... do you think fewer lines makes your code run more
> effeciently.
No, but it can make maintaining the code more efficient. It I can see an
entire logical unit of the program on the screen without have to keep
scolling up and down, that makes things a lot easier. Wise use of white
space, and wise non-use of white-space, both contribute to good coding
style.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 27 Mar 2005 20:24:38 -0800
From: "Xah Lee" <xah@xahlee.org>
Subject: Re: sorting matrixes
Message-Id: <1111983878.706828.54230@o13g2000cwo.googlegroups.com>
Here's the solution to previous post.
-------------------------------
perl code:
sub sort_matrix($$) {
my $ref_matrix =3D $_[0];
my @indexMatrix =3D @{$_[1]};
my @indexes =3D map {$_->[0]} @indexMatrix;
my @operators =3D map {$_->[1] ? ' cmp ' : ' <=3D> '} @indexMatrix;
my @directions =3D map {$_->[2]} @indexMatrix;
my $body_code =3D '';
my @body_array;
for (my $i =3D 0; $i <=3D $#indexes; $i++) {
if ($directions[$i]) {
push(@body_array, "(\$a->[$i]" . $operators[$i] .
"\$b->[$i])");
} else {
push(@body_array, "(\$b->[$i]" . $operators[$i] .
"\$a->[$i])");
};
};
$body_code =3D join( ' or ', @body_array);
my $array_code =3D '(map { [' . join(q(, ), map {"\$_->[$_]"}
@indexes) . ', $_]} @$ref_matrix)';
my $code =3D "map {\$_->[-1]} (sort { $body_code} $array_code)";
my @result =3D eval $code;
return [@result];
};
------------------------------------------
Python code
# python v 2.4
def sort_matrix(matrix, directives):
result=3Dmatrix
for dir in directives:
if dir[1]:
if dir[2]:
result.sort(lambda x,y: cmp( str(x[dir[0]]),
str(y[dir[0]])) )
else:
result.sort(lambda x,y: cmp( str(x[dir[0]]),
str(y[dir[0]])), None, True)
else:
if dir[2]:
result.sort(lambda x,y: cmp(float(x[dir[0]]),
float(y[dir[0]])) )
else:
result.sort(lambda x,y: cmp(float(x[dir[0]]),
float(y[dir[0]])), None, True )
return result
m =3D [
[3, 99, 'a'],
[2, 77, 'a'],
[1, 77, 'a']
]
print sort_matrix(m,[
[2,True,True],
[1,False,True]
])
The Python code has not been tested much.
http://xahlee.org/perl-python/sort_matrix.html
Xah
xah@xahlee.org
=E2=88=91 http://xahlee.org/PageTwo_dir/more.html
=E2=98=84
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 7919
***************************************