[22997] in Perl-Users-Digest
Perl-Users Digest, Issue: 5217 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jul 12 03:06:12 2003
Date: Sat, 12 Jul 2003 00:05:10 -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 Sat, 12 Jul 2003 Volume: 10 Number: 5217
Today's topics:
Carriage Return / Line Feed question <phignuton at nospamplease hotmail com>
Re: Carriage Return / Line Feed question (David Efflandt)
Re: Comparing 2 hashes of array refs (Jay Tilton)
Re: Comparing two files.. <asu1@c-o-r-n-e-l-l.edu>
converting word documents <zchababe@comcast.net>
Re: converting word documents <asu1@c-o-r-n-e-l-l.edu>
Extracting data from the Internet (Disco Stu)
Re: Extracting data from the Internet <palladium@spinn.net>
Re: Extracting data from the Internet (Tad McClellan)
flock() and W95 <noreply@gunnar.cc>
Re: flock() and W95 <tassilo.parseval@rwth-aachen.de>
Re: naming hash using a variable name. (Tad McClellan)
Need help with posting to CGI from javascript <stacye@optonline.net>
Re: Need help with posting to CGI from javascript <_sodamnmad_@_hotmail_._com_>
Re: Need help with posting to CGI from javascript <stacye@optonline.net>
p book john62@electronmail.com
Re: p book <asu1@c-o-r-n-e-l-l.edu>
Re: p book (Tad McClellan)
Re: Perl code question (Jay Tilton)
problem with DBI.pm and ORDER BY RAND() in mysql (Joe Blower)
Re: Simple: How to include file? <REMOVEsdnCAPS@comcast.net>
Re: String concatenation with .= <FH> (Luke Powell)
Re: Win32-OLE, excel, and empties. (Jay Tilton)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Fri, 11 Jul 2003 17:25:32 -0500
From: Chris Kolosiwsky <phignuton at nospamplease hotmail com>
Subject: Carriage Return / Line Feed question
Message-Id: <qrdugvg9u0igi0taku7lmt98q5feerqv3t@4ax.com>
<de-lurk>
Hello all,
Given the script listed below:
LINE: while (<>)
{
while (! m/.+?<endad>/){
if(m/<cat:(\d+)>/) {
$cat = $1;
}
if($cat =~ /^14/) {
if(m/(>(.+?)<endad>)/) {
print $cat . "\|" . $2 ."\n";
}
}
next LINE;
}
}
and the data format as:
<cat:nnnnn>
<some useless discarded text>
<logo:>TEXT that I want to keep<endad>
(each line is seperate)
and this is the expected output:
nnnnn|TEXT that I want to keep
Is there any reason that this script should function fine in files
that use a \x0d\x0a between lines instead of just a \x0d?
The script gives the expected output in the CR/LF scenario, but int he
CR case, I get nothing.
I'm exceptionally sorry if this is listed in the faq, but a perldoc -q
"carriage return" returned zip.
TIA
Chris
<re-lurk>
------------------------------
Date: Sat, 12 Jul 2003 00:06:58 +0000 (UTC)
From: efflandt@xnet.com (David Efflandt)
Subject: Re: Carriage Return / Line Feed question
Message-Id: <slrnbguk91.6se.efflandt@typhoon.xnet.com>
On Fri, 11 Jul 2003, Chris Kolosiwsky <phignutonatnospampleasehotmailcom> wrote:
><de-lurk>
> Hello all,
>
> Given the script listed below:
>
> LINE: while (<>)
> {
> while (! m/.+?<endad>/){
> if(m/<cat:(\d+)>/) {
> $cat = $1;
> }
> if($cat =~ /^14/) {
> if(m/(>(.+?)<endad>)/) {
> print $cat . "\|" . $2 ."\n";
>
> }
> }
> next LINE;
> }
> }
>
> and the data format as:
>
><cat:nnnnn>
><some useless discarded text>
><logo:>TEXT that I want to keep<endad>
>
> (each line is seperate)
>
> and this is the expected output:
>
> nnnnn|TEXT that I want to keep
>
>
> Is there any reason that this script should function fine in files
> that use a \x0d\x0a between lines instead of just a \x0d?
It depends what OS the script is running on. An OS that expects \x0d\0a
for line endings (DOS/Win) is not going to recognize just \x0d (old Mac)
as a line ending. An OS that uses \x0a for line endings would not
recognize \x0d as a line ending and may give unexpected results with
\x0d\x0a line endings.
So you should either convert data line endings to proper type for the OS
the script is running on or, set $/ to whatever you expect actual line
endings to be (see: perldoc perlvar).
> The script gives the expected output in the CR/LF scenario, but int he
> CR case, I get nothing.
Because no line endings were found and the data all ended up in one long
line, therefore, breaking your regex's.
> I'm exceptionally sorry if this is listed in the faq, but a perldoc -q
> "carriage return" returned zip.
>
> TIA
>
> Chris
>
><re-lurk>
--
David Efflandt - All spam ignored http://www.de-srv.com/
------------------------------
Date: Fri, 11 Jul 2003 22:56:22 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Comparing 2 hashes of array refs
Message-Id: <3f0f3d76.165090991@news.erols.com>
simoninusa2001@yahoo.co.uk (simo) wrote:
: I have a hash of task titles which holds an array of permissions
: needed to do those tasks:
:
: my %task = {'gardening' => [1,2,6], 'cleaning' => [3,2,5]}
:
: And then I have an array of permissions that the user has, which I put
: into a hash so that the data formats would be similar, although it
: might not be helping, basically the permissions array is [1,4] for
: this example:
:
: my %permissions = {'gardening' => [1,4], 'cleaning' => [1,4]}
:
: So I basically have to loop through each task, checking to see if I
: have permission to do it (if any of 1,4 are included then I have
: permission, so it's 1 OR 4, not 1 AND 4).
:
: So it's kinda like comparing each array reference in the task hash
: with each element in the permissions array, but I can't do it! :-(
Building a set of nested iterations just to find the intersection of
two arrays is for the birds.
Nested...birds...that's a joke, son!
See perlfaq4, "How do I compute the difference of two arrays? How do
I compute the intersection of two arrays?"
for( keys %task ) {
if( isect( $task{$_}, $permissions{$_} ) ) {
print "$_ can be performed\n"
}
else {
print "$_ cannot be performed\n"
}
}
sub isect {
my($a1, $a2) = @_;
my %seen = map( ($_, 1), @$a1 );
grep $seen{$_}, @$a2;
}
Or, just because the array values permit it,
for( keys %task ) {
my($t, $p) = (0,0);
vec($t, $_, 1) = 1 for @{$task{$_}};
vec($p, $_, 1) = 1 for @{$permissions{$_}};
if( $t & $p ) {
print "$_ can be performed\n"
}
else {
print "$_ cannot be performed\n"
}
}
: I also tried this earlier, but think it's worse than the first one, as
: it uses grep which the perldoc/faq says not to do!
Feh. The FAQ advises against using grep() in void context. You're
using it in a scalar (boolean) context. Anyway, discarding a puny
two-item list returned by grep() isn't going to hurt anything.
: So I'd be able to garden, but not clean.
That's good. Gardening while cleaning only makes the cleaning harder.
------------------------------
Date: 12 Jul 2003 01:37:54 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: Comparing two files..
Message-Id: <Xns93B5DC0DCB919asu1cornelledu@132.236.56.8>
xx087@freenet.carleton.ca (Glenn Jackman) wrote in
news:slrnbgubia.e98.xx087@freenet10.carleton.ca:
> A. Sinan Unur <asu1@c-o-r-n-e-l-l.edu> wrote:
>> if(exists $first{$_}) {
>> $first{$_} .= ",$.";
>> } else {
>> $first{$_} = $.;
>> }
>>
>> I originally intended to store an array of line numbers for each line
>> in the file, and tried various syntax combinations. Could someone
>> give me a hint as to how to do it?
>
> In place of the above code, I would write: push @{$first{$_}}, $.
> $first{$_} then contains a reference to an array of line numbers.
Great. Thank you very much.
Sinan.
--
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov
------------------------------
Date: Sat, 12 Jul 2003 02:14:43 GMT
From: "Zak Chababe" <zchababe@comcast.net>
Subject: converting word documents
Message-Id: <naKPa.38830$ye4.30120@sccrnsc01>
Hi,
We are in the process of converting thousands of MS word documents into
some sort of database files like sql or comma separated files. Is there
something out there with Perl that allows me to convert all these word
documents? Any help is appreciated.
/Zak
------------------------------
Date: 12 Jul 2003 04:04:44 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: converting word documents
Message-Id: <Xns93B6CEF2DA7asu1cornelledu@132.236.56.8>
"Zak Chababe" <zchababe@comcast.net> wrote in news:naKPa.38830$ye4.30120
@sccrnsc01:
> We are in the process of converting thousands of MS word documents into
> some sort of database files like sql or comma separated files. Is there
> something out there with Perl that allows me to convert all these word
> documents? Any help is appreciated.
If you are on a Windows system with Word installed, you could use
Win32::OLE to automate conversion of Word documents.
Sinan.
--
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov
------------------------------
Date: 11 Jul 2003 20:02:45 -0700
From: scsand@acxiom.com (Disco Stu)
Subject: Extracting data from the Internet
Message-Id: <2ef636b0.0307111902.6b812d72@posting.google.com>
I'm a newbie with Perl and can extract data from Web Pages, but need
guidance on how to use perl to extract files from websites that you
would normally use a browser to download/save to a location.
I've got several text browsers on my RH 9.0 server. Is their some way
with Perl I can pass in enough parameters to the website that I can
get it to download a file to my server.
thanks in advance
scott
------------------------------
Date: Fri, 11 Jul 2003 21:46:59 -0600
From: "Rodney" <palladium@spinn.net>
Subject: Re: Extracting data from the Internet
Message-Id: <vgvb7bo6ot8c64@corp.supernews.com>
"Disco Stu" <scsand@acxiom.com> wrote in message
news:2ef636b0.0307111902.6b812d72@posting.google.com...
> I'm a newbie with Perl and can extract data from Web Pages, but need
> guidance on how to use perl to extract files from websites that you
> would normally use a browser to download/save to a location.
>
> I've got several text browsers on my RH 9.0 server. Is their some way
> with Perl I can pass in enough parameters to the website that I can
> get it to download a file to my server.
>
> thanks in advance
>
> scott
Have you tried to research this yourself?
I'll give you a hint. You probably already have everything you need, and a
couple of lines of code will do what you want.
I would read the documentation that comes with perl normally before posting
these questions. Check CPAN for modules (search.cpan.org).
and buy a couple of Really good reference books (I prefer the O'Reilly
Books) The perl CD bookshelf is handy with a search cd with thousands of
pages of documentation and examples to peruse.
Exerpt Richly Stolen from Perl in a nutshell which is more elequent than
myself.
17.2.2 LWP::Simple
LWP::Simple provides an easy-to-use interface for creating a web client,
although it is only capable of performing basic retrieving functions. An
object constructor is not used for this class; it defines functions to
retrieve information from a specified URL and interpret the status codes
from the requests.
This module isn't named Simple for nothing. The following lines show how to
use it to get a web page and save it to a file:
use LWP::Simple;
$homepage = 'oreilly_com.html';
$status = getstore('http://www.oreilly.com/', $homepage);
print("hooray") if is_success($status);
------------------------------
Date: Sat, 12 Jul 2003 00:00:09 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Extracting data from the Internet
Message-Id: <slrnbgv5ep.7tv.tadmc@magna.augustmail.com>
Disco Stu <scsand@acxiom.com> wrote:
> how to use perl to extract files from websites that you
> would normally use a browser to download/save to a location.
See Perl FAQ part 9 (Networking):
How do I fetch an HTML file?
> Is their some way
> with Perl I can pass in enough parameters to the website that I can
> get it to download a file to my server.
See Perl FAQ part 9 (Networking):
How do I automate an HTML form submission?
You are expected to check the Perl FAQ *before* posting to
the Perl newsgroup.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 12 Jul 2003 05:27:14 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: flock() and W95
Message-Id: <benvsi$7a8sl$1@ID-184292.news.uni-berlin.de>
Since flock() results in a fatal error on Windows 95 and 98, while I'm
doing development on a W98 box, there are quite a few statements like
this:
flock FH, LOCK_EX unless $W95;
I'm thinking of making flock() generate a non-fatal error instead when
run on W95 and W98 by defining CORE::GLOBAL::flock:
BEGIN {
*CORE::GLOBAL::flock = sub(*$) {
if ($^O eq 'MSWin32') {
require Win32;
if (Win32::GetOSVersion() < 2) {
warn 'flock() unimplemented on this platform';
return 1;
}
}
my ($fh, $op) = @_;
CORE::flock $fh, $op;
};
}
For this to work, it seems as if you need to pass the filehandles with
typeglobs:
flock *FH, LOCK_EX;
Doing so works also when calling the built-in flock() function
directly, so with this solution I'm able to use code that works the
'normal' way as well. Even if this is commented on under the title
"Overriding" in the Camel book, the only thing I'm overriding is the
fatal errors on W95 and W98. The intention is that it shall work
normally on other platforms.
Would appreciate your comments. I'd like to do this, but I don't want
to jeopardize the functioning of flock() where it's implemented.
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
------------------------------
Date: 12 Jul 2003 05:22:27 GMT
From: "Tassilo v. Parseval" <tassilo.parseval@rwth-aachen.de>
Subject: Re: flock() and W95
Message-Id: <beo5uj$ogq$1@nets3.rz.RWTH-Aachen.DE>
Also sprach Gunnar Hjalmarsson:
> Since flock() results in a fatal error on Windows 95 and 98, while I'm
> doing development on a W98 box, there are quite a few statements like
> this:
>
> flock FH, LOCK_EX unless $W95;
>
> I'm thinking of making flock() generate a non-fatal error instead when
> run on W95 and W98 by defining CORE::GLOBAL::flock:
>
> BEGIN {
> *CORE::GLOBAL::flock = sub(*$) {
> if ($^O eq 'MSWin32') {
> require Win32;
> if (Win32::GetOSVersion() < 2) {
> warn 'flock() unimplemented on this platform';
> return 1;
> }
> }
> my ($fh, $op) = @_;
> CORE::flock $fh, $op;
> };
> }
This looks like a lot of work when you could simply do:
eval { flock FH, LOCK_EX or warn $! };
if ($@) {
# failed
}
>
> For this to work, it seems as if you need to pass the filehandles with
> typeglobs:
>
> flock *FH, LOCK_EX;
>
> Doing so works also when calling the built-in flock() function
> directly, so with this solution I'm able to use code that works the
> 'normal' way as well. Even if this is commented on under the title
> "Overriding" in the Camel book, the only thing I'm overriding is the
> fatal errors on W95 and W98. The intention is that it shall work
> normally on other platforms.
There's a different approach that lets you use the bareword as argument.
But it's still not quite like the inbuilt flock() since you have to drop
the comma:
sub IO::Handle::safeflock {
my $res = eval { flock $_[0], $_[1] };
return 1 if $@;
return $res;
}
open F, "<", "file" or die $!;
safeflock F LOCK_EX or die $!;
It uses the indirect method invocation just like print() does.
Tassilo
--
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
------------------------------
Date: Fri, 11 Jul 2003 23:37:03 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: naming hash using a variable name.
Message-Id: <slrnbgv43f.7rl.tadmc@magna.augustmail.com>
Garry Short <g4rry_short@zw4llet.com> wrote:
> That Data::Dumper could be handy - I'll have to
> remember that one!
That is why it is mentioned in the Perl FAQ. :-)
You should (eventually) read each and every FAQ, there is bound
to be lots of similarly time-saving thingies that you are as yet
unaware of.
The FAQ is ineffective when you do not read it. :-) :-)
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Sat, 12 Jul 2003 01:15:06 GMT
From: "Stacey" <stacye@optonline.net>
Subject: Need help with posting to CGI from javascript
Message-Id: <uiJPa.21724$hY1.6513370@news4.srv.hcvlny.cv.net>
Here is a function that is declared in my html page. It is part of a series
of javascript that allows the user to draw a rectangle on a graphic. This
is called when the box is done, so I would like to send the screen coords
which I know already to a cgi script at this point below. Can anybody help
me?
Somebody mentioned this code, but I am not sure how exactly to go about and
where to put code etc etc. It uses the urlconnection. Not sure of syntax.
Thanks again.
http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
function dragDone(x,y) {
startlayer.moveTo(iX,iY)
alert("Box is done")
need to post to cgi here
}
------------------------------
Date: Sat, 12 Jul 2003 01:33:37 GMT
From: "Miguel De Anda" <_sodamnmad_@_hotmail_._com_>
Subject: Re: Need help with posting to CGI from javascript
Message-Id: <RzJPa.1084$jG1.110900543@newssvr13.news.prodigy.com>
"Stacey" <stacye@optonline.net> wrote in message
news:uiJPa.21724$hY1.6513370@news4.srv.hcvlny.cv.net...
> Here is a function that is declared in my html page. It is part of a
series
> of javascript that allows the user to draw a rectangle on a graphic. This
> is called when the box is done, so I would like to send the screen coords
> which I know already to a cgi script at this point below. Can anybody
help
> me?
>
> Somebody mentioned this code, but I am not sure how exactly to go about
and
> where to put code etc etc. It uses the urlconnection. Not sure of
syntax.
> Thanks again.
> http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
>
>
>
> function dragDone(x,y) {
> startlayer.moveTo(iX,iY)
> alert("Box is done")
>
> need to post to cgi here
>
> }
>
>
>
function dragDone(x,y) {
startlayer.moveTo(iX,iY);
alert("Box is done");
if ("java"=="javscript") {
alert("Ask question in comp.lang.java.help");
} else {
alert("Ask question in comp.lang.javascript");
}
}
But if you really want to know, this is what I like to do:
I create a hidden frame, be it an iframe or from a frameset. Have a form in
there that will receive data from the dragDone(x,y) function. Submit the
form to you cgi. This will prevent the main window from submiting the data
(and prevent the user from doing more things). From what I'm guessing, you
may need some way to buffer the input and send larger packets of input
together.
------------------------------
Date: Sat, 12 Jul 2003 06:13:26 GMT
From: "Stacey" <stacye@optonline.net>
Subject: Re: Need help with posting to CGI from javascript
Message-Id: <aGNPa.23618$hY1.7299696@news4.srv.hcvlny.cv.net>
Thanks for your help. For your last suggestion, are there any examples of
such on the net? I am a bit of a newbie and would not know how to go about
it.
Thanks again
"Miguel De Anda" <_sodamnmad_@_hotmail_._com_> wrote in message
news:RzJPa.1084$jG1.110900543@newssvr13.news.prodigy.com...
>
> "Stacey" <stacye@optonline.net> wrote in message
> news:uiJPa.21724$hY1.6513370@news4.srv.hcvlny.cv.net...
> > Here is a function that is declared in my html page. It is part of a
> series
> > of javascript that allows the user to draw a rectangle on a graphic.
This
> > is called when the box is done, so I would like to send the screen
coords
> > which I know already to a cgi script at this point below. Can anybody
> help
> > me?
> >
> > Somebody mentioned this code, but I am not sure how exactly to go about
> and
> > where to put code etc etc. It uses the urlconnection. Not sure of
> syntax.
> > Thanks again.
> > http://www.javaworld.com/javaworld/javatips/jw-javatip34.html
> >
> >
> >
> > function dragDone(x,y) {
> > startlayer.moveTo(iX,iY)
> > alert("Box is done")
> >
> > need to post to cgi here
> >
> > }
> >
> >
> >
>
> function dragDone(x,y) {
> startlayer.moveTo(iX,iY);
> alert("Box is done");
>
> if ("java"=="javscript") {
> alert("Ask question in comp.lang.java.help");
> } else {
> alert("Ask question in comp.lang.javascript");
> }
> }
>
>
>
>
>
>
>
>
> But if you really want to know, this is what I like to do:
>
> I create a hidden frame, be it an iframe or from a frameset. Have a form
in
> there that will receive data from the dragDone(x,y) function. Submit the
> form to you cgi. This will prevent the main window from submiting the data
> (and prevent the user from doing more things). From what I'm guessing, you
> may need some way to buffer the input and send larger packets of input
> together.
>
>
------------------------------
Date: 11 Jul 2003 23:03:01 -0500
From: john62@electronmail.com
Subject: p book
Message-Id: <3f0f8875$1_2@127.0.0.1>
ok, i am going to get a perl book, but i don't know which
one is good. i've been dabbling in perl a bit, but i'm
still pretty bad at it. any recommendations for a
beginner book?
thx.
--------------------------------------------------------------------
For free web access to newsgroups, please visit
http://www.coldmail.us/. Thanks
--------------------------------------------------------------------
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
------------------------------
Date: 12 Jul 2003 04:06:08 GMT
From: "A. Sinan Unur" <asu1@c-o-r-n-e-l-l.edu>
Subject: Re: p book
Message-Id: <Xns93B610B4D60Dasu1cornelledu@132.236.56.8>
john62@electronmail.com wrote in news:3f0f8875$1_2@127.0.0.1:
> ok, i am going to get a perl book, but i don't know which
> one is good. i've been dabbling in perl a bit, but i'm
> still pretty bad at it. any recommendations for a
> beginner book?
perldoc perlbook
and
http://learn.perl.org/
--
A. Sinan Unur
asu1@c-o-r-n-e-l-l.edu
Remove dashes for address
Spam bait: mailto:uce@ftc.gov
------------------------------
Date: Sat, 12 Jul 2003 00:03:39 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: p book
Message-Id: <slrnbgv5lb.7tv.tadmc@magna.augustmail.com>
A. Sinan Unur <asu1@c-o-r-n-e-l-l.edu> wrote:
> john62@electronmail.com wrote in news:3f0f8875$1_2@127.0.0.1:
>> any recommendations for a
>> beginner book?
>
> perldoc perlbook
The Camel book is _not_ a good book for a beginner, get
a "tutorial" type of book first.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Fri, 11 Jul 2003 23:41:13 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Perl code question
Message-Id: <3f0f4159.166085362@news.erols.com>
Matt Oliverius <moliverius@w-link.net> wrote:
: I'm trying to do something which I believe is quite complex, and I'm hoping
: someone here might not find it so complex. The problem involves
: permutations. Here is the structure of my input file
[snip sample data]
: Basically, you can think of the second column as a compartment. There are
: 5 compartments total.
Are 'A' and 'F' in the compartment number field significant? Looks
like they simply mark "aft" and "fore" halves of a compartment.
: Sometimes one or more of these compartments can be
: used to store a half size box, so I basically want to permutate every
: possible combination.
Sounds like we're heading into a variation on the knapsack problem.
The number of permutations could rapidly reach absurdity. How many
compartments are there?
: In the
: above example, there are four possibilities since 613 has either full, or
: half and 614 has either full or half and 615-617 only have full.
Check.
: I need to
: print each permutation along with all the data in columns 0-5 in an ascii
: file. Does anyone have any easy way of doing this?
How about reducing the apparent complexity a little. Is there a
specific portion of the task that is giving trouble?
As a coarse draft of what you might want,
#!perl
use warnings;
use strict;
my %hash;
while(<DATA>) {
my($bucket, $capacity) = (split)[1, 2];
{
# Assuming buckets labelled like '613A' or '613F'
# should be included with 613.
no warnings 'numeric';
$bucket += 0;
}
push @{ $hash{$bucket}{$capacity} }, $_;
}
my @buckets = sort keys %hash;
# Iterate over all possible half/full combinations.
PERM:
for( 0 .. (1<<@buckets)-1) {
# Generate the set of half/full capacities for the
# buckets--convert $_ to binary, then
# convert each 0/1 bit to half/full.
my %caps;
@caps{@buckets} =
('Half', 'Full')
[sprintf('%0*b', scalar @buckets, $_) =~ /[01]/g];
# Verify that each bucket's half/full capacity is
# permitted by the data.
defined $hash{$_}{ $caps{$_} } or next PERM
for @buckets;
# Output
print join ' ', map("$_:$caps{$_}", @buckets), "\n";
print @{ $hash{$_}{ $caps{$_} } } for @buckets;
print "\n\n";
}
__DATA__
A5 613 Full 2100.00 53.00 200.00
A5 613 Full 2110.00 56.00 200.50
A5 613 Full 2113.00 58.00 201.00
A5 613 Full 2114.00 60.00 202.00
[The rest of the data go here.]
------------------------------
Date: Sat, 12 Jul 2003 05:03:00 GMT
From: motiv8xREMOVETHIS@top25web.com (Joe Blower)
Subject: problem with DBI.pm and ORDER BY RAND() in mysql
Message-Id: <ammdnSGv5q0XC5KiXTWJgQ@comcast.com>
When I execute the following SQL statement at the mysql> prompt, I get a
random result:
SELECT proxy FROM proxies WHERE active = 1 ORDER BY RAND() LIMIT 1;
When I wrap it in my perl code, I always get the first row in the database.
my $dbh = DBI->connect("dbi:mysql:$db:host=$db_host", $db_user, $db_pass);
my $sql = "SELECT proxy FROM proxies WHERE active = 1 ORDER BY RAND() LIMIT
1";
my $sth = $dbh->prepare($sql) or die $dbh->errstr;
$sth->execute() or die $dbh->errstr;
while (my $row = $sth->fetch())
{
$proxy = $row->[0];
}
Motiv8x
Top25Web.com: Ranking Report & Search Engine Forums
http://www.top25web.com
------------------------------
Date: Fri, 11 Jul 2003 18:22:33 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Simple: How to include file?
Message-Id: <Xns93B5C52AA5B3Bsdn.comcast@206.127.4.25>
-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1
Tvrtko Ursulin <tvrtko@croadria.com> wrote in
news:20030711132522.1D889943.NOFFLE@orion.local:
>
> Hello everyone!
>
> I am new to perl (from C) and cannot figure how to include a simple
> custom file in the main program?
>
> I need something like:
>
> our $default_value = 'xyz';
>
> in my include.ph, and a way to include this in main.pl so that
> $default_value can normally be used. Can it be done?
I just wrote a module a few weeks ago to do this very thing. See
Config::Vars on CPAN. You would use it more or less like this:
# In file "My_vars.pm":
use strict;
package My_vars;
use Config::Vars;
var $default_value = 'xyz';
# File My_vars.pm ends
# In some other file:
use strict;
use My_vars qw($default_value);
print $default_value;
# other file ends
- --
Eric
$_ = reverse sort qw p ekca lre Js reh ts
p, $/.r, map $_.$", qw e p h tona e; print
-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>
iQA/AwUBPw9GzmPeouIeTNHoEQKPJQCfTLS3vNxlhZRuKK1q+1O8Qlshc5sAoOLO
sPi5oTJMy8/N2EYAwnlQYbCh
=Dj98
-----END PGP SIGNATURE-----
------------------------------
Date: 11 Jul 2003 16:14:43 -0700
From: powell_luke@hotmail.com (Luke Powell)
Subject: Re: String concatenation with .= <FH>
Message-Id: <e7ccbaac.0307111514.1e0a8253@posting.google.com>
rook_5150@yahoo.com (Bryan Castillo) wrote in message news:<1bff1830.0307082117.49fd08a1@posting.google.com>...
> powell_luke@hotmail.com (Luke Powell) wrote in message news:<e7ccbaac.0307081353.527d24e5@posting.google.com>...
> > I was writing a program to parse some data and I found something odd.
> > I'm using the ActiveState 5.8.0 for Windows (build 804) version of
> > Perl, and I found that when I did this:
> >
> > $record .= <FH>; #replaces rather than appends to $record
> >
>
> Could you post a very small script that shows this?
> I couldn't reproduce what you describe above. (although Im using 5.6.1)
> Here is what I tried:
>
> use strict;
> use warnings;
> open(F,"<$0") || die $!;
> my $record = "first line\n";
> $record .= <F>;
> print "record = ",$record,"\n";
>
> Its output was:
> record = first line
> use strict;
>
Sure Brian... This actually made me investigate the problem a little
more closely, so I can be a little more specific about the cause. It
appears to only be a problem when I use $/ = \4 to make it a fixed
record length mode. Here's the script:
use strict;
use warnings;
open(FH,"temp.txt");
$/=\5
my $string="This ";
$string.=<FH>;
print $string;
temp.txt contains:
works
This outputs:
works
Thanks for your replying :).
Luke
------------------------------
Date: Fri, 11 Jul 2003 22:43:01 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Win32-OLE, excel, and empties.
Message-Id: <3f0f3d73.165087376@news.erols.com>
"Richard S Beckett" <spikey-wan@bigfoot.com> wrote:
: I'm trying to find the first empty row in a spreadsheet.
: If I use this:
:
: my $EmptyRow = $sheet->UsedRange->Find({What=>"*",
: SearchDirection=>xlPrevious,
: SearchOrder=>xlByRows})->{Row};
: my $err = Win32::OLE::LastError();
: unless ($err eq 0) {$EmptyRow = 0;}
: $EmptyRow ++;
:
: This also works, unless the spreadsheet is empty, where I get the following
: error:
:
: Can't use an undefined value as a HASH reference at D:\Script.pl line 43,
: <FILE> line 167.
Just make sure the Find() method returns a defined value before trying
to use it as a hash reference.
my $search = $sheet->UsedRange
->Find({
What=>"*",
SearchDirection=>xlPrevious,
SearchOrder=>xlByRows
});
my $EmptyRow = 1 + (defined $search && $search->{Row});
------------------------------
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 5217
***************************************