[19963] in Perl-Users-Digest
Perl-Users Digest, Issue: 2158 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Nov 19 18:10:45 2001
Date: Mon, 19 Nov 2001 15:10:14 -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: <1006211413-v10-i2158@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Mon, 19 Nov 2001 Volume: 10 Number: 2158
Today's topics:
Re: Newbie module problem (Owain McGuire)
Re: Newbie module problem <Tassilo.Parseval@post.rwth-aachen.de>
parsing HTML - from URLs <lreilly@la.com>
Re: parsing HTML - from URLs <tony_curtis32@yahoo.com>
Re: parsing HTML - from URLs (Tad McClellan)
Perl Errors on Cygwin 1.0 <shelorm@earthlink.net>
Picture aspect ratio <mail@nexo.de>
Re: Picture aspect ratio <mikesl@wrq.com>
Re: Picture aspect ratio <cp@onsitetech.com>
Re: please help: good OOP guide for perl (trwww)
Re: please help: good OOP guide for perl <Tassilo.Parseval@post.rwth-aachen.de>
Re: please help: good OOP guide for perl (Randal L. Schwartz)
Re: please help: good OOP guide for perl <Tassilo.Parseval@post.rwth-aachen.de>
Re: send HTTP response (paul)
Re: send HTTP response <roger_faust@bluewin.ch>
Re: send HTTP response <flavell@mail.cern.ch>
Re: slicing multi dimensional arrays <5l259r001@sneakemail.com>
SMS Text Messaging SOAP/Web Service <bill.leigh@leighly.fsnet.co.uk>
Re: Sorting files by date on Win32 (Maynard)
Re: Sorting files by date on Win32 <krahnj@acm.org>
Re: Sorting files by date on Win32 (Tad McClellan)
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Mon, 19 Nov 2001 19:06:08 GMT
From: owain@nospam.demon.co.uk (Owain McGuire)
Subject: Re: Newbie module problem
Message-Id: <3bf95740.349575282@news.demon.co.uk>
On 19 Nov 2001 01:56:24 -0800, pwheeler@kentports.org (Perry Wheeler)
wrote:
>Sorry to cross-post, I originally posted this on 'modules last week
>but didn't get anywhere.
>
>
>Hi,
>
>Possibly I shouldn't be trying to run before I can walk but here's the
>problem.
>
>I've written a short perl script to get stock prices and email the
>results to an sms gateway service based on the Finance::Quote module
>from CPAN.
>
>The script works (it runs on my proxy server) without any problem. I
>can telnet to the server, su, run the thing and a couple of minutes
>later I get an sms message to my phone with the result.
>
>Trouble is I can't get the thing to run as scheduled job (I set this
>up under Webmin). I set it run as root, so it's not permission based.
>An error report is produced as follows (sorry for the long quote):
>
>=======================
>Can't locate loadable object for module HTML::Parser in @INC (@INC
>contains: /usr/local/lib/perl5/site_perl/5.6.1
>/usr/lib/perl5/5.00503/i386-linux /usr/lib/perl5/5.00503
>/usr/lib/perl5/site_perl/5.005/i386-linux
>/usr/lib/perl5/site_perl/5.005 .) at
>/usr/local/lib/perl5/site_perl/5.6.1/HTML/TableExtract.pm line 16
>BEGIN failed--compilation aborted at
>/usr/local/lib/perl5/site_perl/5.6.1/HTML/TableExtract.pm line 16.
>BEGIN failed--compilation aborted at
>/usr/local/lib/perl5/site_perl/5.6.1/Finance/Quote/Cdnfundlibrary.pm
>line 20.
>BEGIN failed--compilation aborted at (eval 3) line 1.
> at pwquote.pl line 36
>Can't locate object method "methods" via package
>"Finance::Quote::Cdnfundlibrary" at
>/usr/local/lib/perl5/site_perl/5.6.1/Finance/Quote.pm line 109.
>========================
>
>Parser.pm is present in /usr/local/lib/perl5/site_perl/5.6.1/HTML. Any
>help, at novice level would be much appreciated
>
>Thanks
>
>Perry W
>psw at kentcoast dot co dot uk
The module is not found in the path. move it to somewhere in the path
or use a lib statement
use lib /the/directory/where/it/is;
I suspect that the job sheduler has a different path than your shell.
you can find out what the path is (@INC) by wrting a script to print
it and then have the job scheduler run it to see how it differs.
O.
------------------
Owain McGuire owain@nospam.demon.co.uk (Minus the nospam.)
------------------------------
Date: Mon, 19 Nov 2001 20:38:07 +0100
From: "Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de>
Subject: Re: Newbie module problem
Message-Id: <9tbn2v$udb$01$1@news.t-online.com>
On Mon, 19 Nov 2001 19:06:08 GMT, Owain McGuire wrote:
[
Please put your reply behind the text that you had been
quoting after trimming it suitably.
]
> The module is not found in the path. move it to somewhere in the path
> or use a lib statement
>
> use lib /the/directory/where/it/is;
And especially, use it correctly. 'use lib' doesn't take a bareword, it
must be quoted.
> you can find out what the path is (@INC) by wrting a script to print
> it and then have the job scheduler run it to see how it differs.
Typing 'perl -V' in a shell would be the more direct approach to find
out what @INC contains.
Tassilo
--
Happiness is the greatest good.
------------------------------
Date: Mon, 19 Nov 2001 12:21:28 -0700
From: Lee <lreilly@la.com>
Subject: parsing HTML - from URLs
Message-Id: <3BF95BB8.A7E9CB99@la.com>
Hi,
I wonder if someone could offer me some advice / point me to some
resources...
I am quite new to Perl, but am enjoying learning and working with it. I
understand that Perl can be used to parse HTML files, but I would like
to know how (or if it's even possible) to parse HTML files located on
another server e.g. supply a URL and have Perl retrieve the HTML page
for processing.
Any help would be greatly appreciated :)
- Best regards,
Lee
------------------------------
Date: Mon, 19 Nov 2001 13:29:39 -0600
From: Tony Curtis <tony_curtis32@yahoo.com>
Subject: Re: parsing HTML - from URLs
Message-Id: <8766868spo.fsf@limey.hpcc.uh.edu>
>> On Mon, 19 Nov 2001 12:21:28 -0700,
>> Lee <lreilly@la.com> said:
> I am quite new to Perl, but am enjoying learning and
> working with it. I understand that Perl can be used to
> parse HTML files, but I would like to know how (or if
> it's even possible) to parse HTML files located on
> another server e.g. supply a URL and have Perl retrieve
> the HTML page for processing.
Well, browsers do it, so perl shouldn't have any
trouble... :-)
perldoc lwpcook
perldoc LWP::Simple
http://search.cpan.org/doc/RSE/lcwa-1.0.0/lib/lwp/lib/LWP.pm
hth
t
--
Oh! I've said too much. Smithers, use the amnesia ray.
------------------------------
Date: Mon, 19 Nov 2001 22:58:45 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: parsing HTML - from URLs
Message-Id: <slrn9vj0ln.h4f.tadmc@tadmc26.august.net>
Lee <lreilly@la.com> wrote:
>
>I wonder if someone could offer me some advice / point me to some
>resources...
The Perl Frequently Asked Questions list is a very good resource,
and it is already installed on your hard drive!
>I am quite new to Perl, but am enjoying learning and working with it.
Use the docs, Luke.
>supply a URL and have Perl retrieve the HTML page
>for processing.
>
>Any help would be greatly appreciated :)
perldoc -q HTML
or
perldoc -q fetch
would have led to this Perl FAQ:
"How do I fetch an HTML file?"
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
Date: Mon, 19 Nov 2001 18:35:23 GMT
From: "Mark Shelor" <shelorm@earthlink.net>
Subject: Perl Errors on Cygwin 1.0
Message-Id: <LhcK7.52832$S4.5036442@newsread1.prod.itd.earthlink.net>
Running "make test" on many of the CPAN modules results in nasty "fork_copy"
errors when running perl (5.005_03) on Cygwin 1.0.
Is there quick patch for this? A search of the usual resources recommends
updating everything (both cygwin and perl) to the lastest versions, but the
update is only a beta version of cygwin. Plus, a complete update (perl +
cygwin) is a lot of work and downloading time on my slow link. Any
suggestions? Thanks,
Mark
------------------------------
Date: Mon, 19 Nov 2001 22:01:36 +0100
From: "Emil Horowitz" <mail@nexo.de>
Subject: Picture aspect ratio
Message-Id: <9tbs0u$1ld5c$1@ID-78897.news.dfncis.de>
Hi,
hopelfully I will be able to describe my problem in a comprehensible way:
My task is to generate small thumbnails of pictures uploaded by the users
and fit them into a dynamically generated HTML page. The picture formates
are not fixed, and therefore I do not know the aspect ratio. To generate
undistorted thumbnails, I would have to find out the height and width of the
original picture. Is there a way to achieve this with Perl?
Thanks,
Emil
------------------------------
Date: Mon, 19 Nov 2001 21:48:07 GMT
From: Michael Slass <mikesl@wrq.com>
Subject: Re: Picture aspect ratio
Message-Id: <m3d72ebfg2.fsf@thneed.na.wrq.com>
"Emil Horowitz" <mail@nexo.de> writes:
>Hi,
>
>hopelfully I will be able to describe my problem in a comprehensible way:
>
>My task is to generate small thumbnails of pictures uploaded by the users
>and fit them into a dynamically generated HTML page. The picture formates
>are not fixed, and therefore I do not know the aspect ratio. To generate
>undistorted thumbnails, I would have to find out the height and width of the
>original picture. Is there a way to achieve this with Perl?
>
>Thanks,
>Emil
>
>
>
If you're working on Linux, there are all the tools which can munge
pictures in the pnm format, and tools to convert to/from pnm to other
formats (jpg, bmp, gif, etc).
With all of those, you can set up a nice pipeline to make a pnm from
the user's image, send that to pnmscale to make a thumbnail with the
same aspect ratio as the original, and then back into gif or jpg
with a -thumb tacked onto the name.
Do an "apropos pnm" to see a list of the pnm tools, and read the man
pages for those that seem promising.
Perl can certainly be the glue that stiches all those tools together,
choosing the correct converters based on the original file type, and
making appropriate choices for the name and location of the created
thumbnail.
--
Mike
------------------------------
Date: 19 Nov 2001 21:49:31 GMT
From: "Curtis Poe" <cp@onsitetech.com>
Subject: Re: Picture aspect ratio
Message-Id: <9tbupb$idq@dispatch.concentric.net>
Check out Image Magick. http://www.imagemagick.org/www/perl.html. Or, more
specifically, http://www.imagemagick.org/www/perl.html#geta, which explains
the methods necessary to get the image attributes.
--
Cheers,
Curtis Poe
Senior Programmer
ONSITE! Technology
www.onsitetech.com
503.233.1418
"Emil Horowitz" <mail@nexo.de> wrote in message
news:9tbs0u$1ld5c$1@ID-78897.news.dfncis.de...
> Hi,
>
> hopelfully I will be able to describe my problem in a comprehensible way:
>
> My task is to generate small thumbnails of pictures uploaded by the users
> and fit them into a dynamically generated HTML page. The picture formates
> are not fixed, and therefore I do not know the aspect ratio. To generate
> undistorted thumbnails, I would have to find out the height and width of
the
> original picture. Is there a way to achieve this with Perl?
>
> Thanks,
> Emil
>
>
>
------------------------------
Date: 19 Nov 2001 11:32:23 -0800
From: toddrw69@excite.com (trwww)
Subject: Re: please help: good OOP guide for perl
Message-Id: <d81ecffa.0111191132.6314d17a@posting.google.com>
"ETHOME USER" <toyshop@cm1.ethome.net.tw> wrote in message news:<9t6n8j$kkb$1@news.ethome.net.tw>...
> I enjoy programming with perl because it is so efficent and elegant but i
> am so confused with the OOP concept...
>
> could anyone please tell me where to find good resouces on perl OOP
> programming ? (network on line free resouces are prefered)
>
> many thanks for your help.
You could help yourself and regulary read the articles posted at www.perl.com.
Simon Cozens just wrote an article about OOP in perl:
http://www.perl.com/pub/a/2001/11/07/ooperl.html
there was also one last year during the beginners intro to perl series:
http://www.perl.com/pub/a/2000/12/begperl5.html
------------------------------
Date: Mon, 19 Nov 2001 21:00:17 +0100
From: "Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de>
Subject: Re: please help: good OOP guide for perl
Message-Id: <9tboch$boo$03$1@news.t-online.com>
On 19 Nov 2001 11:32:23 -0800, trwww wrote:
> "ETHOME USER" <toyshop@cm1.ethome.net.tw> wrote in message news:<9t6n8j$kkb$1@news.ethome.net.tw>...
>> I enjoy programming with perl because it is so efficent and elegant but i
>> am so confused with the OOP concept...
>>
>> could anyone please tell me where to find good resouces on perl OOP
>> programming ? (network on line free resouces are prefered)
>>
>> many thanks for your help.
>
> You could help yourself and regulary read the articles posted at www.perl.com.
>
> Simon Cozens just wrote an article about OOP in perl:
>
> http://www.perl.com/pub/a/2001/11/07/ooperl.html
Well, hopefully this was only the first part of a series on this topic.
He basically just explained how to use an OO-module. That is, he explained
the invocation of methods with the arrow-operator, not much more. You
wont get a more prfound glance behind the scenes of, for instance,
object-oriented Perl modules.
I think a more useful ressource would be the perldocs, especially
perltoot and perltootc give a nice insight into the needed basics.
[...]
Tassilo
--
Examinations are formidable even to the best prepared, for
even the greatest fool may ask more the the wisest man can answer.
-- C.C. Colton
------------------------------
Date: 19 Nov 2001 12:36:34 -0800
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: please help: good OOP guide for perl
Message-Id: <m1elmu7b1p.fsf@halfdome.holdit.com>
>>>>> "Tassilo" == Tassilo v Parseval <Tassilo.Parseval@post.rwth-aachen.de> writes:
Tassilo> I think a more useful ressource would be the perldocs, especially
Tassilo> perltoot and perltootc give a nice insight into the needed basics.
And don't forget perlboot!
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
------------------------------
Date: Mon, 19 Nov 2001 22:05:31 +0100
From: "Tassilo v. Parseval" <Tassilo.Parseval@post.rwth-aachen.de>
Subject: Re: please help: good OOP guide for perl
Message-Id: <9tbs6r$beq$07$1@news.t-online.com>
On 19 Nov 2001 12:36:34 -0800, Randal L. Schwartz wrote:
>>>>>> "Tassilo" == Tassilo v Parseval <Tassilo.Parseval@post.rwth-aachen.de> writes:
>
>Tassilo> I think a more useful ressource would be the perldocs, especially
>Tassilo> perltoot and perltootc give a nice insight into the needed basics.
>
> And don't forget perlboot!
Hehe, sorry. I had been thinking of yours when recommending perltoot
actually. You easily get confused with perltoot, perltootc, perlboot, perlbot
and the lot so someone definitely should come up with better names.
Perhaps perloo1 to perloo4 or so, where perlboot should be the first one
to read.
Tassilo
--
Too much of everything is just enough.
-- Bob Wier
------------------------------
Date: 19 Nov 2001 10:13:18 -0800
From: paulthomson@hotmail.com (paul)
Subject: Re: send HTTP response
Message-Id: <464e478f.0111191013.328d10c2@posting.google.com>
Hi Again!
Thanks for your help. For debugging purposes I have a script making
the http request and printing the response to screen.
The raw version (print "Status: 200 OK\n\n";) works ok so far (I
think...) BUT if I try either of the following:
print $query->header(-nph=>1,
-status=>'200 OK',
-type=>'text/html');
print $query->header(-type=>'text/html',
-status=>'204 No Response');
These 2 examples give the error 'Can't call method "header" on an
undefined value ' I know CGI is installed as I am using it elsewher in
the script (e.g. escape). I even tried doing a use lib, etc in case it
was not installed properly by my host.
my $res = HTTP::Response->new(200, "OK");
$res->content("OK\n");
$res;
This effort seems to return nothing to my request script. I know I am
building the request properly but just in case I have it here:
### begin actual send request
$ua->timeout( 5 ); # set timeout to 5 seconds.
my $request = new HTTP::Request POST
=>'http://mysite/cgi-bin/getsms.pl';
$request->content_type('application/x-www-form-urlencoded');
$reqcontent ="&UserName=paul&Key=ULXM7BPTB7MIZB1B=test";
$request->content($reqcontent);
my $response = $ua->request($request);
$rcode = $response->content;
print $response->content;
Thanks in advance for any help,
Paul
------------------------------
Date: Sun, 18 Nov 2001 05:00:31 +0100
From: Roger Faust <roger_faust@bluewin.ch>
Subject: Re: send HTTP response
Message-Id: <3bf96d0e_2@news.bluewin.ch>
paul wrote:
> Hi Again!
>
> Thanks for your help. For debugging purposes I have a script making
> the http request and printing the response to screen.
>
> The raw version (print "Status: 200 OK\n\n";) works ok so far (I
> think...) BUT if I try either of the following:
\n\n isn't right, unfortunately, it works on most *nix, and lazzy people
are using it :-), maybe that's a bug. use \r\n\r\n
the seems to be a problem with the modules, check the docs for more
information
--
Roger Faust
------------------------------
Date: Mon, 19 Nov 2001 23:06:14 +0100
From: "Alan J. Flavell" <flavell@mail.cern.ch>
Subject: Re: send HTTP response
Message-Id: <Pine.LNX.4.30.0111192252360.9713-100000@lxplus023.cern.ch>
On Nov 18, Roger Faust inscribed on the eternal scroll:
[following up to a rather confused posting that doesn't seem to have
decided whether it's an HTTP transaction or a CGI script]
> > The raw version (print "Status: 200 OK\n\n";) works ok so far (I
> > think...) BUT if I try either of the following:
>
> \n\n isn't right, unfortunately, it works on most *nix, and lazzy people
> are using it :-), maybe that's a bug. use \r\n\r\n
Yet another case of someone who hasn't read perldoc perlport, it
seems. If you really _do_ want ASCII CRLF (but that isn't entirely
clear yet), the proper portable notation would be \015\012 in binmode.
There's a bit about newline representations in here:
http://cvs.perl.org/cvsweb/perlfaq/perlfaq9.pod
(thanks for the pointer, brian!) You probably want to click on
"Revision 1.5".
If this is a CGI question, I'll try to be more helpful about it when
an updated version of the question appears on c.i.w.a.cgi [1]
> the seems to be a problem with the modules,
does there, indeed?
> check the docs for more information
Now you're talking.
[1] I mean with some kind of clarification about whether it's meant
to be a call to CGI.pm, or some hand-knitted CGI code (and if so,
whether it's parsed-headers or NPH), or an attempt to initiate an
actual HTTP transaction rather than a CGI.
Some indication of the big-picture would also be useful; it's
difficult at the moment to see what the overall purpose of the edifice
is meant to be.
------------------------------
Date: Mon, 19 Nov 2001 22:24:44 +0100
From: "Steffen Müller" <5l259r001@sneakemail.com>
Subject: Re: slicing multi dimensional arrays
Message-Id: <9tbted$quf$05$1@news.t-online.com>
"Anno Siegel" <anno4000@lublin.zrz.tu-berlin.de> schrieb im Newsbeitrag
news:9t3acn$s1q$1@mamenchi.zrz.TU-Berlin.DE...
| According to Steffen Müller <tsee@gmx.net>:
| >
| > Hi,
| > I have been torturing my head and the perldocs for a simple
solution,
| > but failed (or I would not post in the first place ;). What I am trying
to
| > do is to create a copy of a portion of a multi-dimensional array. Please
do
| > not tell me they do not really exist in Perl, as I already know that.
[snip example]
| Anyway, this smells of recursion. Whatever the details, I'd look into
| an approach like this:
You're right - recursion.
[snip pseudo-code]
| This would likely be destructive to @coords and @dimensions, so
| you may want a wrapper around it.
I did that and changed your code a bit to suit my needs. Note that it can be
used in two distinct ways: Either pass a ref to @ranges and @coords and have
the behaviour I originally sought, or pass only a ref to @ranges and have
the sub use the contents of @ranges as the indices to grab. If an element of
@ranges is an array ref, the sub uses all values contained in the referenced
array as indices for the current dimension. It's simpler than this might
make it seem:
#!perl
use strict;
use warnings;
use Data::Dumper;
sub multi_dim_slice {
my ( $array, $ranges, $coords) = @_;
my @coords;
@coords = @$coords if defined($coords);
my @ranges = @$ranges;
if ( @ranges ) {
my $coord;
$coord = shift @coords;
my $range = shift @ranges;
my @indices;
if ( ref($range) eq 'ARRAY' ) {
@indices = @$range;
} else {
@indices = defined($coord) ?
$coord .. ($coord + $range - 1) :
$range;
}
my @partial_slice = @{$array}[ @indices ];
my @slice;
return multi_dim_slice( \@partial_slice, \@ranges, \@coords)
if (@ranges == 0);
foreach (@partial_slice) {
push @slice, [ multi_dim_slice( $_, \@ranges, \@coords) ];
}
return @slice;
} else {
# no slicing necessary
return @$array;
}
}
my @ary = ( [
[ 1 .. 3 ],
[ 4 .. 6 ],
[ 7 .. 9 ]
],
[
['a'..'c'],
['d'..'f'],
['g'..'i']
],
[
[10 .. 12],
[13 .. 15],
[16 .. 18]
] );
my @coords = (2,0,1);
my @ranges = (1,2,2);
my @slice = multi_dim_slice(\@ary, \@ranges, \@coords);
print "This is the original behavior:\n";
print Dumper @slice;
@ranges = (2,[0,1],[1,2]);
@slice = multi_dim_slice(\@ary, \@ranges);
print "\n\nThis is the behavior without \@coords: (Should print the
same)\n";
print Dumper @slice;
Sorry for the large amount of data, but I figured you would prefer to have
some drag & drop code if you decided to test this.
Thank you very much for your help.
Steffen
--
$_=q;0cb212c210b0bb010c0113bb0c410c0b516c0bb3d212c2b0b0b016b6cb2b2c21010c0
b41110b3bba0e0c0d2c4b2b6bc013d2c0d0b01012b0b0;;s/\n//g;s/(\d)/$1<2?$1:'0'x
$1/ge;s/([a-f])/'1'x(ord($1)-97)/ge;$o=$_;push@o,substr($o,$_*8,8) for(0..
24);for(@o){print"\0"x(26-$i).chr(oct('0b'.($_)))."\r";$i++};print"\n"#stm
------------------------------
Date: Mon, 19 Nov 2001 19:35:40 -0000
From: "Bill Leigh" <bill.leigh@leighly.fsnet.co.uk>
Subject: SMS Text Messaging SOAP/Web Service
Message-Id: <9tbng5$ve5$1@newsg2.svr.pol.co.uk>
Ok, its a bit of a gimmic, but the truth is that
the following web service is a fully
commercial SMS web service that allows you
to send SMS text messages, ringtones and operator
logos from within any application.
http://www.soapengine.com/lucin/soapenginex/smsx.asmx
The manuals a bit heavy at 100+ pages but
you can get by without really using it.
Bill
------------------------------
Date: 19 Nov 2001 13:16:35 -0800
From: rev_maynard3@hotmail.com (Maynard)
Subject: Re: Sorting files by date on Win32
Message-Id: <a4847dc.0111191316.660d4c91@posting.google.com>
Tad,
Thanks for the speedy response. After my original post I tried
something a little different... I'm now grabbing the file details with
stat(), and putting the filename, size, and last modified date into a
2d array. This is what I had originally intended. I still seem
unable to sort my populated array. Same problem, different angle.
I'm sure there are more eloquent ways of doing pieces of my work...
I'm just curious about what I'm doing wrong with the line "@sorted =
sort {$a[2] <=> $b[2]}@unsorted;" (it's supposed to sort the array by
the modified date column).
#----- BEGIN -----
opendir (LOGDIR,'d:/Logs');
@fileList = grep !/^\./, readdir(LOGDIR);
closedir LOGDIR;
# POPULATE UNSORTED ARRAY
$rowCounter = 0;
foreach $f (@fileList){
($size, $mtime) = (stat $f)[7,9];
${unsorted[$rowCounter]} = ["$f", "$size", "$mtime"];
$rowCounter++;
}
# SORT ARRAY
@sorted = sort {$a[2] <=> $b[2]}@unsorted;
#DISPLAY UNSORTED and SORTED LISTS (DEBUG ONLY)
print "Unsorted list:\n";
print "-------------------\n";
$rowCounter = 0;
foreach $element (@unsorted) {
print "$unsorted[$rowCounter][0], $unsorted[$rowCounter][1],
$unsorted[$rowCounter][2]\n";
$rowCounter++;
}
print "\nSorted list:\n";
print "-------------------\n";
$rowCounter = 0;
foreach $element (@sorted) {
print "$sorted[$rowCounter][0], $sorted[$rowCounter][1],
$sorted[$rowCounter][2]\n";
$rowCounter++;
}
#----- END -----
Thanks in advance.
------------------------------
Date: Mon, 19 Nov 2001 22:51:27 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Sorting files by date on Win32
Message-Id: <3BF98DB2.AD2A1C89@acm.org>
Maynard wrote:
>
> Tad,
>
> Thanks for the speedy response. After my original post I tried
> something a little different... I'm now grabbing the file details with
> stat(), and putting the filename, size, and last modified date into a
> 2d array. This is what I had originally intended. I still seem
> unable to sort my populated array. Same problem, different angle.
>
> I'm sure there are more eloquent ways of doing pieces of my work...
> I'm just curious about what I'm doing wrong with the line "@sorted =
> sort {$a[2] <=> $b[2]}@unsorted;" (it's supposed to sort the array by
> the modified date column).
>
> #----- BEGIN -----
use warnings;
use strict;
my $logdir = 'd:/Logs';
> opendir (LOGDIR,'d:/Logs');
opendir LOGDIR, $logdir or die "Cannot open $logdir: $!";
> @fileList = grep !/^\./, readdir(LOGDIR);
> closedir LOGDIR;
>
> # POPULATE UNSORTED ARRAY
> $rowCounter = 0;
> foreach $f (@fileList){
> ($size, $mtime) = (stat $f)[7,9];
> ${unsorted[$rowCounter]} = ["$f", "$size", "$mtime"];
> $rowCounter++;
> }
my @unsorted = map { [ $_, (stat "$logdir/$_")[7,9] ] } @fileList;
> # SORT ARRAY
> @sorted = sort {$a[2] <=> $b[2]}@unsorted;
my @sorted = sort { $a->[2] <=> $b->[2] } @unsorted;
> #DISPLAY UNSORTED and SORTED LISTS (DEBUG ONLY)
> print "Unsorted list:\n";
> print "-------------------\n";
> $rowCounter = 0;
> foreach $element (@unsorted) {
> print "$unsorted[$rowCounter][0], $unsorted[$rowCounter][1],
> $unsorted[$rowCounter][2]\n";
> $rowCounter++;
> }
for my $element ( @unsorted ) {
local $" = ', ';
print "@$element\n";
}
> print "\nSorted list:\n";
> print "-------------------\n";
> $rowCounter = 0;
> foreach $element (@sorted) {
> print "$sorted[$rowCounter][0], $sorted[$rowCounter][1],
> $sorted[$rowCounter][2]\n";
> $rowCounter++;
> }
for my $element ( @sorted ) {
local $" = ', ';
print "@$element\n";
}
> #----- END -----
>
> Thanks in advance.
John
--
use Perl;
program
fulfillment
------------------------------
Date: Mon, 19 Nov 2001 22:58:44 GMT
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Sorting files by date on Win32
Message-Id: <slrn9vivhd.h4f.tadmc@tadmc26.august.net>
Maynard <rev_maynard3@hotmail.com> wrote:
>I'm just curious about what I'm doing wrong with the line "@sorted =
>sort {$a[2] <=> $b[2]}@unsorted;" (it's supposed to sort the array by
You should have "use strict" turned on, it would have helped
you find the problem. You are accessing arrays named @a and @b
when you are supposed to be accessing scalars named $a and $b.
>opendir (LOGDIR,'d:/Logs');
You should (still) check the return value to see if it did what you
asked it to do:
opendir (LOGDIR, 'd:/Logs') or die "could not opendir 'd:/Logs' $!";
>@fileList = grep !/^\./, readdir(LOGDIR);
>closedir LOGDIR;
>
># POPULATE UNSORTED ARRAY
>$rowCounter = 0;
If you use push() then you won't need to maintain the counter yourself.
>foreach $f (@fileList){
You do not need the @fileList temporary variable:
foreach my $f ( grep !/^\./, readdir(LOGDIR) ){
> ($size, $mtime) = (stat $f)[7,9];
> ${unsorted[$rowCounter]} = ["$f", "$size", "$mtime"];
^ ^ ^ ^ ^ ^ ^ ^
None of those characters are needed, so they should not be there:
$unsorted[$rowCounter] = [$f, $size, $mtime];
Or even better:
push @unsorted, [$f, $size, $mtime]; # Look Ma! No indexes!
> $rowCounter++;
>}
>
># SORT ARRAY
>@sorted = sort {$a[2] <=> $b[2]}@unsorted;
@unsorted contains references, so $a/$b are references. You need to
dereference the references:
@sorted = sort { $a->[2] <=> $b->[2]} @unsorted;
>$rowCounter = 0;
>foreach $element (@unsorted) {
> print "$unsorted[$rowCounter][0], $unsorted[$rowCounter][1],
>$unsorted[$rowCounter][2]\n";
Gak! You don't even use $element for anything!
foreach my $element ( @unsorted ) {
print "$element->[0], $element->[1], $element->[2]\n";
}
> $rowCounter++;
If you find yourself doing explicit indexing in Perl, pause to
rethink your approach. You are often not "doing it right" if
you are indexing yourself.
--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
------------------------------
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 2158
***************************************