[16757] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4169 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 29 21:05:39 2000

Date: Tue, 29 Aug 2000 18:05:24 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <967597524-v9-i4169@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 29 Aug 2000     Volume: 9 Number: 4169

Today's topics:
    Re: "Require" weirdness (Mark-Jason Dominus)
    Re: another "why doesn't this work?" question. <abe@ztreet.demon.nl>
        calling CGI programs <jpalley@jps.net>
    Re: Counting across multiple lines? <elephant@squirrelgroup.com>
    Re: Counting across multiple lines? <elephant@squirrelgroup.com>
        Creating seperate HTML files via CGI script <gallantknave@my-deja.com>
    Re: fast http server in perl (brian d foy)
        fetching information from cookie with name FireScape <danielxx@bart.nl>
    Re: File handles with scalar value <elephant@squirrelgroup.com>
    Re: Getting the greedy RegEx, wanting the Lazy - help? <lr@hpl.hp.com>
    Re: Getting the greedy RegEx, wanting the Lazy - help? ptomsic@my-deja.com
    Re: Getting the greedy RegEx, wanting the Lazy - help? (Daniel Chetlin)
        HELP! emergency - please help. skwilson@my-deja.com
    Re: HELP! emergency - please help. <mtaylorlrim@my-deja.com>
    Re: HELP! emergency - please help. <mtaylorlrim@my-deja.com>
    Re: HELP! emergency - please help. skwilson@my-deja.com
    Re: HELP! emergency - please help. <mtaylorlrim@my-deja.com>
    Re: HELP! emergency - please help. skwilson@my-deja.com
    Re: hour difference between localtime and POSIX::mktime <elephant@squirrelgroup.com>
        How can I sort using two fields? <carter@aodinc.com>
    Re: How can I sort using two fields? <jeff@vpservices.com>
    Re: How can I sort using two fields? <lr@hpl.hp.com>
    Re: How can I sort using two fields? (Richard J. Rauenzahn)
    Re: How can I sort using two fields? (Mark-Jason Dominus)
    Re: How can I sort using two fields? <lr@hpl.hp.com>
    Re: How can I sort using two fields? (David Wall)
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Tue, 29 Aug 2000 23:43:55 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: "Require" weirdness
Message-Id: <39ac4aba.6d0f$2cc@news.op.net>

In article <8ohb78$1c7$1@panix3.panix.com>,
malgosia askanas <askanas@panix.com> wrote:
>>require 
>>
>>        ... demands that a library file be included
>>         if it hasn't already been included.  
>>
>>        ...
>
>You mean if it has been included in one "used" module, it doesn't get included
>in others?

Sorry, once per day is my limit for reading the manual to you.

>  Don't these modules have independent namespaces?

Yes, they do.

If you're trying to write a module that exports functions into a
namespace, you might want to have a look at the tutorial at

        http://www.plover.com/~mjd/perl/Hello/

that has examples of how to do that.



------------------------------

Date: Wed, 30 Aug 2000 00:50:42 +0200
From: Abe Timmerman <abe@ztreet.demon.nl>
Subject: Re: another "why doesn't this work?" question.
Message-Id: <bleoqso1el02b0rlrt8u1ft9vhgrteu5mg@4ax.com>

On 29 Aug 2000 06:27:06 GMT, jameslee@cse.buffalo.edu (James Lee) wrote:

> Sorry, it's been a while since I've touched perl, and I'm running short on
> time. Any way, I'm trying to do convert all tabs to spaces in my c programs
> using the following code:
> 
> ---begin perl code (windows NT platform)

which doesn't justify the lack of:

	#!/path/to/perl -w
	use strict;

> 
> @ARGV = glob ("*") unless @ARGV;
> 
> while (@ARGV) 

Yes there is magic with files and @ARGV (but it's not that magic).

	perldoc perlrun

Look at the '-n', '-p' and '-i' switches to get you started,

	perldoc perlop

Look in the section "I/O Operators" for 'the null filehandle <>'

 ...

> The program just hangs.

Which was explained by Abigail.

-- 
Good luck,
Abe


------------------------------

Date: Tue, 29 Aug 2000 17:59:10 -0700
From: Jonathan Palley <jpalley@jps.net>
Subject: calling CGI programs
Message-Id: <39AC5C5E.FF6F90B7@jps.net>

How do you call one PERL script from within another?

THANX!
JP



------------------------------

Date: Tue, 29 Aug 2000 22:20:52 GMT
From: jason <elephant@squirrelgroup.com>
Subject: Re: Counting across multiple lines?
Message-Id: <MPG.1416d4b72697a18598971f@localhost>

Colin Keith <newsgroups@ckeith.clara.net> wrote ..
>In article <39A8981B.47000002@uswest.net>, Pedro A Navarro <pnavarro@uswest.net
>>TADSTWTASRTDRAFTARSGRQWTS
>>AGSTDRASGATFSASDGTASTTAGS
>>TQTRAEWSTATSGATSGTASTATST
>>
>>That is for a protein sequence file, in case you are wondering. When I
-
>>I join the multiple lines so I have one single string? I'm fairly sure
>>this is a simple thing to do, but so far I don't know what else to try!
>
>  my($sum) = 0;
>  $sum += length($_) while(<STDIN>);
>  $sum -= $.;  # cheeky cos it assumes 1 \n per line
>  print "Number of characters: $sum\n";

it also assumes that there's no non A-Z characters in the file .. which 
from the originator's use of the tr/A-Z// *and* the fact that the first 
line of his example file was '>header' (which you snipped) is probably 
not a reasonable assumption

>or
>  my($sum) = 0;
>  while(<STDIN>){
>    chomp;
>    $sum += length($_);
>  }

again .. assuming no header and no non A-Z characters

>$/ = "\n\n";  # (won't match for us)
>$_ = <STDIN>;
>s/\n//g;
>print length($_);

why would you assume that "\n\n" wouldn't match ? .. why not undef $/ 
instead which is the standard way of slurping in the whole file (usually 
within a block after you've 'local'ised it)

>You get the idea .. I would suggest length() rather than tr// because tr is 
>replacing the characters it finds, so it takes longer.

rubbish .. there is a minute difference between them .. in fact on 
5.005_03 the tr/A-Z// is quicker than length for this example (not sure 
whether length has been better optimised in 5.6)

PLUS .. there's no s/\n//g; (which would have been better performed with 
tr/\n//d; btw) which means that the tr/A-Z// solution is definitely 
quicker overall

not to mention that it's more accurately what the user wants

  [ lines exceeding 80 chars truncated ]

-- 
  jason -- elephant@squirrelgroup.com --


------------------------------

Date: Tue, 29 Aug 2000 23:07:47 GMT
From: jason <elephant@squirrelgroup.com>
Subject: Re: Counting across multiple lines?
Message-Id: <MPG.1416dfbe5ae1ac90989725@localhost>

jason <elephant@squirrelgroup.com> wrote ..
>rubbish .. there is a minute difference between them .. in fact on 
>5.005_03 the tr/A-Z// is quicker than length for this example (not sure 
>whether length has been better optimised in 5.6)

my apologies for the above .. I had been doing the following benchmark

  use Benchmark;

  my($a,$b) = (0,0);

  timethese( 1 << ( shift || 10 )
         , { tr => sub {
                           local $_ = 'TADSTWTASRTDRAFTARSGRQWTS';
                           $a =   tr/A-Z//;
                         }

           , length => sub {
                               local $_ = 'TADSTWTASRTDRAFTARSGRQWTS';
                               $b = length
                             }
           }
         );

which gave me significantly different results than

  my $x =<<__EOI;			# indented in usenet only
  TADSTWTASRTDRAFTARSGRQWTS
  __EOI

  my($a,$b) = (0,0);

  timethese( 1 << ( shift || 10 )
           , { tr => sub { $a = $x =~ tr/A-Z//; }
             , length => sub { $b = length $x }
             }
           );

and obviously the 'tr' speed is proportional to the length of the 
evaluated string .. whereas 'length' isn't

-- 
  jason -- elephant@squirrelgroup.com --


------------------------------

Date: Tue, 29 Aug 2000 23:46:50 GMT
From: gallantknave <gallantknave@my-deja.com>
Subject: Creating seperate HTML files via CGI script
Message-Id: <8ohi14$j5l$1@nnrp1.deja.com>

Hello,
I would like to know if it's possible for a cgi script to automatically
create seperate HTML files for each message posted on a bulletin board
so as to create a link to the individual message when listed in a index
page of the message entries.

Thanks,
Knave

--
I'm not a nay sayer, I'm just a fan.
Knave

--
I'm not a nay sayer, I'm just a fan.
Knave


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Tue, 29 Aug 2000 19:00:59 -0400
From: brian@smithrenaud.com (brian d foy)
Subject: Re: fast http server in perl
Message-Id: <brian-ya02408000R2908001900590001@news.panix.com>

In article <8oh9gd$rbe$1@news.msu.ru>, "Roman Chumakov" <zfido88@zr.ru> posted:

> Is it possible to write little high-performance web server in perl for
> serving gif files (10-15Kb each)
> I gonna make a counter server.

why not use a stripped down apache with mod_perl?  heck, even a stock 
apache could handle this.  

why do you want to do something different?

-- 
brian d foy                    
CGI Meta FAQ <URL:http://www.smithrenaud.com/public/CGI_MetaFAQ.html>
Perl Mongers <URL:http://www.perl.org/>


------------------------------

Date: Tue, 29 Aug 2000 23:35:23 GMT
From: "Daniel van den Oord" <danielxx@bart.nl>
Subject: fetching information from cookie with name FireScape
Message-Id: <%MXq5.21$bt4.943@Typhoon.bART.nl>

I want to fetch the info in a cookie with the name FireScape how can I do
that ????
I have read the cgi::cookie but since my english isn't that good I don't
understand what they mean... if somebody could maybe post a example on howto
do that I would be very greatfull !!!


Daniel




------------------------------

Date: Tue, 29 Aug 2000 22:41:34 GMT
From: jason <elephant@squirrelgroup.com>
Subject: Re: File handles with scalar value
Message-Id: <MPG.1416d996bb8c857e989722@localhost>

QarnoS <qarnos@ozemail.com.au> wrote ..
>I'm in the intermediate stages of learning Perl and have a question
>which is annoying me enough to matter ;-)
>
>When working with the CGI module, if the submitted form contains a file
>upload, the CGI::param method will return a scalar which doubles as a
>filename and a file handle.
>
>Ie. I can do this:
>$file = $cgi->param('uploadedfile');
>print "Contents of $file:";
>print <$file>;
>
>I was just wondering how the CGI module achieves this.

this is a function of the <> operator (and the 'open', 'print' and 
'close' functions .. and where it makes sense - all other Perl things 
that deal with filehandles)

they accept either a FILEHANDLE or a scalar containing the name of the 
FILEHANDLE

so it's quite simple for a scalar to be both a string and a filehandle

-- 
  jason -- elephant@squirrelgroup.com --


------------------------------

Date: Tue, 29 Aug 2000 14:54:48 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Getting the greedy RegEx, wanting the Lazy - help?
Message-Id: <MPG.1415d105eb6fea398acf5@nntp.hpl.hp.com>

[Please don't quote the entire article you are responding to, after your 
comments.]

n article <39AC3C81.9FFEDA4A@mirusweb.com> on Tue, 29 Aug 2000 21:40:35 
GMT, Mike Cameron <mcameron@mirusweb.com> says...
> Rookie looking to learn. Just curious..
> 
> Why not use RE's to parse html?

perlfaq9: "How do I remove HTML from a string?"

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


------------------------------

Date: Tue, 29 Aug 2000 23:08:56 GMT
From: ptomsic@my-deja.com
Subject: Re: Getting the greedy RegEx, wanting the Lazy - help?
Message-Id: <8ohfpq$gmd$1@nnrp1.deja.com>

In article <eli$0008291617@qz.little-neck.ny.us>,
  Eli the Bearded <elijah@workspot.net> wrote:
>
> I could write a reg-exp to do that, but you probably wouldn't
> understand it and lots of people here would jump down my throat
> about how one should never use REs to parse HTML.
>
> You would be much better off using an HTML parser like HTML::Parser
> from CPAN.


Thanks for the comments, but I'm not actually PARSING HTML.  I'm
looking for a BEGIN and END flag in an HTML file that acts like HTML
comments, but it's not the same.

I might just be able to learn something from your posting, so would it
be too big an inconvience for a look at your RegEx to do that?

Thanks again.
PJT


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Wed, 30 Aug 2000 00:53:15 GMT
From: daniel@chetlin.com (Daniel Chetlin)
Subject: Re: Getting the greedy RegEx, wanting the Lazy - help?
Message-Id: <%VYq5.9474$D7.360632@news-west.usenetserver.com>

On Tue, 29 Aug 2000 23:08:56 GMT,
 ptomsic@my-deja.com <ptomsic@my-deja.com> wrote:
>Thanks for the comments, but I'm not actually PARSING HTML.  I'm
>looking for a BEGIN and END flag in an HTML file that acts like HTML
>comments, but it's not the same.

But the thing is, you can still use HTML::Parser for something like that, and
it's much more likely to work well.

If you post a better spec of exactly what you're trying to do, I'll be happy
to try to show you how.

-dlc





------------------------------

Date: Tue, 29 Aug 2000 23:29:57 GMT
From: skwilson@my-deja.com
Subject: HELP! emergency - please help.
Message-Id: <8ohh0v$i2d$1@nnrp1.deja.com>

I am not a perl programmer and may be in the wrong place - I have a cgi
script for a client site that is down.  can anyone tell me what this
piece of script is trying to do?  I know it's looking for a file and
loading it into a variable, but I don't know what -e means, or what the
+< and > are doing.

$filename = "flatfiles/application.txt";
if(-e $filename)
{
open(FILE, "+<$filename") || &error_exit;
}
else{
open(FILE, ">$filename") || &error_exit;
}
flock FILE, 2;

Please help if you can.

thanks


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Tue, 29 Aug 2000 23:44:25 GMT
From: Mark <mtaylorlrim@my-deja.com>
Subject: Re: HELP! emergency - please help.
Message-Id: <8ohhsj$j2t$1@nnrp1.deja.com>

In article <8ohh0v$i2d$1@nnrp1.deja.com>,
  skwilson@my-deja.com wrote:
> I am not a perl programmer and may be in the wrong place - I have a
cgi
> script for a client site that is down.  can anyone tell me what this
> piece of script is trying to do?  I know it's looking for a file and
> loading it into a variable, but I don't know what -e means, or what
the
> +< and > are doing.

Basically, it is saying...
>
> $filename = "flatfiles/application.txt";
> if(-e $filename)
If application.txt exists

> {
> open(FILE, "+<$filename") || &error_exit;
> }
then open it for input...

> else{
> open(FILE, ">$filename") || &error_exit;
> }
else open it for output (same as creates the file)

> flock FILE, 2;
lock the file

>
> Please help if you can.
>
> thanks
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>

Mark


--
Please reply to this newsgroup as my Deja mail
is used as a spam catcher only!


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Tue, 29 Aug 2000 23:50:39 GMT
From: Mark <mtaylorlrim@my-deja.com>
Subject: Re: HELP! emergency - please help.
Message-Id: <8ohi87$jfu$1@nnrp1.deja.com>


> > +< and > are doing.
>
> > {
> > open(FILE, "+<$filename") || &error_exit;
> > }
> then open it for input...
Actually, the + makes it opened for both read and write.

Mark


--
Please reply to this newsgroup as my Deja mail
is used as a spam catcher only!


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Tue, 29 Aug 2000 23:56:28 GMT
From: skwilson@my-deja.com
Subject: Re: HELP! emergency - please help.
Message-Id: <8ohij2$jrj$1@nnrp1.deja.com>

In article <8ohi87$jfu$1@nnrp1.deja.com>,
  Mark <mtaylorlrim@my-deja.com> wrote:
>
> > > +< and > are doing.
> >
> > > {
> > > open(FILE, "+<$filename") || &error_exit;
> > > }
> > then open it for input...
> Actually, the + makes it opened for both read and write.
>
> Mark
>
> --
> Please reply to this newsgroup as my Deja mail
> is used as a spam catcher only!
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>

So, if the error is getting called and the file is in fact where it's
supposed to be, what could be the problem?


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Wed, 30 Aug 2000 00:06:04 GMT
From: Mark <mtaylorlrim@my-deja.com>
Subject: Re: HELP! emergency - please help.
Message-Id: <8ohj4v$khk$1@nnrp1.deja.com>

Two possibilities.

First, check and see it the file is in fact there. If it is then the
error trapping came from the attempt to open the file for both read and
write.  Probably a permissions error on the file. What user and group
is attempting to open the file? Make sure the file matches the
appropriate user or group permissions.

If the file is not there then the error trapping came from the attempt
to create the file because it did not exist. In this case check to see
that the directory has the proper permissions for the script to create
a file.

I'm far from an expert.  Anyone else want to suggest anything?

Mark



In article <8ohij2$jrj$1@nnrp1.deja.com>,
  skwilson@my-deja.com wrote:
> In article <8ohi87$jfu$1@nnrp1.deja.com>,
>   Mark <mtaylorlrim@my-deja.com> wrote:
> >
> > > > +< and > are doing.
> > >
> > > > {
> > > > open(FILE, "+<$filename") || &error_exit;
> > > > }
> > > then open it for input...
> > Actually, the + makes it opened for both read and write.
> >
> > Mark
> >
> > --
> > Please reply to this newsgroup as my Deja mail
> > is used as a spam catcher only!
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.
> >
>
> So, if the error is getting called and the file is in fact where it's
> supposed to be, what could be the problem?
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>

--
Please reply to this newsgroup as my Deja mail
is used as a spam catcher only!


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Wed, 30 Aug 2000 00:10:30 GMT
From: skwilson@my-deja.com
Subject: Re: HELP! emergency - please help.
Message-Id: <8ohjd7$ksk$1@nnrp1.deja.com>

Thanks so much...  from the sounds of it that's got to be it.  I can't
call anyone tonight, but I'll let you know how it comes out in the
morning.

thanks again...  I thought I was going nuts.  :)




In article <8ohj4v$khk$1@nnrp1.deja.com>,
  Mark <mtaylorlrim@my-deja.com> wrote:
> Two possibilities.
>
> First, check and see it the file is in fact there. If it is then the
> error trapping came from the attempt to open the file for both read
and
> write.  Probably a permissions error on the file. What user and group
> is attempting to open the file? Make sure the file matches the
> appropriate user or group permissions.
>
> If the file is not there then the error trapping came from the attempt
> to create the file because it did not exist. In this case check to see
> that the directory has the proper permissions for the script to create
> a file.
>
> I'm far from an expert.  Anyone else want to suggest anything?
>
> Mark
>
> In article <8ohij2$jrj$1@nnrp1.deja.com>,
>   skwilson@my-deja.com wrote:
> > In article <8ohi87$jfu$1@nnrp1.deja.com>,
> >   Mark <mtaylorlrim@my-deja.com> wrote:
> > >
> > > > > +< and > are doing.
> > > >
> > > > > {
> > > > > open(FILE, "+<$filename") || &error_exit;
> > > > > }
> > > > then open it for input...
> > > Actually, the + makes it opened for both read and write.
> > >
> > > Mark
> > >
> > > --
> > > Please reply to this newsgroup as my Deja mail
> > > is used as a spam catcher only!
> > >
> > > Sent via Deja.com http://www.deja.com/
> > > Before you buy.
> > >
> >
> > So, if the error is getting called and the file is in fact where
it's
> > supposed to be, what could be the problem?
> >
> > Sent via Deja.com http://www.deja.com/
> > Before you buy.
> >
>
> --
> Please reply to this newsgroup as my Deja mail
> is used as a spam catcher only!
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Tue, 29 Aug 2000 22:27:03 GMT
From: jason <elephant@squirrelgroup.com>
Subject: Re: hour difference between localtime and POSIX::mktime
Message-Id: <MPG.1416d62ac6a7a6f0989720@localhost>

Villy Kruse <vek@pharmnl.ohout.pharmapartners.nl> wrote ..
>On Tue, 29 Aug 2000 06:30:25 GMT,
>            steinra@my-deja.com <steinra@my-deja.com> wrote:
>
>
>>> well I guess it is because of the summer/winter time.
>>> try it out with another month and you'll
>>> be surprised.
>>> I think mktime takes care of summer/winter and
>>> ctime does not. (didn't find the
>>> explanation in the POSIX manpage, though)
>>>
>>> tina
>>
>>Thank you!
>>
>>I feel really dumb now :) *blush* It didn't even occur to me.  I really
>>hate dumb mistakes.
>>
>
>
>Not sure you what your mistake is supposed to be.  I tried to run your
>program and find no problems.
>
>If ctime, mktime and localtime does not all handle summer and winter
>time properly I would call that a bug.

surely it's completely reliant on the POSIX subsystem .. which on a lot 
of platforms (or even implementations) will not necessarily be setup 
correctly

-- 
  jason -- elephant@squirrelgroup.com --


------------------------------

Date: Tue, 29 Aug 2000 16:26:13 -0600
From: "Carter" <carter@aodinc.com>
Subject: How can I sort using two fields?
Message-Id: <8ohd3i$8q3$1@news.chatlink.com>

I hope the subject is deceptive.  I can not describe what I wish to
accomplish very easily in a short sentence.

I have a list of lines stored in an array called @links.
Each line contains 5 words separated by |.  Of course, each line is ended by
\n.  BTW, the entire array contains 100 lines.

ie:

This|is|an|example|line
This|line|is|also|one
 .
 .
 .


I've read the PerlFaq4 "How do I sort an array by (anything)? as well as Tom
Christiansen's most excellent "Far More Than Everything You've Ever Wanted
to Know About Sorting", but the solution still evades me.

I need to sort this list alphabetically by group 4.  If more than one word
in group 4 match, then those links must be further sorted alphabetically
according to group 2.

I can sort alphabetically by group 4:

@idx = ();
for (@links) {
    ($link) = /\|[^\|]*\|[^\|]*\|([^\|]*)/;
    push @idx, uc($link);
}
@sorted = @links[ sort { $idx[$a] cmp $idx[$b] } 0 .. $#idx ];

 ...or by group 2

($link) = /[^\|]*\|([^\|]*)/;

But if several lines contain the same word in group 4, I have no idea where
to even begin using the contents of group 2 to further the sort.

Thanks for your time.

--
carter




------------------------------

Date: Tue, 29 Aug 2000 15:43:09 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: How can I sort using two fields?
Message-Id: <39AC3C7D.5D554303@vpservices.com>

Carter wrote:
> 
> I hope the subject is deceptive.

That's either a most excellent typo or something I don't understand. :-)

> I have a list of lines stored in an array called @links.
> Each line contains 5 words separated by |.  Of course, each line is ended by
> \n.  BTW, the entire array contains 100 lines.
> 
> ie:
> 
> This|is|an|example|line
> This|line|is|also|one
> 
> I need to sort this list alphabetically by group 4.  If more than one word
> in group 4 match, then those links must be further sorted alphabetically
> according to group 2.

If your object is to sort the stuff (as oppossed to learning how to
sort), then this will do what you want:

use DBI;
my $dbh = DBI->connect('dbi:RAM:',,,{RaiseError=>1})
                      or die $DBI::errstr;
$dbh->func({data_type=>'PIPE'},[<DATA>],'import');
my $new = $dbh->selectall_arrayref(qq/
    SELECT * FROM table1 ORDER BY col4,col2
/);
__END__
2|cat|banana|kiwi|fungus
3|zebra|banana|kiwi|fungus
1|mole|banana|apple|fungus

It ends up with $new as a reference to an array of arrayrefs, sorted in
the order you specified (i.e. first by field 4, then by field 2) and
would work on any number of lines or fields.

-- 
Jeff


------------------------------

Date: Tue, 29 Aug 2000 16:27:00 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: How can I sort using two fields?
Message-Id: <MPG.1415e69e667e902c98acf7@nntp.hpl.hp.com>

In article <39AC3C7D.5D554303@vpservices.com> on Tue, 29 Aug 2000 
15:43:09 -0700, Jeff Zucker <jeff@vpservices.com> says...
> Carter wrote:
> > 
> > I hope the subject is deceptive.
> 
> That's either a most excellent typo or something I don't understand. :-)

I thought we were intended to be snookered into reading the article by a 
deceptive Subject.  But it is just a typo.  :-(

 ...

> If your object is to sort the stuff (as oppossed to learning how to
> sort), then this will do what you want:
> 
> use DBI;
> my $dbh = DBI->connect('dbi:RAM:',,,{RaiseError=>1})
>                       or die $DBI::errstr;
> $dbh->func({data_type=>'PIPE'},[<DATA>],'import');
> my $new = $dbh->selectall_arrayref(qq/
>     SELECT * FROM table1 ORDER BY col4,col2
> /);
> __END__
> 2|cat|banana|kiwi|fungus
> 3|zebra|banana|kiwi|fungus
> 1|mole|banana|apple|fungus
> 
> It ends up with $new as a reference to an array of arrayrefs, sorted in
> the order you specified (i.e. first by field 4, then by field 2) and
> would work on any number of lines or fields.

hammer::nail

Yet another Little Language to learn (in this case, two: SQL, and how to 
set it up with this particular DBD module).

Uri Guttman promised but never delivered a Sort::Records module using 
the GRT, and for this simple stuff there already is Sort::Fields.  But 
each of those needs its own language too.

I'll respond to 'learning how to sort' directly to the OP.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


------------------------------

Date: 29 Aug 2000 23:44:02 GMT
From: nospam@hairball.cup.hp.com (Richard J. Rauenzahn)
Subject: Re: How can I sort using two fields?
Message-Id: <967592641.975706@hpvablab.cup.hp.com>

"Carter" <carter@aodinc.com> writes:
[...]
>ie:
>
>This|is|an|example|line
>This|line|is|also|one
[...]
>I need to sort this list alphabetically by group 4.  If more than one word
>in group 4 match, then those links must be further sorted alphabetically
>according to group 2.
[...]
>I can sort alphabetically by group 4:
>
>@idx = ();
>for (@links) {
>    ($link) = /\|[^\|]*\|[^\|]*\|([^\|]*)/;
>    push @idx, uc($link);
>}
>@sorted = @links[ sort { $idx[$a] cmp $idx[$b] } 0 .. $#idx ];
>
>...or by group 2
>
>($link) = /[^\|]*\|([^\|]*)/;
>

You're making this more complicated than it needs to be.  Here's one way
to do it -- but not the fastest since the split and uc are performed
multiple times.  You could use map and/or precalculation to speed things
up, but since you're new to this and you're only dealing with 100
elements, I kept it simple.

@links = qw(This|is|an|example|line
            This|line|is|also|one
            a|a|a|d|a|a
            a|a|a|c|a|a
            a|a|a|e|a|a
            a|b|a|e|a|a
            a|c|a|e|a|a
            );

my @sorted = sort { my @a = split(/\|/, $a);
                    my @b = split(/\|/, $b);
                    uc $a[3] cmp uc $b[3]
                              ||
                    uc $a[1] cmp uc $b[1] } @links;

print join("\n", @sorted) . "\n";

>But if several lines contain the same word in group 4, I have no idea where
>to even begin using the contents of group 2 to further the sort.

You need to compare 4th words -- and if they are equal -- return the
comparison of the 2nd words.

Rich
-- 
Rich Rauenzahn ----------+xrrauenza@cup.hp.comx+ Hewlett-Packard Company
Technical Consultant     | I speak for me,     |   19055 Pruneridge Ave. 
Development Alliances Lab|            *not* HP |                MS 46TU2
ESPD / E-Serv. Partner Division +--------------+---- Cupertino, CA 95014


------------------------------

Date: Tue, 29 Aug 2000 23:57:28 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: How can I sort using two fields?
Message-Id: <39ac4de7.6d75$82@news.op.net>

[mailed and posted]

In article <8ohd3i$8q3$1@news.chatlink.com>, Carter <carter@aodinc.com> wrote:
>I need to sort this list alphabetically by group 4.  If more than one word
>in group 4 match, then those links must be further sorted alphabetically
>according to group 2.

Try this:

        @sorted_links = 

        sort {
          my @field_a = split /\|/, $a;
          my @field_b = split /\|/, $b;
             uc $field_a[3] cmp uc $field_b[3] 
                            ||
             uc $field_a[1] cmp uc $field_b[1] 
             ;
        } @links;


(Perl arrays start with item #0, so group 4 is item #3, and group 2 is
item #1.)



------------------------------

Date: Tue, 29 Aug 2000 16:54:52 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: How can I sort using two fields?
Message-Id: <MPG.1415ed2af4a5235a98acf8@nntp.hpl.hp.com>

In article <8ohd3i$8q3$1@news.chatlink.com> on Tue, 29 Aug 2000 16:26:13 
-0600, Carter <carter@aodinc.com> says...
> I hope the subject is deceptive.  I can not describe what I wish to
> accomplish very easily in a short sentence.

Actually, your Subject was *very* descriptive.

> I have a list of lines stored in an array called @links.
> Each line contains 5 words separated by |.  Of course, each line is ended by
> \n.  BTW, the entire array contains 100 lines.
> 
> ie:
> 
> This|is|an|example|line
> This|line|is|also|one

 ...

> I've read the PerlFaq4 "How do I sort an array by (anything)? as well as Tom
> Christiansen's most excellent "Far More Than Everything You've Ever Wanted
> to Know About Sorting", but the solution still evades me.

Then how did you miss this, from the quoted FAQ:

If you need to sort on several fields, the following paradigm is useful.

    @sorted = sort { field1($a) <=> field1($b) ||
                     field2($a) cmp field2($b) ||
                     field3($a) cmp field3($b)
                   }     @data;

> I need to sort this list alphabetically by group 4.  If more than one word
> in group 4 match, then those links must be further sorted alphabetically
> according to group 2.
> 
> I can sort alphabetically by group 4:
> 
> @idx = ();
> for (@links) {
>     ($link) = /\|[^\|]*\|[^\|]*\|([^\|]*)/;
>     push @idx, uc($link);
> }
> @sorted = @links[ sort { $idx[$a] cmp $idx[$b] } 0 .. $#idx ];
> 
> ...or by group 2
> 
> ($link) = /[^\|]*\|([^\|]*)/;

Your regexes hurt the eyes.  It isn't necessary to escape '|' in a 
character class.  But using split() is much more appropriate!

> But if several lines contain the same word in group 4, I have no idea where
> to even begin using the contents of group 2 to further the sort.

You could use your index sort, but store two fields in each element of 
the index array (ref to anonymous two-element array), or use two index 
arrays.  The latter is probably faster, but the former is more fun, so:

my @idx = ();
for (@links) {
    my ($link4, $link2) = (split /\|/)[3, 1];
    push @idx, [ uc $link4, uc $link2 ];
}
my @sorted = @links[ sort { $idx[$a][0] cmp $idx[$b][0]
                                        ||
                            $idx[$a][1] cmp $idx[$b][1] } 0 .. $#idx ];

A Schwartz Transform makes this all cleaner, and doesn't need a physical 
copy of the data before sorting:

my @sorted =
    map $_->[0] => sort { $a->[1] cmp $b->[1]
                                  ||
                          $a->[2] cmp $b->[2] }
    map { my ($link4, $link2) = (split /\|/)[3, 1];
          [ $_, uc $link4, uc $link2 ] } <DATA>;

This is fine for 100 lines (in fact, almost anything is :-), but when 
you get up to 10,000 you might want to do better  -- the Guttman-Rosler 
Transform (ta-da!)

my @sorted =
    map substr($_, 1 + rindex $_, "\0") => sort
    map { my ($link4, $link2) = (split /\|/)[3, 1];
          pack 'A*xA*xA*' => uc $link4, uc $link2, $_ } <DATA>;

> Thanks for your time.

Heck, all the hard work was done more than a year ago.

http://www.hpl.hp.com/personal/Larry_Rosler/sort/

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


------------------------------

Date: 29 Aug 2000 20:56:44 -0400
From: darkon@one.net (David Wall)
Subject: Re: How can I sort using two fields?
Message-Id: <8F9FDE762darkononenet@206.112.192.118>

carter@aodinc.com (Carter) wrote in <8ohd3i$8q3$1@news.chatlink.com>:

>I hope the subject is deceptive.  I can not describe what I wish to
>accomplish very easily in a short sentence.

I guess you meant "not deceptive" or "informative".  In any case, yes, it's  
a good subject line. :-)

>I have a list of lines stored in an array called @links.
>Each line contains 5 words separated by |.  Of course, each line is
>ended by \n.  BTW, the entire array contains 100 lines.

Hmm, you're storing the whole line as a single scalar element of the array 
@links.  That makes it harder than it need be (at least for me). This would 
be easier if you stored it in some slightly more complex data structure, 
such as an array of arrays.


>I've read the PerlFaq4 "How do I sort an array by (anything)? as well as
>Tom Christiansen's most excellent "Far More Than Everything You've Ever
>Wanted to Know About Sorting", but the solution still evades me.

You looked in the FAQ -- good!


>I need to sort this list alphabetically by group 4.  If more than one
>word in group 4 match, then those links must be further sorted
>alphabetically according to group 2.

Below is a program that illustrates the kind of thing you might want to do.    
It uses an array of (references to) arrays.  For more information on this 
kind of thing, see the perlreftut and perlref sections of the Perl 
documentation, as well as the sections you already looked at.


#!/usr/bin/perl -w
use strict;
my @stuff;
while (<DATA>) {
    my @line = split /\|/, $_;
    push @stuff, \@line;
}
my @sorted = sort   { 
       $a->[4] cmp $b->[4] 
    || $a->[1] <=> $b->[1] 
} @stuff;

foreach my $record (@sorted) {
    print join '|', @$record;
}

__DATA__
f1|2|aaa|bbb|Homer
f1|1|aaa|bbb|Homer
f1|3|aaa|bbb|Marge
f1|1|aaa|bbb|Marge
f1|2|aaa|bbb|Marge
f1|2|aaa|bbb|Bart
f1|1|aaa|bbb|Lisa
f1|2|aaa|bbb|Lisa


That's a bit more verbose than it really needs to be, but I wanted all the 
steps to be as clear as I could make them.

Somewhat more Perlishly:

#!/usr/bin/perl -w
use strict;
my @stuff;
push @stuff, [ split /\|/ ] while <DATA>;
my @sorted = sort { $a->[4] cmp $b->[4] || $a->[1] <=> $b->[1] } @stuff;
print join '|', @$_ foreach @sorted;


Or maybe even (IIRC) Schwartz style:

#!/usr/bin/perl -w
use strict;
print map { join '|', @$_ } 
      sort { $a->[4] cmp $b->[4] || $a->[1] <=> $b->[1] } 
      map { [split /\|/] } <DATA>;


(Same DATA section each time)

HTH.

(I think I've posted something similiar to this a couple of times in the 
last week or two.  Maybe the FAQ on sorting should have something added to 
it?  I'm definitely getting deja vu....)

-- 
David Wall
darkon@one.net


------------------------------

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V9 Issue 4169
**************************************


home help back first fref pref prev next nref lref last post