[28209] in Perl-Users-Digest
Perl-Users Digest, Issue: 9573 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Aug 7 21:05:49 2006
Date: Mon, 7 Aug 2006 18:05:04 -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 Mon, 7 Aug 2006 Volume: 10 Number: 9573
Today's topics:
Re: B::Lint does not detect undefined subs xhoster@gmail.com
Re: counting number of uniques in a multidimensional ar <jack_posemsky@yahoo.com>
geometry problem <carlston88@gmail.com>
Re: geometry problem usenet@DavidFilmer.com
Re: geometry problem <carlston88@gmail.com>
Re: geometry problem <john@castleamber.com>
HOA redundancy in array. <hpbenton@scripps.edu>
Perl question sibijohn@gmail.com
Re: Perl question usenet@DavidFilmer.com
Re: Perl question <john@castleamber.com>
Re: Reading HTTP response body that is gzip'd *and* in <jsm@jmarshall.com>
Re: Simple file list in directory to array <rvtol+news@isolution.nl>
Re: Simple file list in directory to array usenet@DavidFilmer.com
Re: Simple file list in directory to array <1usa@llenroc.ude.invalid>
Re: Test::Harness and test values. <yusufm@gmail.com>
Re: Test::Harness and test values. <yusufm@gmail.com>
Re: Zero'ing out an array <abigail@abigail.be>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 07 Aug 2006 22:21:22 GMT
From: xhoster@gmail.com
Subject: Re: B::Lint does not detect undefined subs
Message-Id: <20060807183025.061$a1@newsreader.com>
Alexander Frink <Alexander.Frink@Uni-Mainz.DE> wrote:
>
> And still the question: can I force Perl in unthreaded mode (because I
> have to use the packaged, precompiled Perl on the target platform)?
No. C preprocessor/compiler directives like #ifdef USE_ITHREADS are
sprinkled throughout the perl source code. There is no runtime switch that
can circumvent the effects that these have.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: 7 Aug 2006 17:53:06 -0700
From: "Jack" <jack_posemsky@yahoo.com>
Subject: Re: counting number of uniques in a multidimensional array column
Message-Id: <1154998386.188437.130780@n13g2000cwa.googlegroups.com>
Ted Zlatanov wrote:
> On 25 Jul 2006, jack_posemsky@yahoo.com wrote:
>
> > Forgive me if I am limited to some degree. I am just asking if someone
> > can provide some sample code that works takes $multidimarray[1][0],
> > $multidimarray[2][0], (a column) and produces a distinct count...
> >
> > I dont know how to take your suggestion of
> > map { $_->[0] } @columnarray
> > and convert that into a solution for that counts the distinct entires
> > for the first column in a multidimensional array ..
>
> I'll try to help you. Keep in mind that the advice Paul gave was
> useful, I'm just restating it and elaborating. Don't feel bad about
> missing things here and there, everyone has to start somewhere.
>
> That map call will return the first (0) column of the array as a list.
>
> Your original question was how to find unique elements in a column.
>
> You posted:
>
> > sort @$columnarray;
> > @out = grep($_ ne $prev && ($prev = $_, 1), @$columnarray);
> > if ($#out == -1) { $#out = 0; }
> > $out = $#out +1; # makes $#out of 0 = 1 so it gets counted !
> > push @distinctcounts, $out;
>
> The first line does nothing at all. Paul mentioned that too. Use
> warnings and strict mode, if possible, to avoid such code. Sort
> *returns* the sorted list, it doesn't modify in place.
>
> In addition your 'uniques' code is not very good. It may work in some
> cases, but really you should use a hash. Look at 'perldoc -q
> duplicates' and 'perldoc perldata' to get started. Actually all of
> the perldoc info is good :)
>
> Here's a (very simple) function to give you the unique items from a
> list you pass:
>
> sub uniques
> {
> my %unique = ();
> $unique{$_}++ foreach @_;
> return keys %unique;
> }
>
> Now use it like this:
>
> my @columnarray = ( [1,2,3], [1,2,3], [4,5,6], [7,8,9], );
>
> foreach my $column (1 .. scalar @{$columnarray[0]})
> {
> print "Unique elements in column $column: ";
> print join ', ',
> uniques(map { $_->[$column-1] }
> @columnarray
> );
> print "\n";
> }
>
> I formatted this to be easy to understand, and I tested it with the
> data above under
>
> use warnings;
> use strict;
>
> and it worked correctly. Please learn from the code posted above - it
> shows many useful techniques.
>
> Ted
Ted - this is excellent stuff - how exactly can I capture an example of
2 elements representing a duplicate in a variable from this code ???
thanks again,
Jack
------------------------------
Date: 7 Aug 2006 17:22:15 -0700
From: "IcyMint" <carlston88@gmail.com>
Subject: geometry problem
Message-Id: <1154996535.080057.73700@b28g2000cwb.googlegroups.com>
Hi, I'm trying to write a program that's able to place boxes into a
room. Basically it's an empty rectangular room with different sizes of
boxes in it. The program will need to figure out where to place the new
boxes. I'm thinking of marking out the place taken by the boxes in the
room then compare if the coordinate of the new box overlaps with them.
But now, the problem is, I don't know how to start. Can you please help
me? Thanks a lot!
------------------------------
Date: 7 Aug 2006 17:27:07 -0700
From: usenet@DavidFilmer.com
Subject: Re: geometry problem
Message-Id: <1154996827.259793.261670@m73g2000cwd.googlegroups.com>
IcyMint wrote:
> But now, the problem is, I don't know how to start.
I'll be happy to tell you where to start. Start here:
http://www.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: 7 Aug 2006 17:58:12 -0700
From: "IcyMint" <carlston88@gmail.com>
Subject: Re: geometry problem
Message-Id: <1154998692.781512.241220@b28g2000cwb.googlegroups.com>
Thanks a lot!
------------------------------
Date: 8 Aug 2006 00:58:46 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: geometry problem
Message-Id: <Xns9818CB3CAA8D3castleamber@130.133.1.4>
"IcyMint" <carlston88@gmail.com> wrote:
> Hi, I'm trying to write a program that's able to place boxes into a
> room. Basically it's an empty rectangular room with different sizes of
> boxes in it. The program will need to figure out where to place the new
> boxes.
If you want to do it near optimal, it sounds like 2D bin packing:
http://en.wikipedia.org/wiki/Bin_packing
http://en.wikipedia.org/wiki/Packing_problem
> I'm thinking of marking out the place taken by the boxes in the
> room then compare if the coordinate of the new box overlaps with them.
So you want to test if one rectangle overlaps another..
Google will give you plenty of solutions to that geometric problem.
--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
------------------------------
Date: 7 Aug 2006 17:41:51 -0700
From: "PB0711" <hpbenton@scripps.edu>
Subject: HOA redundancy in array.
Message-Id: <1154997711.130254.175640@h48g2000cwc.googlegroups.com>
Hello,
So I have an Hash of Arrays and I would like to remove all of the
redundancy from the array.
So I did this
"for my $i (0 .. $#{HOA_protein{$pro_name}}){
if ($HOA_protein{$pro_name}[$i] eq
$HOA_protein{$pro_name}[$i+1]){
$HOA_protein{$pro_name}[$i] = undef;
}
}"
Please excuse any grammer errors or misspellings I rewrote this on
windows from Linux, network problems today :(
I was wondering if anyone knows of a CPAN module that I can wave a
magic wand with and get ride of it.
It could be that the piece's in the array are not the same, only by eye
do they look the same. But the HOA was generated by RE's so.....
thanks for any help.
PB
------------------------------
Date: 7 Aug 2006 15:49:22 -0700
From: sibijohn@gmail.com
Subject: Perl question
Message-Id: <1154990962.749307.264170@b28g2000cwb.googlegroups.com>
I have a file name result01182006.xml which is named according to the
date. I know how to split it to get the values
$a = "result01182006.xml";
$a =~ /^(result)(\d{2})(\d{2})(\d{4})/;
print "$1 $2 $3 $4\n";
In the contents of this file i I need to replace every instance of
1/7/2006 to 01/18/2006.
Change the date of "Sat 07 Jan 2006" to corresponding day which
01/18/2006 falls on, so it should be changed to "Wed 18 Jan 2006
and change the string result01072006 to result01182006
Please can somebody help here.. Working on no sleep whatsover in 24
hrs.
Sibi
------------------------------
Date: 7 Aug 2006 16:12:55 -0700
From: usenet@DavidFilmer.com
Subject: Re: Perl question
Message-Id: <1154992375.881929.308520@p79g2000cwp.googlegroups.com>
sibijohn@gmail.com wrote:
> a multiposted question
http://tinyurl.com/ryucn
http://tinyurl.com/ohy3g
http://tinyurl.com/q234q
Multiposting is universally considered rude in usenet. Folks are
unlikely to help you if you do not abide by basic rules of usenet
courtesy.
--
David Filmer (http://DavidFilmer.com)
------------------------------
Date: 8 Aug 2006 00:52:25 GMT
From: John Bokma <john@castleamber.com>
Subject: Re: Perl question
Message-Id: <Xns9818CA2919CC0castleamber@130.133.1.4>
sibijohn@gmail.com wrote:
> I have a file name result01182006.xml which is named according to the
> date. I know how to split it to get the values
>
> $a = "result01182006.xml";
don't use $a, use a meaningfull name (e.g. $filename)
Also make sure you have:
use strict;
use warnings;
near the top of your script-
> $a =~ /^(result)(\d{2})(\d{2})(\d{4})/;
> print "$1 $2 $3 $4\n";
>
> In the contents of this file i I need to replace every instance of
> 1/7/2006 to 01/18/2006.
>
> Change the date of "Sat 07 Jan 2006" to corresponding day which
> 01/18/2006 falls on, so it should be changed to "Wed 18 Jan 2006
The best thing to do this is to parse the date(s) to an internal format,
for example the one that time uses, and add the right amount of hours,
convert back to the desired format (see Time::Local for example). That
way you don't have to worry about things like Sat and Wed.
OTOH, it it are a restricted number of conversions, like replacing 7
with 18 iff date = 1/7/2006 etc. you might want to do just some s///,
e.g.
for ( $line ) {
s{1/7/2006}{01/18/2006}g;
s{Sat 07 Jan 2006}{Wed 18 Jan 2006}g;
}
> and change the string result01072006 to result01182006
>
> Please can somebody help here.. Working on no sleep whatsover in 24
> hrs.
Then get some sleep. I never understand people who think that
programming just a few more hours is really productive. My experience is
that the next time you spend twice the amount of hours you worked extra
the previous day with cleaning up the garbage.
What you want to do could be done with a text editor like TextPad
(search & replace) in a few steps. If this kept you up for 24 hrs, you
probably should find different work.
Programming requires concentration, and there is a limitation/day. It's
certainly not 18 hrs on coke (the drink) and pizza's combined with
sleeping on the office floor, how "romantic" that might sound.
--
John Bokma Freelance software developer
&
Experienced Perl programmer: http://castleamber.com/
------------------------------
Date: Mon, 7 Aug 2006 15:01:57 -0700
From: James Marshall <jsm@jmarshall.com>
Subject: Re: Reading HTTP response body that is gzip'd *and* in UTF-8
Message-Id: <20060807145013.R79663@jmarshall.com>
Thanks, Ben! I was able to get it working with utf8::encode() and
utf8::decode(). I appreciate the cleanliness of your other solutions, but
at this time they won't quite fit the situation (e.g. I need to know the
Content-Length: I'm sending out in an HTTP response.) I'm also a little
concerned that some of these features (utf8::decode(), ":pop") are
documented as experimental, but hopefully they've been around long enough
by now to lose that condition. I'd love to have this program work in Perl
5.6.1 (since its users may have zero control over how their server is
configured), but that may be asking too much.
The reason I'm not using PerlIO::gzip is mostly just ignorance. The
0.17 version number is kinda low, but maybe I shouldn't worry about that.
So thank you for your helpful and educational post, it clarified some
things.
Cheers,
James
............................................................................
James Marshall james@jmarshall.com Berkeley, CA @}-'-,--
"Teach people what you know."
............................................................................
On Sat, 5 Aug 2006, Ben Morrow wrote:
BM>
BM> Quoth james@jmarshall.com:
BM> > I'm writing an HTTP client that handles gzip'd content as well as UTF-8
BM> > text, including when a response body is both gzip'd and in UTF-8.
BM> >
BM> > I'm newish to both compression and PerlIO layers, so I'd like a second
BM> > opinion from someone who knows them better than I do. Does the code below
BM> > look correct? The goal is to end up with the uncompressed body in $body,
BM> > and interpreted as UTF-8 if identified as such by "charset".
BM> >
BM> > I appreciate not wanting to use utf8::upgrade() ; is there a better way to
BM> > handle it in this case, or is this one of those cases where it's
BM> > legitimately needed?
BM>
BM> It's never (IMHO) legitimately needed. The only possible case is where
BM> some XS code has messed something up. utf8::upgrade doesn't change
BM> anything that's visible at the Perl level. All it does is change how
BM> perl represents the data internally, but you don't care about that.
BM>
BM> > Finally, does anyone know if Compress::Zlib::memGzip() handles UTF-8 input
BM> > correctly, or do I need to "utf8::downgrade($body)" before compressing it?
BM>
BM> I don't know what Compress::Zlib does aobut it, but it's pretty
BM> meaningless to apply gzip to a stream of characters. It's not defined on
BM> characters, it's defined on bytes; so you need to convert from
BM> characters to bytes. This is Encode::encode, or the :encoding layer on
BM> output.
BM>
BM> >
BM> > =======================================================
BM> >
BM> > use Compress::Zlib ;
BM> >
BM> > # Assume S is the socket, and $is_gzipped and $is_utf8 are set correctly
BM> > # from the HTTP response headers, which have just been read from S.
BM> >
BM> > if ($is_gzipped) {
BM> > $body= &read_full_body(S) ;
BM>
BM> Don't call subs with & unless you know why.
BM>
BM> > $body= Compress::Zlib::memGunzip($body) ;
BM>
BM> Is there a good reason why you're not using PerlIO::gzip? (I've never
BM> used it, so there may be some reason it doesn't work I'm not aware
BM> of...?)
BM>
BM> > if ($is_utf8) {
BM> > utf8::upgrade($body) ;
BM>
BM> This should be either
BM>
BM> utf8::decode($body);
BM>
BM> or (preferably)
BM>
BM> $body = Encode::decode(utf8 => $body);
BM>
BM> (and then you can handle all the other charsets the same way: just pass
BM> Encode the value of the charset MIME parameter).
BM>
BM> I would have written this more like
BM>
BM> # push gzip first: closer to the outside world
BM>
BM> # +---------+ +-----+
BM> # program <--> |:encoding| <--> |:gzip| <--> socket
BM> # +---------+ +-----+
BM>
BM> if (is_gzip) {
BM> binmode S, ':gzip';
BM> }
BM>
BM> my $charset = get_charset;
BM> binmode S, ":encoding($charset)";
BM>
BM> # read data... when you've finished, and want the next request:
BM> binmode S, ':pop:pop';
BM>
BM> and then just read or write characters.
BM>
BM> Ben
BM>
BM> --
BM> The Earth is degenerating these days. Bribery and corruption abound.
BM> Children no longer mind their parents, every man wants to write a book,
BM> and it is evident that the end of the world is fast approaching.
BM> Assyrian stone tablet, c.2800 BC benmorrow@tiscali.co.uk
BM>
------------------------------
Date: Mon, 7 Aug 2006 23:38:01 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Simple file list in directory to array
Message-Id: <eb8j4g.d0.1@news.isolution.nl>
Jim Gibson schreef:
> darkmoo:
>> how to grab a list
>> of all files in a directory and load it up into an array.
>
> In addition to the other, more recommended ways, there is always:
>
> my @array = `ls /my/directory`;
If ls is available, and directories and links are welcome.
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: 7 Aug 2006 16:04:34 -0700
From: usenet@DavidFilmer.com
Subject: Re: Simple file list in directory to array
Message-Id: <1154991874.446840.150630@m73g2000cwd.googlegroups.com>
darkmoo wrote:
> Now one more question. I've completely forgetten how to grab a list of all
> files in a directory and load it up into an array.
Good. That's a good thing to forget, because it's almost always a bad
idea (and reflects poor programming practice).
You are probably thinking of doing something ill-advised such as:
my @files = [something that gets a list of files];
foreach my $file( @files ) {
# do something with $file
}
That approach is not good. You should instead skip the array and do
something like this:
foreach my $file( [something that gets a list of files] ) {
# do something with $file
}
You should never (or, at least, very rarely) load up a data structure
(such as an array) and then iterate over the structure. It is (almost)
always better to simply iterate over whatever process loads up the
array (bypassing the need for the array). There are exceptions, such
as if you have a valid need to iterate over the structure more than
once (but things like that should be very uncommon - usually that would
also reflect an error in logic).
Others have given you references to [something that gets a list of
files]. If you are fortunate enough to be using *NIX, though, you might
also consider IO::All. For example, to iterate over all *.txt (case
insensitive) files recursed no more than two directories deep which are
smaller than 1000 bytes and whose first line contains the string
"spooler" (case sensitive):
use strict; use warnings;
use IO::All;
my $root_dir = "/tmp";
foreach my $file (
io($root_dir) -> filter(sub { $_->name =~ /\.txt$/i
&& $_->size < 1000
&& ($_->head)[0] =~ /spooler/
} )
-> all_files(2);
) {
#do something with $file
}
__END__
------------------------------
Date: Mon, 07 Aug 2006 23:41:39 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Simple file list in directory to array
Message-Id: <Xns9818C86F86A2Easu1cornelledu@127.0.0.1>
usenet@DavidFilmer.com wrote in
news:1154991874.446840.150630@m73g2000cwd.googlegroups.com:
> darkmoo wrote:
>
>> Now one more question. I've completely forgetten how to grab a list
>> of all files in a directory and load it up into an array.
>
> Good. That's a good thing to forget, because it's almost always a bad
> idea (and reflects poor programming practice).
>
> You are probably thinking of doing something ill-advised such as:
>
> my @files = [something that gets a list of files];
>
> foreach my $file( @files ) {
> # do something with $file
> }
>
> That approach is not good. You should instead skip the array and do
> something like this:
>
> foreach my $file( [something that gets a list of files] ) {
> # do something with $file
> }
Wait a minute ;-)
The list will still be constructed in its entirety before the loop.
Scale approaches include File::Find and while/readdir.
> use IO::All;
>
> my $root_dir = "/tmp";
> foreach my $file (
> io($root_dir) -> filter(sub { $_->name =~ /\.txt$/i
> && $_->size < 1000
> && ($_->head)[0] =~ /spooler/
> } )
> -> all_files(2);
> ) {
> #do something with $file
> }
I have not used IO::All, but I think you'll still get the list here.
That is, there is no difference between the code above, and one where I
do:
my @files = io($root_dir)->filter(sub { $_->name =~ /\.txt$/i
&& $_->size < 1000
&& ($_->head)[0] =~ /spooler/
} )-> all_files(2);
for my $file ( @files ) {
}
In both cases, processing won't begin until the list has been
constructed.
Sinan
--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
------------------------------
Date: 7 Aug 2006 16:43:03 -0700
From: "yusuf" <yusufm@gmail.com>
Subject: Re: Test::Harness and test values.
Message-Id: <1154994183.848687.240500@75g2000cwc.googlegroups.com>
Ok, heres a cleaned up verion of t.pl:
#!/usr/bin/perl
use strict;
use warnings;
use Test::Harness;
$Test::Harness::Verbose=1;
$|=1;
print "before\n";
my @a = ("t2.pl");
my $allok = runtests(@a);
print " all ok? :" . $allok ." \n";
and t2.pl:
#!/usr/bin/perl
use strict;
use warnings;
use Test::More tests => 1;
ok(1==2);
1;
The question is, why doesn't the print after the runtests() function
print out?
------------------------------
Date: 7 Aug 2006 17:00:37 -0700
From: "yusuf" <yusufm@gmail.com>
Subject: Re: Test::Harness and test values.
Message-Id: <1154995236.984094.284530@m79g2000cwm.googlegroups.com>
> I really don't think it is acceptable to post code with obvious problems
> (the kind that would have been solved by following the posting
> guidelines). Bye.
Where can I read up on the guidelines?
------------------------------
Date: 07 Aug 2006 22:52:33 GMT
From: Abigail <abigail@abigail.be>
Subject: Re: Zero'ing out an array
Message-Id: <slrnedfh1h.hb.abigail@alexandra.abigail.be>
darkmoo (nospam@nospam.net) wrote on MMMMDCCXXIV September MCMXCIII in
<URL:news:pan.2006.08.07.03.04.35.157000@nospam.net>:
== How does one do so?
Ambiguous question, and a question that begs the question "What is
it what you really want to do?"
Anyway, some possible answers:
@array = (0) x @array; # Makes all elements 0.
@array = (); # Empties the array.
$_ = undef for @array; # Undefines all elements.
{
my @array;
... Things with @array ...
} # @array no longer exists.
Abigail
--
# Count the number of lines; code doesn't match \w. Linux specific.
()=<>;$!=$=;($:,$,,$;,$")=$!=~/.(.)..(.)(.)..(.)/;
$;++;$*++;$;++;$*++;$;++;`$:$,$;$" $. >&$*`;
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9573
***************************************