[18029] in Perl-Users-Digest
Perl-Users Digest, Issue: 189 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Feb 1 03:10:37 2001
Date: Thu, 1 Feb 2001 00:10:17 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <981015017-v10-i189@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Thu, 1 Feb 2001 Volume: 10 Number: 189
Today's topics:
Re: Include bug in HTML::Template 2.2 <james@NOSPAM.demon.co.uk>
Re: Include bug in HTML::Template 2.2 (Eric Bohlman)
making -w flag part of perl command shanmughom_sivasubra@my-deja.com
Re: making -w flag part of perl command (Eric Bohlman)
Re: making -w flag part of perl command (Bernard El-Hagin)
Newbie question 1st script topdawg@charter.net
Re: Newbie question 1st script <artd@artd3.com>
Re: Newbie question 1st script dtbaker_dejanews@my-deja.com
Re: open(), quoting, and Win32 woes <jdf@pobox.com>
Problem running perl scripts on Netscape server <krishnasanjay@hotmail.com>
Re: qr and backreferences (Ilya Zakharevich)
Re: question about sort <jdf@pobox.com>
Re: Question on appending to file dtbaker_dejanews@my-deja.com
Re: question on external functions bits101010@my-deja.com
single executable file <hauhau@seed.net.tw>
Re: single executable file <johnm@acadiacom.net>
Re: single executable file <wyzelli@yahoo.com>
Re: Sorting in NT like in Unix <jdf@pobox.com>
Re: sorting IP addresses (David Efflandt)
Re: sorting IP addresses (Charles DeRykus)
Re: sorting IP addresses <uri@sysarch.com>
Re: sorting IP addresses (Garry Williams)
Re: sorting through a hash of arrays bits101010@my-deja.com
Re: substituting "@" with regexp <nospam.newton@gmx.li>
Re: Using global var in subroutine dtbaker_dejanews@my-deja.com
What is the perlrun switch for "use" and "use lib"? <johnlin@chttl.com.tw>
Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 1 Feb 2001 03:38:20 +0000
From: James Taylor <james@NOSPAM.demon.co.uk>
Subject: Re: Include bug in HTML::Template 2.2
Message-Id: <ant0103201cbfNdQ@oakseed.demon.co.uk>
A few days ago I posted the following article, but it had no comment
from anyone. Is this because I have chosen the wrong group for this
question, or have I unknowingly upset the group and been killfiled?
In article <ant3104210b0fNdQ@oakseed.demon.co.uk>, James Taylor
<URL:mailto:james@NOSPAM.demon.co.uk> wrote:
>
> I think I've found a bug in HTML::Template but I'll pass it by you
> guys first as a sanity check. I noticed it in version 2.0 but I've
> just downloaded 2.2 from CPAN and the alleged bug's still there.
>
> I have the output of one template pass which I am using to create a
> new template object via the scalarref option to new, like this:
>
> $newtemplate = HTML::Template->new( scalarref => \$tempoutput,
> die_on_bad_params => 0,
> );
>
> This works fine unless the string in $tempoutput uses a TMPL_INCLUDE tag
> to include another file, in which case my program gives the following errors:
>
> Use of uninitialized value at /PerlSiteLib:/HTML/Template.pm line 2095.
> Modification of non-creatable array value attempted, subscript -1 at /PerlSiteLib:/HTM
> L/Template.pm line 1419.
>
> I took a look at line 2095 and I saw this:
>
> my @path = split('/', $options->{filepath});
>
> After a bit of fiddling around I've discovered I can work around the
> problem by calling new() with the parameter pair: filepath => '.'
>
> I don't understand why filepath => '.' fixes it, or why it should be
> necessary, after all "filepath" is an undocumented option and I'd
> really rather not have to use it. I had a look at the similar option
> called "path" the use of which does NOT fix the problem in the way one
> would expect.
>
> I have double checked that the syntax of the templates I am using are
> correct, and that the file named in the TMPL_INCLUDE is present in the
> correct place. Please could someone tell me if I'm doing something
> fundamentally wrong, or whether this is a genuine bug in
> HTML::Template. Thank you.
I hope someone out there can help. Thanks again.
--
James Taylor <james (at) oakseed demon co uk>
Based in Hammersmith, London, UK.
PGP key available ID: 3FBE1BF9
Fingerprint: F19D803624ED6FE8 370045159F66FD02
------------------------------
Date: 1 Feb 2001 06:28:59 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: Include bug in HTML::Template 2.2
Message-Id: <95avnb$81d$1@bob.news.rcn.net>
James Taylor <james@nospam.demon.co.uk> wrote:
> A few days ago I posted the following article, but it had no comment
> from anyone. Is this because I have chosen the wrong group for this
> question, or have I unknowingly upset the group and been killfiled?
Probably neither. "A few days ago" was actually *one* day before you
posted this. Usenet isn't an instantaneous medium. My *guess* (and it's
only a guess) is that in the day since you posted it, nobody who's
familiar enough with HTML::Template to properly answer your question has
had the chance to:
a) Have their news server pick up your original post *and*
b) Read comp.lang.perl.misc *and*
c) Come across your post before they had to do something else *and*
d) Read your post *and*
e) Formulate a response (might take some time to do research; remember
that the right answer next week is more valuable than the wrong answer
today) *and*
f) Compose and post their response *and*
g) Have the response propagate as far as your news server.
A lot of things have to happen there. It appears that you've done
everything you can reasonably do on your end; you used a highly
descriptive Subject: header, described the problem precisely, and included
actual code. But you've described a non-trivial problem, which means that
it's likely to take a while for someone to solve it. Have patience.
------------------------------
Date: Thu, 01 Feb 2001 03:41:09 GMT
From: shanmughom_sivasubra@my-deja.com
Subject: making -w flag part of perl command
Message-Id: <95alsl$mq5$1@nnrp1.deja.com>
Hi,
How can I make the -w flag part of the 'perl'
command itself in my installation so that I don't have to specify it
in all my scripts?
Thanks.
Regards,
Siva
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 1 Feb 2001 06:49:35 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: making -w flag part of perl command
Message-Id: <95b0tv$81d$2@bob.news.rcn.net>
shanmughom_sivasubra@my-deja.com wrote:
> How can I make the -w flag part of the 'perl'
> command itself in my installation so that I don't have to specify it
> in all my scripts?
Read the section in perlrun about the PERL5OPT environment variable.
------------------------------
Date: Thu, 1 Feb 2001 07:33:46 +0000 (UTC)
From: bernard.el-hagin@lido-tech.net (Bernard El-Hagin)
Subject: Re: making -w flag part of perl command
Message-Id: <slrn97i4aq.2q0.bernard.el-hagin@gdndev25.lido-tech>
On Thu, 01 Feb 2001 03:41:09 GMT, shanmughom_sivasubra@my-deja.com
<shanmughom_sivasubra@my-deja.com> wrote:
>Hi,
>
> How can I make the -w flag part of the 'perl'
>command itself in my installation so that I don't have to specify it
>in all my scripts?
If you're on a Unix you can create an alias.
Cheers,
Bernard
--
#requires 5.6.0
perl -le'* = =[[`JAPH`]=>[q[Just another Perl hacker,]]];print @ { @ = [$ ?] }'
------------------------------
Date: Wed, 31 Jan 2001 22:33:00 -0600
From: topdawg@charter.net
Subject: Newbie question 1st script
Message-Id: <q9ph7tsiarkjbc1oichcpveha2rqgpdomk@4ax.com>
I am brand new to Perl. Ok, I have created my first script. nothing
big. But I want to send it to a friend to run. How does another person
run Perl scripts. Do they have to have Perl installed on there
machine? This would be win2k or 95 boxes.
If I want to run admin scripts on my servers, I would have to have
PERL installed on those servers.?
Thanks in advance
------------------------------
Date: Wed, 31 Jan 2001 23:51:17 -0500
From: "Art" <artd@artd3.com>
Subject: Re: Newbie question 1st script
Message-Id: <t7hqq6e8gf2349@corp.supernews.com>
Yes, for wintel machines take a look at http://www.activestate.com you can
download perl for win32...
<topdawg@charter.net> wrote in message
news:q9ph7tsiarkjbc1oichcpveha2rqgpdomk@4ax.com...
> I am brand new to Perl. Ok, I have created my first script. nothing
> big. But I want to send it to a friend to run. How does another person
> run Perl scripts. Do they have to have Perl installed on there
> machine? This would be win2k or 95 boxes.
> If I want to run admin scripts on my servers, I would have to have
> PERL installed on those servers.?
>
> Thanks in advance
------------------------------
Date: Thu, 01 Feb 2001 04:55:00 GMT
From: dtbaker_dejanews@my-deja.com
Subject: Re: Newbie question 1st script
Message-Id: <95aq72$q70$1@nnrp1.deja.com>
In article <q9ph7tsiarkjbc1oichcpveha2rqgpdomk@4ax.com>,
topdawg@charter.net wrote:
> I am brand new to Perl. Ok, I have created my first script. nothing
> big. But I want to send it to a friend to run. How does another person
> run Perl scripts. Do they have to have Perl installed on there
> machine? This would be win2k or 95 boxes.
> If I want to run admin scripts on my servers, I would have to have
> PERL installed on those servers.?
-----------
yes, you need perl installed on whatever box the script is going to
run...
The good news is that for *most* things your script will be platform
independent. If you write it on UNIX, you can probably move the source
to a win98 machine that has perl installed, and run the same script,
unless it calls UNIX system functions directly. You have to be a little
careful the way you handle directory paths and a few other things.
There *are* ways to "compile" it to get around installing perl at a
client site, but it doesnt work for everything. You can check out
www.perl2exe.com
D
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 01 Feb 2001 01:38:44 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: open(), quoting, and Win32 woes
Message-Id: <y9vqoqkr.fsf@pobox.com>
allandclarke@my-deja.com writes:
> my $results = `"/path/myprog.exe" "/otherpath/arg1"`;
>
> I naively put quotes around both items because the path components
> may contain spaces.
You will have to use the DOS 8 character directory names in your
path. So for example, if myprog is in "space dir", you'd refer to
c:/spaced~1/myprog.exe
HTH.
--
Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
http://pobox.com/~jdf
------------------------------
Date: Thu, 01 Feb 2001 07:28:54 GMT
From: Sanjay Krishna <krishnasanjay@hotmail.com>
Subject: Problem running perl scripts on Netscape server
Message-Id: <95b37j$rb$1@nnrp1.deja.com>
Hi,
I am trying to run a perl script wrapped in a .bat file. So the
contents of the bat file are
c:\perl\bin\perl script.pl
script.pl contains
.....
.....
print "Location: $url\n\n";#$url contains the url where it has to be
redirected to.
when I execute the perl script using...
http://mymachine.domain.com/cgi/test.bat
I get this message in the browser
Location : url_itself
in short redirection is not working. Also if I add @echo off as the
first line in the .bat file the perl script doesn't execute.
I am using Active state Perl/Windows NT/Netscape FastTrack 2.01 Server
Any help is greatly appreciated.
Thanks
Sanjay
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: 31 Jan 2001 19:59:23 GMT
From: ilya@math.ohio-state.edu (Ilya Zakharevich)
Subject: Re: qr and backreferences
Message-Id: <959qqr$1k0$1@charm.magnus.acs.ohio-state.edu>
[A complimentary Cc of this posting was sent to Abigail
<abigail@foad.org>],
who wrote in article <slrn97flc2.c6t.abigail@tsathoggua.rlyeh.net>:
[about $rex = qr/(.)\1/, $dREX = qr/$rex$rex/;]
> That's not fixing the problem, it just avoids it in a specific case.
> It won't work for instance for:
>
> /$repeat$repeat/;
>
> which matches 'aaba' and not aabb.
>
> I strongly suggest that the original poster uses perlbug to report this.
This is a feature, not a bug. qr// is just a quoting operator. This
$rex1 = qr/(.)\1/;
$rex2 = qr/(.)\2/;
$dREX = qr/$rex1$rex2/;
should work as expected. perldoc perlop for details.
But the functionality is badly needed indeed. Since to implement \g<
and \g> (which would set beginning/end-of-$&) one needs something like
"reorderings of match variables" anyway, I think the following
solution is both easy to implement (at least after \g< and \g> are
there), and solves a lot of current deficiencies:
Implement a new //w "wrap" modifier - and the matched (?w:) construct;
a) Inside each (?w:) the numeration of backreferences and ()-groups
should start anew:
$rex = qr/(.)\1/w;
$dREX = $dREX = qr/$rex$rex/;
will match the same as /(.)\1(.)\2/ (but will not set $1 and $2,
see below).
b) Possible: (?-w:) to return to the enclosing numeration:
(.) (?w: (.)
\1
(?-w: \1 )
\1
)
will match the same as (.)(.)\2\1\2
c) The $<digit> variables reflect the ()-groups on the topmost level
of wrapping only (no things inside (?w:) leak to $<digit>);
d) Possible: the *array context result* of matching reflects the
()-groups on the two topmost levels only. No things inside (?ww:)
leak to @array:
@array = / (.)\1 (?ww: (.)\1 ) /g
Will put to @array only A from each AABB group. Similarly,
@array = / (.)\1 (?ww: (.)\1 ) /;
will set both @array and $1, but
@array = / (.)\1 (?w: (.)\1 ) /w;
will set @array, but not $1.
a) c) and d) were wanted for a looong time. b) is at least logical,
and maybe may also solve some problems ;-).
Ilya
------------------------------
Date: 01 Feb 2001 01:24:20 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: question about sort
Message-Id: <3ddyq5t7.fsf@pobox.com>
k9boy@my-deja.com writes:
> Okay, maybe I'm too new to all this, but what FAQ?
If you have perl, then you have the FAQ, along with many hundreds of
pages of other documentation. It came with perl. I'm guessing you
use Windows, right? Go to the Start menu, look for "ActiveState
ActivePerl" in the Programs menu, et voila! The docs.
> Well, thanks for making me aware of an FAQ. Didn't mean to come off
> as a lazy bum :)
Reacting, as you did, with humor and contrition goes a long way
towards the benefit of a doubt.
However, you still have a couple of lessons to learn.
1) You should read the news group for at least a few days before you
post, just to get an idea of the culture here.
2) You should never post your reply on top of the quoted OP (original
post), as that makes it much harder for people to follow the thread
of conversation. Furthermore, you should edit the OP down to the
fewest lines possible, no more than is necessary to provide a
context for your reply. Look at how I formatted this post, for
example.
Good luck in learning Perl. And speaking of learning Perl, I strongly
suggest you buy yourself a copy of the book "Learning Perl". You'll
enjoy it.
--
Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
http://pobox.com/~jdf
------------------------------
Date: Thu, 01 Feb 2001 03:10:12 GMT
From: dtbaker_dejanews@my-deja.com
Subject: Re: Question on appending to file
Message-Id: <95ak2i$ld8$1@nnrp1.deja.com>
In article <3A78B43F.CD9D5951@coventry.ac.uk>,
John Tutchings <ccx138@coventry.ac.uk> wrote:
>
> It reads a line from the file handle and throws it away, it may look
as if
> it does nothing but it is the same as saying..
> $junk = <FILE>;
> undef($junk);
------------
if this is true... and the file contained many lines, wouldnt it simply
read ALL the lines into the scalar $junk before undef-ing it? i.e. using
the memory? I think the original test would catch an EMPTY file, but not
one with one line in it; and would have problems with very large files.
whatever.... I havent tested, I just thought I'd point out a possible
memory problem with an unknown file size.
D
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 01 Feb 2001 02:14:28 GMT
From: bits101010@my-deja.com
Subject: Re: question on external functions
Message-Id: <95agpu$ilo$1@nnrp1.deja.com>
Something like the following?
#!/usr/local/bin/perl
system("echo hello world");
Winston.
In article <3A7898B2.DE2F6BFA@trg.saic.com>,
bob@trg.saic.com wrote:
> Can someone give me a simple example of how to call an external
function that
> prints the arguement sent to it (e.g. "Hello world")?
>
> Thanks,
>
> ~ Bob
>
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 1 Feb 2001 11:00:11 +0800
From: "hauhau" <hauhau@seed.net.tw>
Subject: single executable file
Message-Id: <95aj6r$n08@netnews.hinet.net>
Hello! Every body!
I need to compile Perl and the modules into ONE single executable file.
Because I can't use any dll in my system. Does anyone can tell me how to do,
or where is the doc about that ?
Thansk!
Hsing Hau Wang
------------------------------
Date: Thu, 1 Feb 2001 00:29:23 -0600
From: "John Michael" <johnm@acadiacom.net>
Subject: Re: single executable file
Message-Id: <3a79042f$1@news.acadiacom.net>
You can not compile perl. It uses an open source interprter. You need to
write it in c, c++, java, or another lower level language if you want to
compile it.
"hauhau" <hauhau@seed.net.tw> wrote in message
news:95aj6r$n08@netnews.hinet.net...
> Hello! Every body!
> I need to compile Perl and the modules into ONE single executable file.
> Because I can't use any dll in my system. Does anyone can tell me how to
do,
> or where is the doc about that ?
> Thansk!
>
> Hsing Hau Wang
>
>
------------------------------
Date: Thu, 1 Feb 2001 16:25:56 +0930
From: "Wyzelli" <wyzelli@yahoo.com>
Subject: Re: single executable file
Message-Id: <vC7e6.29$wU5.6555@vic.nntp.telstra.net>
"hauhau" <hauhau@seed.net.tw> wrote in message
news:95aj6r$n08@netnews.hinet.net...
> Hello! Every body!
> I need to compile Perl and the modules into ONE single executable
file.
> Because I can't use any dll in my system. Does anyone can tell me how
to do,
> or where is the doc about that ?
> Thansk!
>
Look into Perl2exe
Wyzelli
--
@x='07411711511603209711011111610410111403208010111410803210409709910710
1114'=~/(...)/g;
print chr for @x;
------------------------------
Date: 01 Feb 2001 02:14:14 -0500
From: Jonathan Feinberg <jdf@pobox.com>
Subject: Re: Sorting in NT like in Unix
Message-Id: <lmrqooxl.fsf@pobox.com>
"Chris W" <chrisw@dynamite.com.au> writes:
> <donz30@my-deja.com> wrote in message news:958ksh$t5q$1@nnrp1.deja.com...
> > I need to find a way to sort in NT like in Unix environment.
> > sort -k 4
>
> There is nothing in the standard NT environment that has the equivalent
> functionality.
Every programmer who finds him/herself stuck in Win32 should have the
Cygwin distribution, which provides an excellent shell (bash), and the
most important Unix tools.
http://www.cygwin.com
--
Jonathan Feinberg jdf@pobox.com Sunny Brooklyn, NY
http://pobox.com/~jdf
------------------------------
Date: Thu, 1 Feb 2001 02:06:24 +0000 (UTC)
From: efflandt@xnet.com (David Efflandt)
Subject: Re: sorting IP addresses
Message-Id: <slrn97hh3k.rk4.efflandt@efflandt.xnet.com>
On Wed, 31 Jan 2001, mike_solomon@lineone.net <mike_solomon@lineone.net> wrote:
>I have written a script to sort a list of IP addresses that I take
>from an Access database.
>
>It is working properly but I wandered if there was any way I could
>simplify it.
>
>I include here the relevant code
>
>I am using strict and have predeclared the variables.
>
>#run sql
>$cO->Sql("SELECT ipaddress FROM ip_address");
>
># Fetch the next rowset
>while($cO->FetchRow()){
> undef %Data;
> my %Data = $cO->DataHash();
>
>#split ip address
> ($ip1,$ip2,$ip3,$ip4) = split(/\./,$Data{ipaddress});
>
> $ips = ($ip1*10000) + ($ip2 * 1000) + ($ip3 + 100) + $ip4 ;
>
> $new{$ips} = $Data{ipaddress};
>}
>
>foreach (sort keys %new) {
> print"$new{$_}\n";
>};
>
>
>Any suggestions will be gratefully received
The following is a test example of converting IPs entered on the
commandline to their binary format, sorting them and converting back to
IPs:
#!/usr/bin/perl -w
my (@list,@new,@old,@ip);
while($_ = shift) { push @list,$_; }
foreach (@list) { push @old, pack('C4',split(/\./,$_)); }
@new = sort { $a cmp $b } @old;
@list = ();
foreach (@new) {
(@ip) = unpack('C4',$_);
push @list,join(".",@ip);
}
foreach (@list) { print "$_\n"; }
--
David Efflandt efflandt@xnet.com http://www.de-srv.com/
http://www.autox.chicago.il.us/ http://www.berniesfloral.net/
http://cgi-help.virtualave.net/ http://hammer.prohosting.com/~cgi-wiz/
------------------------------
Date: Thu, 1 Feb 2001 02:23:01 GMT
From: ced@bcstec.ca.boeing.com (Charles DeRykus)
Subject: Re: sorting IP addresses
Message-Id: <G823yD.Fxy@news.boeing.com>
In article <9598im$cdh$1@nnrp1.deja.com>, <mike_solomon@lineone.net> wrote:
>I have written a script to sort a list of IP addresses that I take
>from an Access database.
>
>It is working properly but I wandered if there was any way I could
>simplify it.
>
>I include here the relevant code
>
>I am using strict and have predeclared the variables.
>
>#run sql
>$cO->Sql("SELECT ipaddress FROM ip_address");
>
># Fetch the next rowset
>while($cO->FetchRow()){
> undef %Data;
> my %Data = $cO->DataHash();
>
>#split ip address
> ($ip1,$ip2,$ip3,$ip4) = split(/\./,$Data{ipaddress});
>
> $ips = ($ip1*10000) + ($ip2 * 1000) + ($ip3 + 100) + $ip4 ;
>
> $new{$ips} = $Data{ipaddress};
>}
>
>foreach (sort keys %new) {
> print"$new{$_}\n";
>};
>
use Socket;
while ...
...
$new{ inet_aton $Data{ipaddress} } = $Data{ipaddress};
}
print "$_\n" for @new{ sort keys %new };
__END__
hth,
--
Charles DeRykus
------------------------------
Date: Thu, 01 Feb 2001 05:03:18 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: sorting IP addresses
Message-Id: <x7bssnro4q.fsf@home.sysarch.com>
>>>>> "DE" == David Efflandt <efflandt@xnet.com> writes:
DE> The following is a test example of converting IPs entered on the
DE> commandline to their binary format, sorting them and converting back to
DE> IPs:
DE> #!/usr/bin/perl -w
DE> while($_ = shift) { push @list,$_; }
@list = @ARGV ;
but why even copy it?
DE> foreach (@list) { push @old, pack('C4',split(/\./,$_)); }
you have to learn map. and why is it called @old?
@old = map pack( 'C4', split(/\./, $_ ), @ARGV ;
DE> @new = sort { $a cmp $b } @old;
??? why use cmp when that is the default compare code used inside sort?
@new = sort @old;
DE> @list = ();
DE> foreach (@new) {
DE> (@ip) = unpack('C4',$_);
DE> push @list,join(".",@ip);
DE> }
again, map is your friend:
@list = map unpack( 'C4', $_ ), @new ;
hmm, i wonder what would happen if i merged all those map and sort
operations?
@list = map { unpack( 'C4', $_ ) }
sort
map pack( 'C4', split(/\./, $_ ), @ARGV ;
that looks interesting. maybe i will write a paper on it. :)
i leave printing it in dotted quad as an exercise to the reader.
uri
--
Uri Guttman --------- uri@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
------------------------------
Date: Thu, 01 Feb 2001 06:05:20 GMT
From: garry@zvolve.com (Garry Williams)
Subject: Re: sorting IP addresses
Message-Id: <A07e6.183$Sn3.9113@eagle.america.net>
On Wed, 31 Jan 2001 14:47:54 GMT, mike_solomon@lineone.net
<mike_solomon@lineone.net> wrote:
>I have written a script to sort a list of IP addresses that I take
>from an Access database.
>
>It is working properly but I wandered if there was any way I could
>simplify it.
It does _not_ work properly. See below.
>#run sql
>$cO->Sql("SELECT ipaddress FROM ip_address");
>
># Fetch the next rowset
>while($cO->FetchRow()){
> undef %Data;
> my %Data = $cO->DataHash();
>
>#split ip address
> ($ip1,$ip2,$ip3,$ip4) = split(/\./,$Data{ipaddress});
>
> $ips = ($ip1*10000) + ($ip2 * 1000) + ($ip3 + 100) + $ip4 ;
> ^
> $new{$ips} = $Data{ipaddress}; ^
>} ^
^
I assume that this is a typo. Don't do that. Copy and paste code to
your post.
Since each part of the dotted-decimal version of an IP address
represents 256 possibilities, your "shift" algorithm will not produce
a correct ordering.
Try running your algorithm on these two addresses and see how it
orders them. I think you will be disappointed:
1.1.99.102
1.1.100.1
Your algorithm converts the first to 21002 and the second to 21001.
The conversion does _not_ preserve the original ordering.
Try these on your algorithm:
1.1.99.101
1.1.100.1
Now you lose one of the addresses! These two map to the same key in
your result hash. :-(
One more point: since you use your result as a key in a hash, you
cannot sort duplicate addresses. (That may not be a problem in your
data set, but it means that your algorithm -- even when corrected --
see below -- will not work in other applications of sorting IP
addresses.)
You were close to the right idea, though.
You can think of a dotted-decimal representation as being a four digit
base 256 number. Converting it to decimal would be this instead:
$ips = $ip1 * 256**3 + $ip2 * 256**2 + $ip3 * 256 + $ip4;
or
$ips = $ip1 * 16777216 + $ip2 * 65536 + $ip3 * 256 + $ip4;
This will correct your algorithm, but why convert an IP address to
decimal in the first place? By definition, an IP address (ipv4) is an
unsigned 32-bit binary number. Convert it to binary and sort that as
a simple string.
The Socket module (part of the standard distribution of perl) provides
two functions that convert back and forth between binary and dotted-
decimal forms. Using them changes your snippet to this:
# Untested
use Socket;
my @ip_list;
$cO->Sql("SELECT ipaddress FROM ip_address");
while ($cO->FetchRow()) {
my %data = $cO->DataHash();
push @ip_list, inet_aton($data{ipaddress});
}
print join "\n", map { inet_ntoa($_) } sort @ip_list;
print "\n";
More generally, to sort a list of IP addresses that are in dotted-
decimal form:
# Tested
sub sort_ip {
return map { inet_ntoa($_) }
sort
map { inet_aton($_) }
@_;
}
(See "How do I sort an array by (anything)?" in the perlfaq4 manual
page and the sort() section of the perlfunc manual page for an
explanation of the last bit.)
--
Garry Williams
------------------------------
Date: Thu, 01 Feb 2001 02:41:30 GMT
From: bits101010@my-deja.com
Subject: Re: sorting through a hash of arrays
Message-Id: <95aicq$k0v$1@nnrp1.deja.com>
# 1-based unique key
# can be 1 through N (last column of data)
$unique = 1;
# number of columns in data, N
$field_num = 3;
# constructing the regular expression
# eg. (.*?)\t(.*?)\t(.*?)
$re = '(.*?)';
for $i(1..$field_num-1) {
$re .= '\t(.*?)';
}
# go through the lines of data, hash the matched unique key with the
whole line
foreach (@lines) {
chomp;
/($re)/;
$HASH{$$unique} = $_;
}
Hope this helps...
Winston.
In article <95adkn$g75$1@nnrp1.deja.com>,
onarasim@my-deja.com wrote:
> Hi,
> Here is the problem I am facing.
> 1. I have a text file which has a number of fields, delimited by tabs.
> Each of the fields has some data - all can be treated as strings.
>
> Now, I am interested in ensuring that I keep only those records with
> unique values for (say) field 1. Uniqueness suggests that I need a
> hash - the wrinkle is that I don't just want a list of unique field 1
> values, but instead, the complete record for those cases. Do I need
> some kind of array reference as the hash value here? Any suggestions
on
> how I could proceed?
>
> Thanks a lot for any help.
>
> Sent via Deja.com
> http://www.deja.com/
>
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 01 Feb 2001 07:19:03 +0100
From: "Philip 'Yes, that's my address' Newton" <nospam.newton@gmx.li>
Subject: Re: substituting "@" with regexp
Message-Id: <pkvh7tkca8ch0spsiclojjfesrrnemcsbg@4ax.com>
On Tue, 30 Jan 2001 23:40:05 -0700, Todd Ahlstrom <todda@xmission.com> wrote:
[the CGI module]
> yes, I downloaded it, I have now seen the light. : )
It should be part of the standard Perl distribution. (However, if you have an
old Perl, the CGI.pm version available "out there", e.g. CPAN or Lincoln Stein's
page, may be more current than the one shipped with Perl.)
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Thu, 01 Feb 2001 05:18:39 GMT
From: dtbaker_dejanews@my-deja.com
Subject: Re: Using global var in subroutine
Message-Id: <95arjb$rd3$1@nnrp1.deja.com>
In article <t7bsai40pbbm04@corp.supernews.com>,
"Steve Yates" <steve@teamITS.com> wrote:
> I have a scoping issue that I'm trying to figure out, and it just
seems
> like there's an easy answer.
>
> sub ReadGlobalSpecialty {
> undef %Specialty; undef %CodeSpecialty; undef %SpecialtyDescr;
> local ($x, $y, @x);
> open( SPECCODE, "$TEMPLATES$userCode/file.txt" ) || # (...)
> # (...)
> }
>
-----------------
take a close look at wherever $userCode is defined and make sure:
- it is not inside a loop
- it is not inside another sub
- it is "before" this sub is "required" or called
D
Sent via Deja.com
http://www.deja.com/
------------------------------
Date: Thu, 1 Feb 2001 10:30:46 +0800
From: "John Lin" <johnlin@chttl.com.tw>
Subject: What is the perlrun switch for "use" and "use lib"?
Message-Id: <95ahro$l6o@netnews.hinet.net>
Dear all,
How to translate the following "use" and "use lib"
into command line switches?
use FindBin;
use lib $FindBin::Bin;
My experiment is:
put main.pl and A.pm in /some/other/directory
# -------------------------- A.pm
package A;
sub hello { print "hello\n" }
1
# -------------------------- main.pl
use A;
A::hello;
run from /home/john
perl -MFindBin -I'$FindBin::Bin' /some/other/directory/main.pl
I got the error message
Can't locate A.pm in @INC (@INC contains: $FindBin Bin ...
Of course I know the problem is in -I'$FindBin::Bin'.
What shall I do to get "use lib $FindBin::Bin"?
My intension is to set
PERL5OPT = '-w -Mstrict -MFindBin -I$FindBin::Bin'
so I can get rid of those annoying "use strict"
and "use lib $FindBin::Bin" everywhere in my programs.
Just set them as default options.
Thank you.
John Lin
------------------------------
Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.
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 189
**************************************