[31395] in Perl-Users-Digest
Perl-Users Digest, Issue: 2647 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 23 03:09:43 2009
Date: Fri, 23 Oct 2009 00:09:09 -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 Fri, 23 Oct 2009 Volume: 11 Number: 2647
Today's topics:
'use' command being executed <jhavero@gmail.com>
Re: 'use' command being executed <jimsgibson@gmail.com>
Re: 'use' command being executed <anfi@onet.eu>
Re: 'use' command being executed <ben@morrow.me.uk>
Re: 'use' command being executed <jurgenex@hotmail.com>
Re: Indirection in a hash <mvdwege@mail.com>
Re: Indirection in a hash <uri@StemSystems.com>
Re: Indirection in a hash <jurgenex@hotmail.com>
Re: Indirection in a hash <mvdwege@mail.com>
Re: Indirection in a hash <mvdwege@mail.com>
Re: Indirection in a hash <uri@StemSystems.com>
Re: parse multi line string into hash <user@example.net>
Re: Want to write a script to note specific IP addresse sln@netherlands.com
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Thu, 22 Oct 2009 14:01:46 -0700 (PDT)
From: jhavero <jhavero@gmail.com>
Subject: 'use' command being executed
Message-Id: <fda5dba4-0f89-47fb-9bd4-cb9dc43abd9c@r31g2000vbi.googlegroups.com>
The 'use' command below tries to run when this is executed from Unix
so I get an error that it cannot find the Win32::File module. If I
comment out this 'use' line the script works in Unix and the 'print
"Unix"' statement runs and the 'print "Windows"' statement never runs.
How can I stop it from running the 'use' command when run from Unix.
# Main
.
.
.
my %FINDOPTIONS = (
'wanted' => \&entry,
'no_chdir' => 1
);
.
.
.
find(\%FINDOPTIONS,@COMPONENTS);
sub entry {
if($^O eq 'MSWin32') {
#Windows
use Win32::File qw/ GetAttributes SetAttributes /;
print "Windows\n";
}
} else {
#unix
print "Unix\n";
}
} #end sub entry
------------------------------
Date: Thu, 22 Oct 2009 14:14:34 -0700
From: Jim Gibson <jimsgibson@gmail.com>
Subject: Re: 'use' command being executed
Message-Id: <221020091414342850%jimsgibson@gmail.com>
In article
<fda5dba4-0f89-47fb-9bd4-cb9dc43abd9c@r31g2000vbi.googlegroups.com>,
jhavero <jhavero@gmail.com> wrote:
> The 'use' command below tries to run when this is executed from Unix
> so I get an error that it cannot find the Win32::File module. If I
> comment out this 'use' line the script works in Unix and the 'print
> "Unix"' statement runs and the 'print "Windows"' statement never runs.
>
> How can I stop it from running the 'use' command when run from Unix.
> sub entry {
>
> if($^O eq 'MSWin32') {
> #Windows
> use Win32::File qw/ GetAttributes SetAttributes /;
> print "Windows\n";
> }
> } else {
> #unix
> print "Unix\n";
> }
>
> } #end sub entry
Replace the use line with the following (untested):
require Win32::File;
Win32::File->import( qw( GetAttributes SetAttributes ));
See 'perldoc -f use' and note that the above two lines are the
equivalent to the use line but without the BEGIN { } block. A BEGIN
forces unconditional execution of thestatements (and inclusion of the
module) at compile time.
--
Jim Gibson
------------------------------
Date: Thu, 22 Oct 2009 23:15:07 +0200
From: Andrzej Adam Filip <anfi@onet.eu>
Subject: Re: 'use' command being executed
Message-Id: <wltf6zary6-9AM@michael.brudna.chmurka.net>
jhavero <jhavero@gmail.com> wrote:
> The 'use' command below tries to run when this is executed from Unix
> so I get an error that it cannot find the Win32::File module. If I
> comment out this 'use' line the script works in Unix and the 'print
> "Unix"' statement runs and the 'print "Windows"' statement never runs.
>
> How can I stop it from running the 'use' command when run from Unix.
>
> # Main
> .
> .
> .
> my %FINDOPTIONS = (
> 'wanted' => \&entry,
> 'no_chdir' => 1
> );
> .
> .
> .
> find(\%FINDOPTIONS,@COMPONENTS);
>
> sub entry {
>
> if($^O eq 'MSWin32') {
> #Windows
> use Win32::File qw/ GetAttributes SetAttributes /;
> print "Windows\n";
> }
> } else {
> #unix
> print "Unix\n";
> }
>
> } #end sub entry
1) use "require" instead of "use"
2) use full names of functions with package prefix
Win32::File::GetAttributes
--
[pl>en Andrew] Andrzej Adam Filip : anfi@onet.eu : Andrzej.Filip@gmail.com
To believe your own thought, to believe that what is true for
you in your private heart is true for all men -- that is genius.
-- Ralph Waldo Emerson
------------------------------
Date: Fri, 23 Oct 2009 00:04:39 +0100
From: Ben Morrow <ben@morrow.me.uk>
Subject: Re: 'use' command being executed
Message-Id: <7157r6-1rb.ln1@osiris.mauzo.dyndns.org>
Quoth Jim Gibson <jimsgibson@gmail.com>:
>
> Replace the use line with the following (untested):
>
> require Win32::File;
> Win32::File->import( qw( GetAttributes SetAttributes ));
>
> See 'perldoc -f use' and note that the above two lines are the
> equivalent to the use line but without the BEGIN { } block. A BEGIN
> forces unconditional execution of thestatements (and inclusion of the
> module) at compile time.
No. It needs to be loaded at compile time, otherwise the ->import is
pointless. This will work
BEGIN {
if ($^O eq "MSWin32") {
require Win32::File;
Win32::File->import(qw/GetAttributes SetAttributes/);
}
}
as will
use if $^O eq "MSWin32",
"Win32::File" => qw/GetAttributes SetAttributes/;
which simply wraps up the above code for you.
Since Cygwin can also use Win32::File (and maybe one day Interix will be
able to too), you should probably use OSType or Devel::CheckOS rather
than checking $^O yourself. Alternatively, you can just load the module
and catch the error if it isn't there for some reason: this will also
handle perls that *are* running on Win32 but don't have Win32::File
installed. maybe.pm on CPAN will allow you to write this as
use maybe "Win32::File" => ...;
Ben
------------------------------
Date: Thu, 22 Oct 2009 21:40:17 -0700
From: J黵gen Exner <jurgenex@hotmail.com>
Subject: Re: 'use' command being executed
Message-Id: <9nc2e5pl3jeo5uv0r4k1d1bc7rie8h3756@4ax.com>
jhavero <jhavero@gmail.com> wrote:
>The 'use' command below tries to run when this is executed from Unix
>so I get an error that it cannot find the Win32::File module. If I
>comment out this 'use' line the script works in Unix and the 'print
>"Unix"' statement runs and the 'print "Windows"' statement never runs.
>
>How can I stop it from running the 'use' command when run from Unix.
You cannot because use() is executed at compile time, i.e. before any
if() is evaluated.
However you can step back to the components of use() (see 'perldoc -f
use') and use require() instead of use(). require() is executed at
runtime, thus you can prevent its execution in an if() statement.
jue
------------------------------
Date: Fri, 23 Oct 2009 06:58:24 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: Indirection in a hash
Message-Id: <86ws2mu2of.fsf@gareth.avalon.lan>
jt@toerring.de (Jens Thoms Toerring) writes:
> "multidimensional arrays" (I put that in parantheses since there
> aren't real multi- dimensional arrays in Perl but instead you use
> arrays of array references to emulate them).
(ITYM quotes, not parentheses).
What is the difference? Surely this is the most obvious way to implement
multidimensional arrays on a lower level?
And if Perl's "array of references" is functionally equivalent to a
multidimensional array (and it is), then isn't it a multidimensional
array?
Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Fri, 23 Oct 2009 01:57:47 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Indirection in a hash
Message-Id: <87ocnyabz8.fsf@quad.sysarch.com>
>>>>> "MvdW" == Mart van de Wege <mvdwege@mail.com> writes:
MvdW> jt@toerring.de (Jens Thoms Toerring) writes:
>> "multidimensional arrays" (I put that in parantheses since there
>> aren't real multi- dimensional arrays in Perl but instead you use
>> arrays of array references to emulate them).
MvdW> (ITYM quotes, not parentheses).
MvdW> What is the difference? Surely this is the most obvious way to implement
MvdW> multidimensional arrays on a lower level?
MvdW> And if Perl's "array of references" is functionally equivalent to a
MvdW> multidimensional array (and it is), then isn't it a multidimensional
MvdW> array?
by the ways classic compiled langs like c does them, then no, perl
doesn't support multidimensional arrays. in those langs you usually
predeclare all the array's dimensions and you can directly access
elements with a fast calculation of the correct offset given the
indices. in perl's version you have to loop over each dimension then
index to get the next level down. it is linear growth with the number of
dimensions so it is slower. the advantage of perl's design is that you
can mix and match arrays and hashes in such a data tree and create it
dynamically without knowing any dimensions in advance. this is worth the
extra cost in speed IMO as real world data is rarely fixed in dimensions
at compile time.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 22 Oct 2009 23:25:54 -0700
From: J黵gen Exner <jurgenex@hotmail.com>
Subject: Re: Indirection in a hash
Message-Id: <5si2e5hkprlnui1loe809i9k7t9peuin28@4ax.com>
Mart van de Wege <mvdwege@mail.com> wrote:
>jt@toerring.de (Jens Thoms Toerring) writes:
>
>> "multidimensional arrays" (I put that in parantheses since there
>> aren't real multi- dimensional arrays in Perl but instead you use
>> arrays of array references to emulate them).
>
>(ITYM quotes, not parentheses).
>
>What is the difference? Surely this is the most obvious way to implement
>multidimensional arrays on a lower level?
Actually no because individual elements cannot be accessed directly with
O(1) by pre-calculating the memory location. Instead in Perl the
low-level implementation has to follow the reference chain for each and
every access.
>And if Perl's "array of references" is functionally equivalent to a
>multidimensional array (and it is), then isn't it a multidimensional
>array?
That is debateable. Depending upon which level of abstraction you are
talking about a yes is as justifiable as a no.
jue
------------------------------
Date: Fri, 23 Oct 2009 08:28:34 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: Indirection in a hash
Message-Id: <86ocnytyi5.fsf@gareth.avalon.lan>
"Uri Guttman" <uri@StemSystems.com> writes:
>>>>>> "MvdW" == Mart van de Wege <mvdwege@mail.com> writes:
>
> MvdW> jt@toerring.de (Jens Thoms Toerring) writes:
> >> "multidimensional arrays" (I put that in parantheses since there
> >> aren't real multi- dimensional arrays in Perl but instead you use
> >> arrays of array references to emulate them).
>
> MvdW> (ITYM quotes, not parentheses).
>
> MvdW> What is the difference? Surely this is the most obvious way to implement
> MvdW> multidimensional arrays on a lower level?
>
> MvdW> And if Perl's "array of references" is functionally equivalent to a
> MvdW> multidimensional array (and it is), then isn't it a multidimensional
> MvdW> array?
>
> by the ways classic compiled langs like c does them, then no, perl
> doesn't support multidimensional arrays. in those langs you usually
> predeclare all the array's dimensions and you can directly access
> elements with a fast calculation of the correct offset given the
> indices. in perl's version you have to loop over each dimension then
> index to get the next level down.
Surely this is only true if the dimensions are of unknown size. If your data
model is such that you expect to have fixed size arrays, you can still
directly index into it, even with Perl's model.
I agree it is not common, but functionally multidimensional arrays are
possible to implement with Perl's "array of references" model.
Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Fri, 23 Oct 2009 08:35:08 +0200
From: Mart van de Wege <mvdwege@mail.com>
Subject: Re: Indirection in a hash
Message-Id: <86k4ymty77.fsf@gareth.avalon.lan>
J眉rgen Exner <jurgenex@hotmail.com> writes:
> Mart van de Wege <mvdwege@mail.com> wrote:
>>jt@toerring.de (Jens Thoms Toerring) writes:
>>
>>> "multidimensional arrays" (I put that in parantheses since there
>>> aren't real multi- dimensional arrays in Perl but instead you use
>>> arrays of array references to emulate them).
>>
>>(ITYM quotes, not parentheses).
>>
>>What is the difference? Surely this is the most obvious way to implement
>>multidimensional arrays on a lower level?
>
> Actually no because individual elements cannot be accessed directly with
> O(1) by pre-calculating the memory location.
Erm.
'Most obvious' != 'Most efficient'.
Certainly pre-calculating is faster, and can't be done in Perl because
the size of the dimensions is unknown, but that's a compiler issue. From
the point of view of the programmer it is quite possible to do
multidimensional arrays, even if the syntax is a bit awkward at times.
>
>>And if Perl's "array of references" is functionally equivalent to a
>>multidimensional array (and it is), then isn't it a multidimensional
>>array?
>
> That is debateable. Depending upon which level of abstraction you are
> talking about a yes is as justifiable as a no.
>
Obviously. And I agree that from the Perl implementer's view it is not
the same.
I am however not a Perl internals hacker. I am a sysadmin who does a bit
of Perl programming to automate tasks and to support business. For me,
if my data model consists of matrix-like structures, an array of
references to arrays looks just like a multidimensional array.
Mart
--
"We will need a longer wall when the revolution comes."
--- AJS, quoting an uncertain source.
------------------------------
Date: Fri, 23 Oct 2009 02:56:51 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Indirection in a hash
Message-Id: <87eioua98s.fsf@quad.sysarch.com>
>>>>> "MvdW" == Mart van de Wege <mvdwege@mail.com> writes:
MvdW> "Uri Guttman" <uri@StemSystems.com> writes:
>>>>>>> "MvdW" == Mart van de Wege <mvdwege@mail.com> writes:
>>
MvdW> jt@toerring.de (Jens Thoms Toerring) writes:
>> >> "multidimensional arrays" (I put that in parantheses since there
>> >> aren't real multi- dimensional arrays in Perl but instead you use
>> >> arrays of array references to emulate them).
>>
MvdW> (ITYM quotes, not parentheses).
>>
MvdW> What is the difference? Surely this is the most obvious way to implement
MvdW> multidimensional arrays on a lower level?
>>
MvdW> And if Perl's "array of references" is functionally equivalent to a
MvdW> multidimensional array (and it is), then isn't it a multidimensional
MvdW> array?
>>
>> by the ways classic compiled langs like c does them, then no, perl
>> doesn't support multidimensional arrays. in those langs you usually
>> predeclare all the array's dimensions and you can directly access
>> elements with a fast calculation of the correct offset given the
>> indices. in perl's version you have to loop over each dimension then
>> index to get the next level down.
MvdW> Surely this is only true if the dimensions are of unknown
MvdW> size. If your data model is such that you expect to have fixed
MvdW> size arrays, you can still directly index into it, even with
MvdW> Perl's model.
no you can't. with perl all array accesses require looping over each
dimension. there is no way to directly access a lower level. the code
looks like it does direct access but internally it does a loop over the
indices.
MvdW> I agree it is not common, but functionally multidimensional arrays are
MvdW> possible to implement with Perl's "array of references" model.
that isn't the issue. you can get something which looks and behaves like
a multidim array but it really isn't. this is why i always review code
that repeats deep access code and tell people to factor out the
duplicate code. in fortran and other langs, deep repeated accesses get
the common code optimized out of the loop by common subexpression
removal. perl can't do that due to its dynamic nature so the coder has
to do it. this is where perl's dynamic arrays show that their internal
design is different than fortran and c's.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
------------------------------
Date: Thu, 22 Oct 2009 19:43:27 -0400
From: monkeys paw <user@example.net>
Subject: Re: parse multi line string into hash
Message-Id: <ONidneBMIKy7c33XnZ2dnUVZ_ixi4p2d@insightbb.com>
sln@netherlands.com wrote:
> On Fri, 16 Oct 2009 14:05:18 -0400, monkeys paw <user@example.net> wrote:
>
>> I need to parse the following string into a hash. The string includes
>> the spaces shown also. How can i get $string into %hash. I can't split
>> on commas because some of the hash values have
>> commas (see states => CA,KY...). So how would i split on newlines
>> to do this is my question. I need hash to be
>>
>> $hash{disposition} = 1;
>> $hash{billkey} = 'CA:2009000:A:708';
>> etc..
>>
>>
>> my $string = 'disposition => 1,
>> billkey => CA:2009000:A:708,
>> max_bills => 1000,
>> states => CA,KY,FL,
>> print_pfdata => 1,
>> query_type => single,
>> range => 0,
>> active => 1,
>> #print_authors => 1,
>> remote_user => zamktph,
>> keys => [
>> {
>> id_type => bill,
>> id => CA2009000A708
>> }
>> ],
>> id_type => bill,
>> network_code => NON,
>> no_topics => 1,';
>
> How $string ever got into this shape is pure speculation.
> To reverse it is not a pleasure.
> -sln
>
> use strict;
> use warnings;
> use Data::Dumper;
>
> my $string = 'disposition => 1,
> billkey => CA:2009000:A:708,
> max_bills => 1000,
> states => CA,KY,FL,
> print_pfdata => 1,
> query_type => single,
> range => 0,
> active => 1,
> #print_authors => 1,
> remote_user => zamktph,
> keys => [
> {
> id_type => bill,
> id => CA2009000A708
> }
> ],
> id_type => bill,
> network_code => NON,
> test =>,
> no_topics => 1,';
>
> ## one pass ..
> ## $string =~ s/ => \s* ( (?:(?!,\s*(?:\n|$))[^\[\{\n]+?)* |) ,?\s*(?:\n|$) /=> '$1',\n/xg;
>
> ## or, two pass ..
> $string =~ s/ ( => \s* (?![\[\{\n\s])) /$1'/xg;
> $string =~ s/ ( (?![\[\]\{\},\n]).) (,|)(\n|$) /$1'$2\n/xg;
>
> print $string;
>
> my %hash = eval $string;
>
> print Dumper (\%hash);
>
> __END__
>
> disposition => '1',
> billkey => 'CA:2009000:A:708',
> max_bills => '1000',
> states => 'CA,KY,FL',
> print_pfdata => '1',
> query_type => 'single',
> range => '0',
> active => '1',
> #print_authors => '1',
> remote_user => 'zamktph',
> keys => [
> {
> id_type => 'bill',
> id => 'CA2009000A708'
> }
> ],
> id_type => 'bill',
> network_code => 'NON',
> test =>'',
> no_topics => '1',
> $VAR1 = {
> 'billkey' => 'CA:2009000:A:708',
> 'disposition' => '1',
> 'test' => '',
> 'max_bills' => '1000',
> 'query_type' => 'single',
> 'active' => '1',
> 'range' => '0',
> 'states' => 'CA,KY,FL',
> 'keys' => [
> {
> 'id_type' => 'bill',
> 'id' => 'CA2009000A708'
> }
> ],
> 'remote_user' => 'zamktph',
> 'id_type' => 'bill',
> 'print_pfdata' => '1',
> 'network_code' => 'NON',
> 'no_topics' => '1'
> };
>
>
This is the input you get from a textarea field in a a HTML form.
Someone pastes a perl hash into a textarea, the perl CGI has to
take the HTML textarea input like you see above, and turn it
back to a perl hash. It allows a lay user to copy/paste return
status from a program into an HTML box, receive information back.
------------------------------
Date: Thu, 22 Oct 2009 18:58:35 -0700
From: sln@netherlands.com
Subject: Re: Want to write a script to note specific IP addresses.
Message-Id: <tr12e5lcbfuj075js3ldb1epffpdslu1n1@4ax.com>
On Thu, 22 Oct 2009 16:13:15 +0800, Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>On Tue, 20 Oct 2009 13:20:18 -0500, "J. Gleixner"
><glex_no-spam@qwest-spam-no.invalid> wrote:
>
>>Quite a few modules that help with IPs, however
>>you could translate the IPs to integers and simply
>>check if one IP is between the start and end.
>>
>>Place to start:
>>
>>use Net::Netmask qw(quad2int);
>>
>>
>>To open/read/process files:
>>
>>perldoc -f open
>>perldoc perlopentut
>
>Firstly, thank you very much for your help.
>
>Here, I'll describe the solution used by me now and some issues when
>use this method:
>
>Currently, I use a complete binary IPdatabase named QQWry.Dat which
>include 373374 IP blocks oall over the world, you can download this
>IPdatabase from the following url:
>
>http://update.cz88.net/soft/qqwry.rar
>
>Then you can use the following perl script to read the IP addresses
>and the corresponding location informations to note the specific IP
>addresses:
>
>http://www.ieasy.org/download/qqwry.pl
>
>The above qqwry.pl has the following content:
>
<snip code>
>
>My issue is that: if the IPfile is a huge one, say including several
>thousands entries in it, the above process will time consuming. So
>how can I revise the above perl script with perl's multithread module
>to improve the efficiency?
>
>Thanks in advance.
I see what you are trying to do, the binary search.
You have a strange layout with a lot of offsets (seeking).
You may want to try a different data layout but you probably
already have a program that generates that.
What you should try is a memory file. This way you won't have
to alter your basic flow and structure.
I have a program below that just traverses the data structure
in a benchmark.
Good Luck!
-sln
========
Just use the example on your existing code, basically,
-------------
use Benchmark ':hireswallclock';
my ($t0,$t1);
my $ipfile = "qqwry/QQWry.Dat";
open FILE, "<$ipfile" or die "Can't open dat file: $!";
binmode (FILE);
my $BUFF = join '',<FILE>;
close FILE;
open my $memfile, '<', \$BUFF or die "Can't open memory file: $!";
binmode ($memfile);
$t0 = new Benchmark;
for (1 .. 1_000)
{
ipwhere ($memfile, '168.105.234.109'); # or pass a list
}
$t1 = new Benchmark;
my $tdif = timediff($t1, $t0);
print STDERR "the code took:",timestr($tdif),"\n";
close $memfile;
--------------
# in different file ..
sub ipwhere {
my $ipbegin,$ipend,$ipData1,$ipData2,$DataSeek,$ipFlag;
my $fh = shift;
my $ip = shift;
my @ip = split(/\./,$ip);
my $ipNum = $ip[0]*16777216+$ip[1]*65536+$ip[2]*256+$ip[3];
seek ($fh, 0, 0);
read ($fh, $ipbegin, 4);
read ($fh, $ipend, 4);
$ipbegin = unpack("L",$ipbegin);
$ipend = unpack("L",$ipend);
my $ipAllNum = ($ipend-$ipbegin)/7+1;
...
}
----------------
Example code and some strange chineese in the data:
use strict;
use warnings;
use Benchmark ':hireswallclock';
my ($t0,$t1);
my ($ipbegin,$ipend,$ipData1,$ipData2,$DataSeek,$ipFlag);
my $ipfile = "qqwry/QQWry.Dat";
open FILE, "<$ipfile" or die "Can't open dat file: $!";
binmode (FILE);
my $BUFF = join '',<FILE>;
close FILE;
open my $memfile, '<', \$BUFF or die "Can't open memory file: $!";
binmode ($memfile);
$t0 = new Benchmark;
for (1 .. 1_000)
{
seek ($memfile, 0, 0);
read ($memfile, $ipbegin, 4);
read ($memfile, $ipend, 4);
$ipbegin = unpack("L",$ipbegin);
$ipend = unpack("L",$ipend);
my $ipAllNum = ($ipend-$ipbegin)/7+1;
# print "Location $ipbegin - $ipend\n\n";
my $BeginNum=0;
my $EndNum=$ipAllNum;
for my $cnt (0 .. 200)
{
seek ($memfile, $ipbegin+7*$cnt, 0);
read ($memfile, $ipData1, 4);
my $ip1num = unpack("L",$ipData1);
my ($Quad, $ipAddr1, $ipAddr2) = (0,'','');
$Quad = sprintf ( "%d.%d.%d.%d",
($ip1num & 0xff000000) >> 24,
($ip1num & 0xff0000) >> 16,
($ip1num & 0xff00) >> 8,
($ip1num & 0xff));
# print +($ipbegin+7*$cnt), " : Data1 = $Quad, ";
read ($memfile, $DataSeek, 3);
$DataSeek = unpack("L",$DataSeek."\0");
# print "DataSeek = $DataSeek, ";
seek ($memfile, $DataSeek,0);
read ($memfile, $ipData2, 4);
my $ip2num=unpack("L",$ipData2);
$Quad = sprintf ( "%d.%d.%d.%d",
($ip2num & 0xff000000) >> 24,
($ip2num & 0xff0000) >> 16,
($ip2num & 0xff00) >> 8,
($ip2num & 0xff));
# print "Data2 = $Quad, ";
{
local $/="\0";
read ($memfile, $ipFlag, 1);
# printf "ipFlag = %d, ", ord $ipFlag;
if ($ipFlag eq "\1") {
my $ipSeek;
read ($memfile, $ipSeek, 3);
$ipSeek = unpack("L",$ipSeek."\0");
# print "ipSeek = $ipSeek, ";
seek($memfile, $ipSeek, 0);
read ($memfile, $ipFlag, 1);
# printf "ipFlag = %d, ", ord $ipFlag;
}
if ($ipFlag eq "\2") {
my $AddrSeek;
read($memfile, $AddrSeek, 3);
read($memfile, $ipFlag, 1);
# print "AddrSeek = ",unpack("L",$AddrSeek."\0"), ", " ;
# printf "ipFlag = %d, ", ord $ipFlag;
if($ipFlag eq "\2") {
my $AddrSeek2;
read ($memfile, $AddrSeek2, 3);
$AddrSeek2 = unpack("L",$AddrSeek2."\0");
seek ($memfile, $AddrSeek2, 0);
}
else {
seek ($memfile, -1, 1);
}
$ipAddr2 = <$memfile>;
$AddrSeek = unpack ("L",$AddrSeek."\0");
seek ($memfile, $AddrSeek, 0);
$ipAddr1 = <$memfile>;
}
else {
seek ($memfile, -1, 1);
$ipAddr1 = <$memfile>;
read ($memfile, $ipFlag, 1);
if($ipFlag eq "\2") {
my $AddrSeek2;
read($memfile,$AddrSeek2,3);
$AddrSeek2 = unpack("L",$AddrSeek2."\0");
seek($memfile,$AddrSeek2,0);
}
else {
seek($memfile,-1,1);
}
$ipAddr2 = <$memfile>;
}
# print "Addr1 = '$ipAddr1', Addr2 = '$ipAddr2'\n";
}
}
}
$t1 = new Benchmark;
my $tdif = timediff($t1, $t0);
print STDERR "the code took:",timestr($tdif),"\n";
close $memfile;
__DATA__
Weird data structure:
int beginRecordOffset_first; // seek begin first record
int beginRecordOffset_last; // seek begin last record total records = (ipEnd - ipBegin)/7 + 1
struct IpEndRecord { //
unsigned ipData1; // end ip (32 bit)
byte ipFlag1;
byte IpOrAddrSeek1[3]; // SEEK_SET offset to ipSeek or AddrSeek (24 bit)
byte ipFlag2;
byte IpOrAddrSeek2[3]; // SEEK_SET offset to ipSeek or AddrSeek (24 bit)
}
struct IpBeginRecord { // 7 bytes
unsigned ipData1; // beginning ip (32 bit)
byte DataSeek[3]; // SEEK_SET offset to IpEndRecord (24 bit)
}
And some translated data:
Location 5352649 - 7967317
5352649 : Data1 = 0.0.0.0, DataSeek = 8, Data2 = 0.255.255.255, ipFlag = 73, Addr1 = 'IANA ', Addr2 = ' CZ88.NET '
5352656 : Data1 = 1.0.0.0, DataSeek = 27, Data2 = 1.51.255.255, ipFlag = 1, ipSeek = 12, ipFlag = 73, Addr1 = 'IANA ', Addr2 = ' CZ88.NET '
5352663 : Data1 = 1.52.0.0, DataSeek = 35, Data2 = 1.52.255.255, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352670 : Data1 = 1.53.0.0, DataSeek = 48, Data2 = 1.153.255.255, ipFlag = 1, ipSeek = 12, ipFlag = 73, Addr1 = 'IANA ', Addr2 = ' CZ88.NET '
5352677 : Data1 = 1.154.0.0, DataSeek = 56, Data2 = 1.154.255.255, ipFlag = 1, ipSeek = 39, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352684 : Data1 = 1.155.0.0, DataSeek = 64, Data2 = 1.255.255.255, ipFlag = 1, ipSeek = 12, ipFlag = 73, Addr1 = 'IANA ', Addr2 = ' CZ88.NET '
5352691 : Data1 = 2.0.0.0, DataSeek = 72, Data2 = 2.255.255.255, ipFlag = 1, ipSeek = 12, ipFlag = 73, Addr1 = 'IANA ', Addr2 = ' CZ88.NET '
5352698 : Data1 = 3.0.0.0, DataSeek = 80, Data2 = 3.255.255.255, ipFlag = 2, AddrSeek = 39, ipFlag = 191, Addr1 = '美国 ', Addr2 = '康涅狄格州费尔菲尔德县费尔菲尔德镇通用电气公司 '
5352705 : Data1 = 4.0.0.0, DataSeek = 135, Data2 = 4.9.255.255, ipFlag = 1, ipSeek = 39, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352712 : Data1 = 4.10.0.0, DataSeek = 143, Data2 = 4.10.255.255, ipFlag = 2, AddrSeek = 39, ipFlag = 208, Addr1 = '美国 ', Addr2 = '新泽西州立大学 '
5352719 : Data1 = 4.11.0.0, DataSeek = 166, Data2 = 4.11.255.255, ipFlag = 2, AddrSeek = 39, ipFlag = 207, Addr1 = '美国 ', Addr2 = '夏威夷 '
5352726 : Data1 = 4.12.0.0, DataSeek = 181, Data2 = 4.19.77.255, ipFlag = 1, ipSeek = 39, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352733 : Data1 = 4.19.78.0, DataSeek = 189, Data2 = 4.19.78.255, ipFlag = 2, AddrSeek = 39, ipFlag = 206, Addr1 = '美国 ', Addr2 = '西南政法大学 '
5352740 : Data1 = 4.19.79.0, DataSeek = 210, Data2 = 4.19.79.63, ipFlag = 2, AddrSeek = 39, ipFlag = 65, Addr1 = '美国 ', Addr2 = 'Armed Forces Radio/Television '
5352747 : Data1 = 4.19.79.64, DataSeek = 248, Data2 = 4.21.160.7, ipFlag = 1, ipSeek = 39, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352754 : Data1 = 4.21.160.8, DataSeek = 256, Data2 = 4.21.160.15, ipFlag = 2, AddrSeek = 39, ipFlag = 194, Addr1 = '美国 ', Addr2 = '马萨诸塞州米德尔塞克斯县剑桥市麻省理工学院 '
5352761 : Data1 = 4.21.160.16, DataSeek = 307, Data2 = 4.21.176.255, ipFlag = 1, ipSeek = 39, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352768 : Data1 = 4.21.177.0, DataSeek = 315, Data2 = 4.21.177.255, ipFlag = 1, ipSeek = 193, ipFlag = 2, AddrSeek = 39, ipFlag = 206, Addr1 = '美国 ', Addr2 = '西南政法大学 '
5352775 : Data1 = 4.21.178.0, DataSeek = 323, Data2 = 4.36.124.127, ipFlag = 1, ipSeek = 39, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352782 : Data1 = 4.36.124.128, DataSeek = 331, Data2 = 4.36.124.255, ipFlag = 2, AddrSeek = 39, ipFlag = 84, Addr1 = '美国 ', Addr2 = 'Technical Resource Connections Inc '
5352789 : Data1 = 4.36.125.0, DataSeek = 374, Data2 = 4.36.127.255, ipFlag = 2, AddrSeek = 39, ipFlag = 204, Addr1 = '美国 ', Addr2 = '坦帕大学 '
5352796 : Data1 = 4.36.128.0, DataSeek = 391, Data2 = 4.37.215.255, ipFlag = 1, ipSeek = 39, ipFlag = 195, Addr1 = '美国 ', Addr2 = ' CZ88.NET '
5352803 : Data1 = 4.37.216.0, DataSeek = 399, Data2 = 4.37.219.255, ipFlag = 2, AddrSeek = 39, ipFlag = 181, Addr1 = '美国 ', Addr2 = '德克萨斯女子大学 '
------------------------------
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:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#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 V11 Issue 2647
***************************************