[24968] in Perl-Users-Digest
Perl-Users Digest, Issue: 7218 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Oct 7 09:11:52 2004
Date: Thu, 7 Oct 2004 06:10:15 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 7 Oct 2004 Volume: 10 Number: 7218
Today's topics:
Re: perl net::ldap problem using variable eg attr => [ <tripal@arcor.de>
perl regexp for iptables (Kaushal Bhandu)
Re: perl regexp for iptables (Anno Siegel)
Re: Regex gurus question <http://joecosby.com/code/mail.pl>
Re: Regex gurus question <toreau@gmail.com>
Re: Regex gurus question <shawn.corey@sympatico.ca>
second question today. eak. use array, hash or somthing (Dan Vesma)
Top 10 list algorithm <fatted@gmail.com>
Re: Top 10 list algorithm <peter@semantico.com>
Re: Top 10 list algorithm <thepoet_nospam@arcor.de>
Re: Top 10 list algorithm <fatted@gmail.com>
Re: Top 10 list algorithm <peter@semantico.com>
Re: Top 10 list algorithm <fatted@gmail.com>
Re: Top 10 list algorithm <fatted@gmail.com>
Re: Top 10 list algorithm <shawn.corey@sympatico.ca>
Re: Top 10 list algorithm (Anno Siegel)
Re: Version? Getopt::Long <jvromans@squirrel.nl>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 07 Oct 2004 08:36:43 +0200
From: Jadeja Tripal <tripal@arcor.de>
Subject: Re: perl net::ldap problem using variable eg attr => [ $v1,$v2..]
Message-Id: <4164e404$0$25919$9b4e6d93@newsread2.arcor-online.net>
Many thanks to Paul, Richard, Keith,
It works now :-). Error was exactly what you pointed out, thanks for
making basic fundamental clear to me.
tripal
------------------------------
Date: 7 Oct 2004 04:59:36 -0700
From: kaushalgoa@yahoo.co.in (Kaushal Bhandu)
Subject: perl regexp for iptables
Message-Id: <b25b43f3.0410070359.65ff8283@posting.google.com>
I want to use the perl regular Expression for iptables script and
arrange the data in tables . I have no idea how to insert the
variables like -m , -A etc . which stand for Appending a new rule to
the script . Then I want to write a custom application ( GUI interface
) that can modify or add rules easily .
eg of a line in the script
-A OUTPUT -p tcp --dport 21 -j ACCEPT FTP
I compared my approach with the webmin mechanism , there seems to be
large difference the way it handles files ..
Can anybody help me with this ??
Kaushal
------------------------------
Date: 7 Oct 2004 12:24:24 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: perl regexp for iptables
Message-Id: <ck3cho$4gn$1@mamenchi.zrz.TU-Berlin.DE>
Kaushal Bhandu <kaushalgoa@yahoo.co.in> wrote in comp.lang.perl.misc:
> I want to use the perl regular Expression for iptables script and
> arrange the data in tables .
Which iptables script? Iptables itself appears to be a binary. Their
web site lists dozens of scripts that go with it.
> I have no idea how to insert the
> variables like -m , -A etc . which stand for Appending a new rule to
> the script .
"-m" and "-A" aren't variables, they are text strings. Where do you
want to insert them?
> Then I want to write a custom application ( GUI interface
> ) that can modify or add rules easily .
>
> eg of a line in the script
> -A OUTPUT -p tcp --dport 21 -j ACCEPT FTP
>
> I compared my approach with the webmin mechanism , there seems to be
> large difference the way it handles files ..
>
> Can anybody help me with this ??
Not before you describe in some more detail what you want to accomplish.
Is the "script" something your program will generate and modify? Or
will your program generate output that is going to be used to run a
given script?
Note that people here will have first-hand knowledge of iptables
only by coincidence. We can help with Perl programs, but not with
how to build an iptables script for a certain purpose.
Anno
------------------------------
Date: Wed, 06 Oct 2004 20:36:33 -0700
From: Joe Cosby <http://joecosby.com/code/mail.pl>
Subject: Re: Regex gurus question
Message-Id: <nce9m0l511844q80c15agkvlmsb9dnqhe6@4ax.com>
On Thu, 07 Oct 2004 01:25:41 +0200, Gunnar Hjalmarsson
<noreply@gunnar.cc> wrote:
>Joe Cosby wrote:
>> I have a string which will always contain a letter followed by
>> numbers, eg "x12345"
>>
>> I want to take the numbers and put them in another variable.
>>
>> So I do this:
>>
>> $test = "x12345";
>>
>> $_ = $test;
>> m/(\d+)/;
>> $justTheNumbers = $1;
>>
>> print "justTheNumbers is $justTheNumbers\n";
>>
>> and that works, $justTheNumbers is "12345", which is what I want.
>>
>> The three-line way I do it seems retarded to me though. Is there a
>> simpler way to do it?
>
>You can do it in one step:
>
> ($justTheNumbers) = $test =~ /(\d+)/;
>
>Or, if the only thing you need to do is cutting off the first character:
>
> $justTheNumbers = substr $test, 1;
Thanks, much appreciated.
--
Joe Cosby
http://joecosby.com/
"The ministry of communication is duty-bound to make the use of the
Internet impossible." - Taliban leader Mullah Mohammad Omar
------------------------------
Date: Thu, 07 Oct 2004 10:36:28 +0200
From: Tore Aursand <toreau@gmail.com>
Subject: Re: Regex gurus question
Message-Id: <pan.2004.10.07.08.36.27.387434@gmail.com>
On Wed, 06 Oct 2004 16:10:18 -0700, Joe Cosby wrote:
> I have a string which will always contain a letter followed by
> numbers, eg "x12345"
>
> I want to take the numbers and put them in another variable.
>
> So I do this:
>
> $test = "x12345";
>
> $_ = $test;
> m/(\d+)/;
> $justTheNumbers = $1;
>
> print "justTheNumbers is $justTheNumbers\n";
>
> and that works, $justTheNumbers is "12345", which is what I want.
>
> The three-line way I do it seems retarded to me though. Is there a
> simpler way to do it?
You could easily write what you have written in only one line;
( $justTheNumbers ) = $test =~ m/(\d+)/;
However, why use a regular expression at all? If you are _sure_ that the
string always will begin with only one character, you could use 'substr';
$justTheNumbers = substr( $test, 1 );
--
Tore Aursand <toreau@gmail.com>
"Software is like sex: It's better when it's free." (Linus Torvalds)
------------------------------
Date: Thu, 07 Oct 2004 07:49:55 -0400
From: Shawn Corey <shawn.corey@sympatico.ca>
Subject: Re: Regex gurus question
Message-Id: <z2a9d.31158$HO1.1197198@news20.bellglobal.com>
Eric Bohlman wrote:
> The philosophy of "defensive programming" suggests that you should write
>
> ($justTheNumbers) = $test =~ /^[[:alpha:]](/d+)$/ or die "unexpected format
> in \$test: [$test]";
>
> It may *look* like a lot of extra effort, but scores of programmers have
> found that the few extra minutes of coding that such techniques entail
> saved them many *hours* of time wasted tracking down subtle bugs.
>
I guess I'm a sloppy programmer. I would have written it as:
( my $just_digits = $test ) =~ s/\D//g;
I would put the test for correct format, or for any input validation
immediately after the input is received. Extra validation during
processing then becomes redundant. Having said that, validation for such
things as range will have to be done after this statement. So there is
an exception to every guideline.
--- Shawn
------------------------------
Date: 7 Oct 2004 05:31:18 -0700
From: daninbrum@hotmail.com (Dan Vesma)
Subject: second question today. eak. use array, hash or somthing else?
Message-Id: <14c7b35.0410070431.182e4ec0@posting.google.com>
Morning,
I am troubled.
As you will have seen from my last post I am building myself a
timesheets system in Perl. I am reading in tasks from a file, and
would like to filter then so they relate to a specific job (via
job-number), then sorting them by date done (a job may well have quite
a few tasks done on the same day).
I can't use a 2d array, because each task has many fields.
I can't use a hash of arrays with dates as keys are there will be
duplications.
Which brings me to 3d arrays... I can't for the life of me work out
how to sort them. I've looked at PerlFAQ and perdoc perllol, but
appear not to be able to find a solution. Is my brain just feeble?
This is obviously linchpin to the whole bloomin' thing. I've been
thinking for hours trying to work out code to sort a 3D array, but
can't do it. Arg! This must have been done before. I take it that you
can't sort a hash of arrays by one of the array values!?!
Please help me. Please. You're all better people than me.
Thanks in advance.
Daniel V
------------------------------
Date: Thu, 07 Oct 2004 13:19:08 +0200
From: Fatted <fatted@gmail.com>
Subject: Top 10 list algorithm
Message-Id: <2skn1fF1msrubU1@uni-berlin.de>
Is there a better (faster) way of implementing my top_sort function?
(Creating a top 10 list of the highest numbers from a large set).
Or did I get lucky? :)
--
#!/usr/bin/perl
use strict;
use warnings;
my @top10 = (0,0,0,0,0,0,0,0,0,0);
for(my $i = 0;$i < 10000; $i++)
{
my $num = rand();
@top10 = @{top_sort(\@top10, $num)};
}
# PRINT RESULTS
foreach (@top10)
{
print $_."\n";
}
sub top_sort
{
my $array = shift;
my $val = shift;
my @top;
# IF THE VALUE IS LESS THAN THE LAST VALUE IN THE TOP LIST,
# NO PROCESSING REQUIRED
if(defined($val) && $val >= 0)
{
if($val < $array->[$#$array])
{
return($array);
}
}
for(my $i = 0; $i < $#$array; ++$i)
{
if($val > $array->[$i])
{
# MAKE NEW TOP10 BY TAKING HIGHER MATCHES PLUS
THE NEW
# VALUE, PLUS THE LOWER MATCHES UP TO THE LIMIT
OF THE
# ARRAY.
@top = @$array[0..($i-1)];
push(@top, $val);
push(@top, @$array[$i..($#$array-1)]);
last;
}
}
# IN CASE OF PROBLEMS
if(!defined($top[0]))
{
@top = @$array;
}
return(\@top);
}
------------------------------
Date: Thu, 07 Oct 2004 12:44:50 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: Top 10 list algorithm
Message-Id: <41652bb6$0$21541$afc38c87@news.easynet.co.uk>
Well I timed yours and wrote mine we have a draw timewise, but I would prefer to
maintain my code rather than yours.
peter@anise:~$ time -p perl tt1.pl (yours)
0.999883882371392
0.999865909542621
0.999743501048854
0.999707466553314
0.99966005237998
0.999624140212287
0.999602350183547
0.999588096251578
0.999575394398029
0.999448703053851
real 0.06
user 0.06
sys 0.00
peter@anise:~$ time -p perl tt2.pl (mine)
0.999866027199676
0.999793617152395
0.999740853320571
0.999674339504168
0.999568519910227
0.999387627277898
0.999359789594308
0.999242443185917
0.999208602751779
0.998986709806623
real 0.06
user 0.06
sys 0.00
peter@anise:~$
#!/usr/bin/perl
use strict;
use warnings;
my %data;
for ( my $i = 0 ; $i < 10000 ; $i++ ) {
$data{rand()}++;
}
my @keys = sort { $b <=> $a } (keys %data);
foreach (@keys[0..9]) {
print "$_\n";
}
------------------------------
Date: Thu, 07 Oct 2004 14:02:43 +0200
From: Christian Winter <thepoet_nospam@arcor.de>
Subject: Re: Top 10 list algorithm
Message-Id: <41653063$0$12577$9b4e6d93@newsread4.arcor-online.net>
Fatted schrieb:
> Is there a better (faster) way of implementing my top_sort function?
> (Creating a top 10 list of the highest numbers from a large set).
> Or did I get lucky? :)
I'd get rid of all those shifts and if()s. I didn't time the
speed, but my solution would look like following:
------------------------------------------------------------
#!perl
use strict;
use warnings;
my @top10 = (0,0,0,0,0,0,0,0,0,0);
for (0..9999)
{
# Just sort random value into @top10 and get the
# 10 topmost values via hash slice notation.
@top10 = (sort { $b <=> $a } @top10, rand())[0..9];
}
foreach (@top10)
{
print $_."\n";
}
__END__
-------------------------------------------------------------
-Christian
------------------------------
Date: Thu, 07 Oct 2004 14:03:32 +0200
From: Fatted <fatted@gmail.com>
Subject: Re: Top 10 list algorithm
Message-Id: <2skpklF1m8fetU1@uni-berlin.de>
Peter Hickman wrote:
> Well I timed yours and wrote mine we have a draw timewise, but I would
> prefer to maintain my code rather than yours.
<snip>
Thanks for having a look! Although when I tried your code versus mine,
Mine appeared faster (Over 50000 iterations for each). I think you've
got a much faster machine than mine and might need to do a couple of
powers of 10 more iterations to make comparisons. (I agree your code is
much neater though) :
# YOURS
$ perl -d:DProf yours.pl
0.999979388766942
0.999954322899448
0.999919576561904
0.999919418581605
0.999911123899963
0.999901084906806
0.999897822368329
0.999895118668992
0.999870641207909
0.999817769402721
$ dprofpp
Total Elapsed Time = 1.27994 Seconds
User+System Time = 1.11994 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
0.89 0.010 0.010 2 0.0050 0.0050 main::BEGIN
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - warnings::BEGIN
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - warnings::import
# MINE
$ perl -d:DProf mine.pl
0.999987822921746
0.999961276796125
0.999960878033757
0.999940209732916
0.999902694088071
0.999897664019262
0.99988878249334
0.999879222743452
0.999863848759027
0.999815730523473
$ dprofpp
Total Elapsed Time = 0.829862 Seconds
User+System Time = 0.809862 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
52.4 0.425 0.425 50000 0.0000 0.0000 main::top_sort
1.23 0.010 0.010 1 0.0100 0.0100 warnings::BEGIN
1.23 0.010 0.020 2 0.0050 0.0100 main::BEGIN
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - warnings::import
------------------------------
Date: Thu, 07 Oct 2004 13:08:45 +0100
From: Peter Hickman <peter@semantico.com>
Subject: Re: Top 10 list algorithm
Message-Id: <41653151$0$21032$afc38c87@news.easynet.co.uk>
However as the numbers get bigger, from 10000 to 100000, yours is faster than
mine. So I wrote a version 3, having @top10 as a global variable rather than
passing it around helped alot.
peter@anise:~$ time -p perl tt1.pl
0.999996444354355
0.999980725756771
0.999961809075284
0.999949246149662
0.999939670484004
0.999931145711979
0.999920330943766
0.999915345843593
0.999905530188123
0.999901835815507
real 0.59
user 0.58
sys 0.00
peter@anise:~$ time -p perl tt2.pl
0.999996550854185
0.999988689081981
0.999954812938235
0.999954112486751
0.999952499012984
0.999916621841294
0.999913707186565
0.999912082149152
0.999911171437891
0.999908870450732
real 0.88
user 0.84
sys 0.03
peter@anise:~$ time -p perl tt3.pl
0.999996013738279
0.999986292514876
0.999969367845608
0.999963241575056
0.999954341617741
0.999953365735578
0.999946424706014
0.999936395023607
0.999934874017583
0.99993412098663
real 0.15
user 0.15
sys 0.00
peter@anise:~$
#!/usr/bin/perl
use strict;
use warnings;
my @top10 = ( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
for ( my $i = 0 ; $i < 100000 ; $i++ ) {
my $num = rand();
top_sort( $num );
}
# PRINT RESULTS
foreach ( reverse @top10 ) {
print $_. "\n";
}
sub top_sort {
my ( $value ) = @_;
if($value > $top10[0]) {
$top10[0] = $value;
@top10 = sort @top10;
}
}
------------------------------
Date: Thu, 07 Oct 2004 14:13:42 +0200
From: Fatted <fatted@gmail.com>
Subject: Re: Top 10 list algorithm
Message-Id: <2skq7pF1m1hn9U1@uni-berlin.de>
Christian Winter wrote:
> Fatted schrieb:
>
>> Is there a better (faster) way of implementing my top_sort function?
>> (Creating a top 10 list of the highest numbers from a large set).
>> Or did I get lucky? :)
>
>
> I'd get rid of all those shifts and if()s. I didn't time the
> speed, but my solution would look like following:
Thanks for your solution. I timed yours against mine (but over 50000
iterations) and its a bit slower (but a hellva lot neater):
# WINTER
$ perl -d:DProf winter.pl
0.999946217800538
0.999944015909879
0.999942441099883
0.999925040395816
0.999911130801731
0.999891723472704
0.99987181193384
0.999861976302704
0.999800055313095
0.999793198711462
$ dprofpp
Total Elapsed Time = 1.639946 Seconds
User+System Time = 1.549946 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
0.65 0.010 0.010 1 0.0100 0.0100 warnings::BEGIN
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - warnings::import
0.00 - 0.010 2 - 0.0050 main::BEGIN
# MINE
# MINE
$ perl -d:DProf mine.pl
0.999987822921746
0.999961276796125
0.999960878033757
0.999940209732916
0.999902694088071
0.999897664019262
0.99988878249334
0.999879222743452
0.999863848759027
0.999815730523473
$ dprofpp
Total Elapsed Time = 0.829862 Seconds
User+System Time = 0.809862 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c Name
52.4 0.425 0.425 50000 0.0000 0.0000 main::top_sort
1.23 0.010 0.010 1 0.0100 0.0100 warnings::BEGIN
1.23 0.010 0.020 2 0.0050 0.0100 main::BEGIN
0.00 - -0.000 1 - - strict::bits
0.00 - -0.000 1 - - strict::import
0.00 - -0.000 1 - - warnings::import
------------------------------
Date: Thu, 07 Oct 2004 14:25:11 +0200
From: Fatted <fatted@gmail.com>
Subject: Re: Top 10 list algorithm
Message-Id: <2skqt9F1m55goU1@uni-berlin.de>
Peter Hickman wrote:
> However as the numbers get bigger, from 10000 to 100000, yours is faster
> than mine. So I wrote a version 3, having @top10 as a global variable
> rather than passing it around helped alot.
Yep, thats about 3 times faster (and tidier) over 50000 iterations than
mine using "time -p". ("perl -d:DProf" was giving me negative times :)
Thanks.
------------------------------
Date: Thu, 07 Oct 2004 08:28:02 -0400
From: Shawn Corey <shawn.corey@sympatico.ca>
Subject: Re: Top 10 list algorithm
Message-Id: <iCa9d.31170$HO1.1206457@news20.bellglobal.com>
Christian Winter wrote:
> Fatted schrieb:
>
>> Is there a better (faster) way of implementing my top_sort function?
>> (Creating a top 10 list of the highest numbers from a large set).
>> Or did I get lucky? :)
>
>
> I'd get rid of all those shifts and if()s. I didn't time the
> speed, but my solution would look like following:
> ------------------------------------------------------------
> #!perl
>
> use strict;
> use warnings;
>
> my @top10 = (0,0,0,0,0,0,0,0,0,0);
>
> for (0..9999)
> {
> # Just sort random value into @top10 and get the
> # 10 topmost values via hash slice notation.
> @top10 = (sort { $b <=> $a } @top10, rand())[0..9];
> }
>
> foreach (@top10)
> {
> print $_."\n";
> }
>
> __END__
>
> -------------------------------------------------------------
>
> -Christian
I wouldn't sort for every number added.
--- Shawn
#!/usr/bin/perl
use strict;
use warnings;
my @top10 = ( 0 ) x 10;
for my $i ( 1 .. 10_000 ){
my $num = rand();
push @top10, $num;
# Change 1_000 to be as large as your memory can handle
if( @top10 > 1_000 ){
@top10 = sort { $b <=> $a } @top10;
$#top10 = 9;
}
}
$#top10 = 9;
print "$_\n" for @top10;
__END__
------------------------------
Date: 7 Oct 2004 12:47:43 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Top 10 list algorithm
Message-Id: <ck3dtf$5i0$1@mamenchi.zrz.TU-Berlin.DE>
Fatted <fatted@gmail.com> wrote in comp.lang.perl.misc:
> Is there a better (faster) way of implementing my top_sort function?
> (Creating a top 10 list of the highest numbers from a large set).
> Or did I get lucky? :)
The standard approach to selection of the top k of n elements is
to use a heap.
A heap is a data structure that allows insertion of arbitrary elements
and extraction of the minimum element, both in log k time where k is
the size of the heap. Inspection of the minimum can be done in unit
time. There are a few implementations on CPAN.
The strategy is to insert elements in the heap that are larger than
the current minimum (or any element when the heap is empty). If
the heap size is greater than k, also remove the current minimum as
you insert a new element. In the end, the heap contains the top
k elements, which can be extracted in ascending order.
The process takes n*log k time instead of the n*log n needed to
sort all n values.
If n is much larger than k, you can even use a simple list instead
of a heap. You either sort it after each insertion, or insert each
element in its place (straight insertion sort). The resulting
n*k*log k time will still be better than n*log n if n is big and
k is small.
Anno
------------------------------
Date: Thu, 07 Oct 2004 14:26:31 +0200
From: Johan Vromans <jvromans@squirrel.nl>
Subject: Re: Version? Getopt::Long
Message-Id: <416535f8$0$124$e4fe514c@dreader17.news.xs4all.nl>
"Paul Lalli" <mritty@gmail.com> writes:
> The canonical way is:
>
> perl -MGetopt::Long -e 'print $Getopt::Long::VERSION'
>
> Of course, that would depend upon the author of this module following
> the convention of creating a package variable $VERSION with the
> appropriate information.
... which he does :-).
This will also work:
% perl -MGetopt::Long=3.00
Getopt::Long version 3.00 required--this is only version 2.34 ...
-- Johan
Author and maintainer of the one and only Getopt::Long.
------------------------------
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 7218
***************************************