[23568] in Perl-Users-Digest
Perl-Users Digest, Issue: 5775 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 10 14:08:10 2003
Date: Mon, 10 Nov 2003 11:05:54 -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 Mon, 10 Nov 2003 Volume: 10 Number: 5775
Today's topics:
ANNOUNCE: HTML::KTemplate v1.33 <usenet@repsak.de>
Re: Checking environment variable existence <jwillmore@remove.adelphia.net>
command line text processing.. (Jim Carter)
Re: command line text processing.. <kha@rogers.com>
Re: count of 1s in a binary number <bik.mido@tiscalinet.it>
Re: count of 1s in a binary number (Anno Siegel)
Re: Getting fields into perl script (Tad McClellan)
Re: Help a non-Perl user update an older program... (Nathan Childress)
Re: Help a non-Perl user update an older program... (Nathan Childress)
Re: Interaction between two strings (Anno Siegel)
Re: Interaction between two strings <noreply@gunnar.cc>
Re: Interaction between two strings <tore@aursand.no>
mod_perl 1.99 apache 2.0.48 <lionel.valero@polymtl.ca>
Re: mod_perl 1.99 apache 2.0.48 <nobull@mail.com>
Re: newbie 4th JaPH <bik.mido@tiscalinet.it>
Re: newbie 4th JaPH <hexkid@hotpop.com>
Re: newbie 4th JaPH <hexkid@hotpop.com>
Re: Newbie question - create a file <raisin@delete-this-trash.mts.net>
Re: Perl features <ThomasKratz@REMOVEwebCAPS.de>
Re: Question about split <noreply@gunnar.cc>
Sort array of objects using a method (Marcus)
Re: Style question: map versus foreach <dmcbride@naboo.to.org.no.spam.for.me>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Sun, 9 Nov 2003 14:37:35 GMT
From: Kasper Dziurdz <usenet@repsak.de>
Subject: ANNOUNCE: HTML::KTemplate v1.33
Message-Id: <Ho541L.1ID0@zorch.sf-bay.org>
HTML::KTemplate - Perl module to process HTML templates.
A simple and fast Perl module to process HTML templates with a
customizable template syntax, support for multidimensional data
structures and many advanced options.
You can download HTML::KTemplate from CPAN:
http://search.cpan.org/dist/HTML-KTemplate/
For more information about HTML::KTemplate visit:
http://html-ktemplate.sourceforge.net/
Changes in 1.33
---------------
Rewrite: some internal changes making this module faster.
Changed: chomped newlines are replaced with a single space as you would
expect from a html template module.
Fixed: assigned undefined block variables were not used if a global
variable with the same name existed.
Changes in 1.32
---------------
New: mailing list available for announcements and discussion (see POD
documentation for more information).
Fixed: process() method emptied scalar reference passed as template.
Fixed: references to undefined values are skipped instead of raising an
error when passed to process() method.
Changes in 1.31
---------------
Fixed: print() method accepts file hande for compatibility reasons.
Fixed: passing a file handle reference to process() did not work with
older Perl versions.
------------------------------
Date: Mon, 10 Nov 2003 16:40:13 GMT
From: James Willmore <jwillmore@remove.adelphia.net>
Subject: Re: Checking environment variable existence
Message-Id: <20031110113856.14cb0e37.jwillmore@remove.adelphia.net>
On 10 Nov 2003 13:18:10 GMT
anno4000@lublin.zrz.tu-berlin.de (Anno Siegel) wrote:
> James Willmore <jwillmore@remove.adelphia.net> wrote in
> comp.lang.perl.misc:
>
> > And checking for unset environmental variables, I'm thinking you
> > were
> ^^^^^^^^^^^^^
>
> Ugh. The term is "environment variables". They're not green...
Well, now I've learned something new today.
--
Jim
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
a fortune quote ...
Different all twisty a of in maze are you, passages little.
------------------------------
Date: 10 Nov 2003 10:28:34 -0800
From: carterave@yahoo.com (Jim Carter)
Subject: command line text processing..
Message-Id: <9c2a26b6.0311101028.39e4df48@posting.google.com>
Hi all,
I am on Windows:
I have a customozed command "lsdesktops" (I mean when I run C:\>
lsdesktops at cmd prompt) and it gives an output like below:
--------------------------------------
TEST-MACHINE1:Windows SP3 Memory
TEST-MACHINE2:Windows SP1 Processor
TEST-MACHINE3:Windows SP4 Configuration
........ ###and many more lines like the above three lines
---------------------------------------
Now I need a commmand line perl code to strip off every thing that
starts after colon (inlcuding colon).
Ex: The output should be
-------------------
TEST-MACHINE1
TEST-MACHINE2
TEST-MACHINE3
TEST-MACHINE4
TEST-MACHINE5
TEST-MACHINE6
.....
.....
---------------------
I know I need to use -e option and reg expression, but is not giving
me the exact result.
Thanks,
JCarter
------------------------------
Date: Mon, 10 Nov 2003 19:05:24 GMT
From: Kien Ha <kha@rogers.com>
Subject: Re: command line text processing..
Message-Id: <UjRrb.11938$Rah1.8368@twister01.bloor.is.net.cable.rogers.com>
Jim Carter wrote:
> Hi all,
>
> I am on Windows:
>
> I have a customozed command "lsdesktops" (I mean when I run C:\>
> lsdesktops at cmd prompt) and it gives an output like below:
> --------------------------------------
> TEST-MACHINE1:Windows SP3 Memory
> TEST-MACHINE2:Windows SP1 Processor
> TEST-MACHINE3:Windows SP4 Configuration
> ........ ###and many more lines like the above three lines
> ---------------------------------------
>
> Now I need a commmand line perl code to strip off every thing that
> starts after colon (inlcuding colon).
> Ex: The output should be
>
> -------------------
> TEST-MACHINE1
> TEST-MACHINE2
> TEST-MACHINE3
> TEST-MACHINE4
> TEST-MACHINE5
> TEST-MACHINE6
> .....
> .....
> ---------------------
>
>
> I know I need to use -e option and reg expression, but is not giving
> me the exact result.
>
Show us your code so we know what it did right/wrong.
lsdesktops | perl -F: -lane "print $F[0]"
------------------------------
Date: Mon, 10 Nov 2003 15:45:28 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: count of 1s in a binary number
Message-Id: <rs8vqv0atat5f1mqiukigke7u3isub9sd2@4ax.com>
On 8 Nov 2003 17:26:21 GMT, anno4000@lublin.zrz.tu-berlin.de (Anno
Siegel) wrote:
>That counts the bits of an n-bit number in n steps. There is an old trick
>in the dying art of bit-fiddling that allows to count them in as many steps
>as there are one-bits. That is a significant advantage if the numbers
>involved are small (or sparse, bit-wise).
[...]
>While this certainly was lengthy to describe, in Perl terms the isolated
>lowest one-bit of "$n" is "(($n - 1) ^ $n) & $n". In particular, there
or equivalently "$n & ~($n-1)".
>This is merely an algorithmic annotation which has nothing to do with
>Perl. Originally it was, of course, done in an assembler or another.
Well, since I mentioned the "obvious" recursive version as an actual
example (but no more than that!), I think that your piece of code was
worth posting here anyway!
And thanks for the supplied information. I appreciated them very much,
for one.
>It's a cute little trick, and a pity there's no more use for it. There
>is nothing similar for the MSB, by the way.
This seems reasonable. But before saying something this definitive...
I'd check Prof. Knuth! BTW: are you absolutely sure there really isn't
anything similar for the MSB? Just for curiosity...
>To bring back some Perl content, the standard way of bit counting is
>probably through pack and unpack. Unpack does checksums, and the bit-count
>can be seen as a checksum of a string of one bit data. Since we are only
>interested in the bit count, we don't have to worry about endian-ness --
>any integer format can be used to pack the bit string. So,
>
> sub size3 {
> my $x = shift;
> unpack '%32b*', pack 'I', $x;
> }
Cool! Unfortunately I've not yet fully grasped the full power of
pack() and its friend unpack(). This is one of those times when
reading a NG reveals to be fruitful, I hope the OP will benefit from
your post too...
>is more compact, and, presumably, faster, than the others. No more
^^^^^^^^^^
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw/:all/;
my $max= ~0;
my $res=timethese (100000, {
'size_rec' => "size_rec(int rand $max)",
'size_tr' => "size_tr(int rand $max)",
'size_trick' => "size_trick(int rand $max)",
'size_pack' => "size_pack(int rand $max)",
});
cmpthese($res);
sub size_rec {
my $n=shift;
return 0 unless $n;
return ($n&1) + size_rec($n>>1);
}
sub size_tr {
qq.@{[map sprintf('%b',$_),@_]}. =~ tr/1//;
}
sub size_trick {
my $n = shift;
my $size = $n ? 1 : 0;
$size++ while $n ^= $n & ~($n-1);
$size;
}
sub size_pack {
my $x = shift;
unpack '%32b*', pack 'I', $x;
}
__END__
Benchmark: timing 100000 iterations of size_pack, size_rec, size_tr,
size_trick...
size_pack: 0 wallclock secs ( 0.44 usr + 0.00 sys = 0.44 CPU) @
227272.73/s (n=100000)
size_rec: 5 wallclock secs ( 4.18 usr + 0.00 sys = 4.18 CPU) @
23923.44/s (n=100000)
size_tr: 0 wallclock secs ( 0.65 usr + 0.00 sys = 0.65 CPU) @
153846.15/s (n=100000)
size_trick: 0 wallclock secs ( 1.32 usr + 0.00 sys = 1.32 CPU) @
75757.58/s (n=100000)
Rate size_rec size_trick size_tr size_pack
size_rec 23923/s -- -68% -84% -89%
size_trick 75758/s 217% -- -51% -67%
size_tr 153846/s 543% 103% -- -32%
size_pack 227273/s 850% 200% 48% --
[Win98]
Benchmark: timing 1000000 iterations of size_pack, size_rec,
size_tr, size_trick...
size_pack: 3 wallclock secs ( 2.38 usr + 0.00 sys = 2.38 CPU) @
420168.07/s (n=1000000)
size_rec: 37 wallclock secs (37.67 usr + 0.00 sys = 37.67 CPU) @
26546.32/s (n=1000000)
size_tr: 5 wallclock secs ( 5.00 usr + 0.00 sys = 5.00 CPU) @
200000.00/s (n=1000000)
size_trick: 9 wallclock secs ( 8.53 usr + 0.00 sys = 8.53 CPU) @
117233.29/s (n=1000000)
Rate size_rec size_trick size_tr size_pack
size_rec 26546/s -- -77% -87% -94%
size_trick 117233/s 342% -- -41% -72%
size_tr 200000/s 653% 71% -- -52%
size_pack 420168/s 1483% 258% 110% --
[Linux, 10x iterations]
Michele
--
# This prints: Just another Perl hacker,
seek DATA,15,0 and print q... <DATA>;
__END__
------------------------------
Date: 10 Nov 2003 15:25:24 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: count of 1s in a binary number
Message-Id: <booal4$pi3$1@mamenchi.zrz.TU-Berlin.DE>
Michele Dondi <bik.mido@tiscalinet.it> wrote in comp.lang.perl.misc:
> On 8 Nov 2003 17:26:21 GMT, anno4000@lublin.zrz.tu-berlin.de (Anno
> Siegel) wrote:
[...]
> >While this certainly was lengthy to describe, in Perl terms the isolated
> >lowest one-bit of "$n" is "(($n - 1) ^ $n) & $n". In particular, there
>
> or equivalently "$n & ~($n-1)".
Oh, right. I guess I got into the habit of avoiding negation when
working with bit vectors (strings). There it tends to create spurious
one-bits from the unused bits in the last byte. Since we have numbers,
that is no concern here.
> >It's a cute little trick, and a pity there's no more use for it. There
> >is nothing similar for the MSB, by the way.
>
> This seems reasonable. But before saying something this definitive...
> I'd check Prof. Knuth! BTW: are you absolutely sure there really isn't
> anything similar for the MSB? Just for curiosity...
Well, it was common wisdom that came with the trick. Since it relies
on carry propagation, and carry propagation works only one way, a
similar way to isolate the MSB can't be all that similar.
Anno
------------------------------
Date: Mon, 10 Nov 2003 08:08:03 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Getting fields into perl script
Message-Id: <slrnbqv6u3.bt0.tadmc@magna.augustmail.com>
Hugh Kang <skang@leaguedata.com> wrote:
> In Unix script, I use $1 and $2 to get the fields.
You access command line arguments in Perl via the @ARGV array.
print "$_\n" for @ARGV; # echo all command line arguments
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: 10 Nov 2003 06:42:43 -0800
From: sauroneru@hotmail.com (Nathan Childress)
Subject: Re: Help a non-Perl user update an older program...
Message-Id: <68e73375.0311100642.2f7b4eef@posting.google.com>
The above suggestion didn't help, but I did fiddle around with it
enough to get it to work. Here is the improved getdata.pl file (you
need Perl to run this, as I don't know how to make it into an EXE):
http://home.earthlink.net/~sauroneru/getdata.cfg
I'm not sure how long the link will stay active, but it should work at
least for the next few months.
------------------------------
Date: 10 Nov 2003 06:44:40 -0800
From: sauroneru@hotmail.com (Nathan Childress)
Subject: Re: Help a non-Perl user update an older program...
Message-Id: <68e73375.0311100644.6e1d9df6@posting.google.com>
Ack... sorry... here's the correct link:
http://home.earthlink.net/~sauroneru/getdata.pl
------------------------------
Date: 10 Nov 2003 14:20:42 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Interaction between two strings
Message-Id: <boo6rq$llb$1@mamenchi.zrz.TU-Berlin.DE>
gilgames <GlgAs@netscape.net> wrote in comp.lang.perl.misc:
> <<
> In a regex, I want to use some characters from a string that is
> supplied as a kind of parameter value. I will give an example to
> explain this.
> >>
>
>
>
> $pattern='abcd';
> $string='abcdaaayz';
>
> if ($string =~ /($pattern)/) {print "Match: ";}
> else {print "Nomatch: ";}
>
> print "$string =~ $pattern = $1\n";
>
> prints :
>
> Match: abcdaaayz =~ abcd = abcd
>
>
> The same with $pattern = 'xyz' prints:
>
> Nomatch: abcdaaayz =~ xyz =
I still don't get it. Yes, the code above would behave as you describe.
Now, what characters from what string do you want to use as a parameter
value in which way?
You really got to work on the way you present your problem.
Anno
------------------------------
Date: Mon, 10 Nov 2003 16:29:47 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Interaction between two strings
Message-Id: <boob0j$1girro$1@ID-184292.news.uni-berlin.de>
Anno Siegel wrote:
> gilgames wrote:
<snip>
> I still don't get it. Yes, the code above would behave as you
> describe. Now, what characters from what string do you want to use
> as a parameter value in which way?
>
> You really got to work on the way you present your problem.
"gilgames" seems not to be OP.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: Mon, 10 Nov 2003 16:56:10 +0100
From: Tore Aursand <tore@aursand.no>
Subject: Re: Interaction between two strings
Message-Id: <pan.2003.11.10.13.18.29.428176@aursand.no>
On Mon, 10 Nov 2003 04:52:35 -0800, Ewout wrote:
> Assume that the parameter value is ABCR. The regex should be such that
> for example a string like ABCC should give a match. But if the
> parameter value is XYZR, then the string XYZC should give a match. To
> summarize: the regex should take part of the pattern from another
> string that is supplied separately from the regex. Is there any way to
> achieve this?
Sounds to me like substr() would cover your needs pretty well;
if ( substr($s1,0,3) eq substr($s2,0,3) ) {
# Match
}
--
Tore Aursand <tore@aursand.no>
------------------------------
Date: Mon, 10 Nov 2003 16:41:23 GMT
From: Lionel Valero <lionel.valero@polymtl.ca>
Subject: mod_perl 1.99 apache 2.0.48
Message-Id: <TcPrb.27415$Ng3.24555@charlie.risq.qc.ca>
Hello,
I built a sever with apache and perl (cgi), and i used the mod_perl to boost my
server, it actually works 4x time faster with mod_perl.
I did not modify my perl scripts in order to use mod_perl, i left them as is.
But i noticed that the available free ram decreases each time a script is called .
How is it ?
Regards,
Lionel.
------------------------------
Date: 10 Nov 2003 17:44:27 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: mod_perl 1.99 apache 2.0.48
Message-Id: <u965hsp18k.fsf@wcl-l.bham.ac.uk>
Lionel Valero <lionel.valero@polymtl.ca> writes:
> I built a sever with apache and perl (cgi), and i used the mod_perl to
> boost my server, it actually works 4x time faster with mod_perl.
>
> I did not modify my perl scripts in order to use mod_perl, i left them as is.
>
> But i noticed that the available free ram decreases each time a script is called .
Off-topic note: "free ram" is generally a bad thing. For this reason
some people think it would be better entitled "wasted ram".
However if what you are really saying is that the memory usage of
Apache is growing read on...
Perl uses reference counting, this means that anything using circular
(unweakened) references leaks memory.
All sorts of things you do in Perl that should not leak memory do anyway.
People who write CGI scripts usually don't worry if they leak memory
since under CGI the process terminates immediately anyhow.
If your CGI scripts are leaking lots of memory then repair the memory
leaks.
mod_perl2 periodically kills off Perl interpreter threads to avoid
memory usage growing for ever. You can tune these settings.
HTH
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Mon, 10 Nov 2003 15:45:27 +0100
From: Michele Dondi <bik.mido@tiscalinet.it>
Subject: Re: newbie 4th JaPH
Message-Id: <uu5vqv8879irlejak1nn62e2r8sp499rcb@4ax.com>
On 7 Nov 2003 18:43:14 GMT, Pedro Graca <hexkid@hotpop.com> wrote:
>How do you like this newbie's 4th JaPH attempt?
>
>-- (not a .sig now)
>#!/usr/bin/perl -w 2003-11-07 Pedro Graca - JaPH4
>@1=(2399670,250604053953,1455299,5114679918);foreach$2(@1){$s='';
>while($2){$c=$2%49;$2=int($2/49);$s.=(chr(($c+64)^32))}push@p,$s}
>print join(' ',@p),"\n"
Personally I like it. Only, it is not too obfuscated and it doesn't
exhibit an intriguing idea either. (like Abigail's ones do, for
example!)
Also, while JAPHs are not generally required to be warnings or strict
safe, it is not so bad that they are. But IIRC I've never seen an
explicit shebang line unless it was really necessary to the logic of
the code.
Last, IMHO lines should be better equal-sized, if possible. If it
matters I think my first and only JAPH (.sig here) sort of sucks too.
Michele
--
$\=q.,.,$_=q.print' ,\g,,( w,a'c'e'h,,map{$_-=qif/g/;chr
}107..q[..117,q)[map+hex,split//,join' ,2B,, w$ECDF078D3'
F9'5F3014$,$,];];$\.=$/,s,q,32,g,s,g,112,g,y,' , q,,eval;
------------------------------
Date: 10 Nov 2003 17:00:34 GMT
From: Pedro Graca <hexkid@hotpop.com>
Subject: Re: newbie 4th JaPH
Message-Id: <boog7h$1h1bg5$1@ID-203069.news.uni-berlin.de>
Ben Morrow wrote:
>
> Pedro Graca <hexkid@hotpop.com> wrote:
>> I wanted to use @1, $2, $3 (for $s) and $4 (for $c), but perl (or
>> Perl?) complained.
>> I don't see any references to $2, $3, and $4 in perldoc perlvar except
>> for regexes which I'm not using.
>
> That is their only significance: they hold the contents of the
> bracketed sections of the last successful match.
>
> perlvar states they are read-only, which is why Perl complained about
> your use of $3 and $4. That it didn't complain about $2 is wrong, and
> is (I think) to do with the special way that globals are treated when
> used as the iterator of a for loop.
Thank you for a very good explanation.
> @1 is (currently) not special, but
> you should not use it as it is reserved for Perl, which means it may
> become so one day.
>
> (Note, for your (probable) immediate change, that $a and $b are
> special as well... :)
hehe, thanks again
--
@N=(2399670, 250604053953, 1455299, 5114679918);
foreach$x(@N){$s='';while($x){$c=$x%49;$x=int($x
/49);$s.=(chr(($c+64)^32))}push@p,$s}print"@p\n"
------------------------------
Date: 10 Nov 2003 17:28:06 GMT
From: Pedro Graca <hexkid@hotpop.com>
Subject: Re: newbie 4th JaPH
Message-Id: <boohr6$1gncn9$1@ID-203069.news.uni-berlin.de>
Michele Dondi wrote:
> Personally I like it. Only, it is not too obfuscated and it doesn't
> exhibit an intriguing idea either. (like Abigail's ones do, for
> example!)
Well ...
JaPHs really are for experts -- I'm trying to reach that level :-)
Many times I try to understand a JaPH I give up because I can't see
where things end
> Also, while JAPHs are not generally required to be warnings or strict
> safe, it is not so bad that they are. But IIRC I've never seen an
> explicit shebang line unless it was really necessary to the logic of
> the code.
>
> Last, IMHO lines should be better equal-sized, if possible. If it
> matters I think my first and only JAPH (.sig here) sort of sucks too.
Thanks for the pointers.
I'm trying to understand Your JaPH.
All those dots are really obfuscating me ... but I'm not giving up!
--
@ "=qw f Jt ss x_x am os hd q y_y Pd rk z_z Hab kd q f; $
_ =join "",(map ++ $ _, @ "); s/1/ /g and print; print $/
------------------------------
Date: Mon, 10 Nov 2003 08:27:59 -0600
From: Master Web Surfer <raisin@delete-this-trash.mts.net>
Subject: Re: Newbie question - create a file
Message-Id: <MPG.1a195c6c167028b59896ca@news.mts.net>
[This followup was posted to comp.lang.perl.misc]
In article <bo8d6102ih@enews1.newsguy.com>, bluecat22@go.com says...
> After toiling over "open" in the Perl docs and the Camel Book with no
> success, I am asking for help:
>
> How do I create a file named "dogs.txt" and write "My dog is a golden
> retriever." into it?
#!/usr/bin/perl -w
$filename = "dogs.txt";
open(GOLDEN,">$filename") or
die("Can't write \"$filename\" : $!\n");
print GOLDEN "My dog is a golden retriever.\n";
close GOLDEN;
exit 0;
------------------------------
Date: Mon, 10 Nov 2003 16:01:50 +0100
From: Thomas Kratz <ThomasKratz@REMOVEwebCAPS.de>
Subject: Re: Perl features
Message-Id: <3fafa913.0@juno.wiesbaden.netsurf.de>
Ben Morrow wrote:
> WRT Python in particular, the only things (IMHO) Perl has over python
> are
>
> 1. A sensible block structure not relying on indentation.
Yes, yes and yes again. Imagine a bunch of programmers working on the
same python source with different ideas of how wide a tab should be
and being unable to get their editor to use spaces instead of tabs.
Been there, seen that.
(Got to get that "tabs are evil" t-shirt :-)
Thomas
------------------------------
Date: Mon, 10 Nov 2003 16:40:22 +0100
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Question about split
Message-Id: <boobkl$1don5r$1@ID-184292.news.uni-berlin.de>
Anno Siegel wrote:
> Gunnar Hjalmarsson wrote:
>> Hugh Kang wrote:
>>>
>>> ($dummy, $word1, $word2, $memsize, $rest) = split; if ( $word1
>>> eq "Memory" ) {
>>
>> Since you are splitting without a pattern, it surprises me that
>> $word1, and not $dummy, is assigned the first word on respective
>> line.
>
> Well, he's reading the binary executablei. :)
Aha, that fact explains my observation as well. Thanks!
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 10 Nov 2003 10:43:19 -0800
From: bayrazone@web.de (Marcus)
Subject: Sort array of objects using a method
Message-Id: <e4e6bd06.0311101043.18052bcc@posting.google.com>
Hello, I'am a newbee in Perl and in Newsgroups. But I hope for some
help.
I have a array of Objekts and i want to sort these Objekts by using a
Objektmethod.
I tried this:
certainly the Objektstore ist filled with Objekts !!!
@unsortedObjektstore
@sortedObjekts = sort {$unsortedObjektstore->getNumber() <=>
$unsortedObjektstore->getNumber()} @unsortedObjektstore;
I thougt it works like an example in perldoc:
# sort numerically descending
@articles = sort {$b <=> $a} @files;
But it don't do so.
Can somebody help me?
------------------------------
Date: Mon, 10 Nov 2003 18:25:53 GMT
From: Darin McBride <dmcbride@naboo.to.org.no.spam.for.me>
Subject: Re: Style question: map versus foreach
Message-Id: <RKQrb.361373$6C4.143831@pd7tw1no>
Abigail wrote:
> Darin McBride (dmcbride@naboo.to.org.no.spam.for.me) wrote on MMMDCCXXIII
> September MCMXCIII in <URL:news:FRCrb.356754$pl3.261426@pd7tw3no>:
> __ Abigail wrote:
> __
> __ No, you're right. I suppose you overlooked the "that's the concept"
> __ part of that paragraph. Context rules in perl. But that doesn't mean
> __ that context rules in the maintainer's head when trying to decipher
> __ what was written.
>
> If you're going to assume the maintainer doesn't know basic concepts
> of Perl, all bets are off. Then one might want to avoid hashes and
> regexes too.
True to a point. However, there is no cleaner way to do hashes or
regexes. There is a clearer way to do map in void context: foreach.
And that's the crux, too. If there was no foreach, then it would be
obvious to all comers that map in void context is used for a particular
purpose.
However, the general rule is to use map for return values and foreach
when you don't need the return value. And that's what most people are
likely to think. Try reading perldoc perlstyle a bit... a few tidbits:
o Just because you CAN do something a particular way
doesn't mean that you SHOULD do it that way. Perl is
designed to give you several ways to do anything, so
consider picking the most readable one.
The main point of map is the return value. The main point of foreach
is the iteration over an array (no return value implied).
o Avoid using grep() (or map()) or `backticks` in a void
context, that is, when you just throw away their
return values. Those functions all have return val-
ues, so use them. Otherwise use a foreach() loop or
the system() function instead.
I think this is the whole debate. Unfortunately, there's no real
explicit justification here.
> __ That it being fixed is "lucky" is somewhat debatable. That doesn't
> __ make map in void context any more clear than before - only not as bad
> __ for runtime speed vs before the "fix".
> __
> __ Note that the fix is not necessarily goodness. It may have a speed
> __ penalty on array-context map's if it means that all of my 80 map's
> need
> __ to check their context (which is never void) just so that one of your
> __ map's (in void context) can be zillions faster. That's an overall
> __ negative to perl's speed since I can't speed up my map's any more, but
> __ you could have simply converted to foreach to get an even better (even
> __ if only slightly better) speed increase.
>
> Of course, map already had to check context. It wasn't that map left
> its intermediar list on the stack if it was called in void or scalar
> context.
It's just an extra check. I'm sure it was something like (simplified
for CODE case only):
sub map {
my $code = shift;
my @r;
foreach (@_) {
my @a = $code->();
if (wantarray) {
push @r, @a;
}
else {
$r[0] += scalar @a;
}
}
wantarray ? @r : $r[0];
}
Now it's more like:
sub map {
my $code = shift;
my @r;
foreach (@_) {
my @a = $code->();
if (defined wantarray) {
if (wantarray) {
push @r, @a;
}
else {
$r[0] += scalar @a;
}
}
}
wantarray ? @r : $r[0];
}
That's just an extra level of checks that isn't needed. Of course,
there are other ways to write this - some simpler, others faster. And,
of course, it's probably all in native (C) code anyway. Probably using
a case statement - which has its own overhead.
> __ I suppose you also missed pretty much any of my post that you didn't
> __ quote. My point, as opposed to others, is that even without the speed
> __ penalty, there is a stylistic benefit to using foreach when you may
> __ otherwise wish to use map in void context.
> __
> __ It's simply more *obvious* to use map for array contexts and foreach
> in
> __ void contexts. And that, regardless of any speed difference, is
> enough
> __ reason to discourage map in void context.
>
> "Obvious" is something subjective. *YOU* might find one way of doing
> something more obvious than doing it another way. But that doesn't
> mean it's true for everyone. Could you please explain why you find
> map in void context so non-obvious? Do you think map in general is
> non-obvious, or do you only get confused when the context is void?
> What about other functions in void context? Are they non-obvious too?
> Or is it only map that's non-obvious?
Given that map and foreach do almost exactly the same thing with only
the desired contexts being different, it makes sense to me that one
would use the version one desires based on that context difference.
Thus, when I see map in void context, the first thought in my mind is
"they didn't get the return from map!". Then, after scrutinising the
map's block (as it's usually a block not an expression), I may figure
out that they intended to have void (e.g., only doing it for the side
effect). And then, if that's not the source of the problem, I can go
on.
However, if they had used foreach to begin with, I might have been able
to skip over the code altogether knowing that "ignoring the return" is
exactly what was intended.
> I've had this discussion before, and people sometimes claim they find
> map in void context hard to understand but when I point out to them they
> happily use other function in void context without a problem, and ask
> them why they special case 'map', they don't know.
I do know. It's that there is another, clearer way to do it. There is
no other function other than 'print' to print to a filehandle. So use
it.
> Is there anyone who can explain why map in void context is confusing,
> but other functions aren't?
Simply have your code say what you mean rather than trick the virtual
machine into doing what you want by side effect. In general, side
effects make code harder to understand.
------------------------------
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.
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 5775
***************************************