[19380] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1575 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Aug 21 06:05:32 2001

Date: Tue, 21 Aug 2001 03:05:07 -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: <998388307-v10-i1575@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Tue, 21 Aug 2001     Volume: 10 Number: 1575

Today's topics:
        Basic question about "closedir" <eliben@il.ibm.comNOSPAM>
    Re: Basic question about "closedir" <ron@savage.net.au>
    Re: Basic question about "closedir" <eliben@il.ibm.comNOSPAM>
    Re: Basic question about "closedir" <philippe.perrin@sxb.bsf.alcatel.fr>
        File handle persistance across fork() ? <andrew.speer+usenet@isolutions.com.au>
    Re: Help with Piped command, capturing output <mbudash@sonic.net>
        How do I sort a 2D-array by three different columns wit (ED)
    Re: How do I sort a 2D-array by three different columns <bcaligari@fireforged.com>
    Re: How do I sort a 2D-array by three different columns <samneric@tigerriverOMIT-THIS.com>
    Re: How to upload files <ron@savage.net.au>
    Re: new modules - looking for (sort-of-)pre-release inp <simon.andrews@bbsrc.ac.uk>
        Passing file handles to subroutines <eliben@il.ibm.comNOSPAM>
    Re: Passing file handles to subroutines (Rafael Garcia-Suarez)
    Re: Passing file handles to subroutines <eliben@il.ibm.comNOSPAM>
    Re: Passing file handles to subroutines (Rafael Garcia-Suarez)
    Re: Perl OO needs the opposite of SUPER:: (Chris Fedde)
    Re: Perl script to update smbpasswd (Rafael Garcia-Suarez)
    Re: select question <philippe.perrin@sxb.bsf.alcatel.fr>
    Re: SQL group by statement? (Malcolm Dew-Jones)
    Re: SQL group by statement? <andy_yew@iocsasia.com>
        subroutines in @INC ? (Rafael Garcia-Suarez)
    Re: subroutines in @INC ? <Tassilo.Parseval@post.rwth-aachen.de>
    Re: subroutines in @INC ? (Rafael Garcia-Suarez)
    Re: Warning using copy from File::Copy <Tassilo.Parseval@post.rwth-aachen.de>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Tue, 21 Aug 2001 08:59:57 +0200
From: Eli Bendersky <eliben@il.ibm.comNOSPAM>
Subject: Basic question about "closedir"
Message-Id: <3B8206ED.70A5AC53@il.ibm.comNOSPAM>

    Hi,

When I want to close a directory with closedir, do I have
to make sure it was opened. eg:

Must I do:

closedir (DIR) if defined(DIR);

Or will:

closedir (DIR);

suffice ?

Also, where can I find the FAQ for this newsgroup ?

Thanks in advance

--
Eli Bendersky




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

Date: Tue, 21 Aug 2001 17:32:26 +1000
From: "Ron Savage" <ron@savage.net.au>
Subject: Re: Basic question about "closedir"
Message-Id: <8bog7.314$vI2.11302@ozemail.com.au>

Eli

See below.

--
Cheers
Ron Savage
ron@savage.net.au
http://savage.net.au/index.html
Eli Bendersky <eliben@il.ibm.comNOSPAM> wrote in message news:3B8206ED.70A5AC53@il.ibm.comNOSPAM...
>     Hi,
>
> When I want to close a directory with closedir, do I have
> to make sure it was opened. eg:
>
> Must I do:
>
> closedir (DIR) if defined(DIR);
>
> Or will:
>
> closedir (DIR);
>
> suffice ?
>
> Also, where can I find the FAQ for this newsgroup ?

Ideally you will have done

opendir(INX, 'some dir') || die("Can't opendir(some dir): $!");

Hence by the time you hit closedir, you _know_ opendir worked...

So a simple closedir(INX) is all that is needed.






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

Date: Tue, 21 Aug 2001 10:46:10 +0200
From: Eli Bendersky <eliben@il.ibm.comNOSPAM>
Subject: Re: Basic question about "closedir"
Message-Id: <3B821FD2.91B630CC@il.ibm.comNOSPAM>

> Ideally you will have done
>
> opendir(INX, 'some dir') || die("Can't opendir(some dir): $!");
>
> Hence by the time you hit closedir, you _know_ opendir worked...
>
> So a simple closedir(INX) is all that is needed.

Yeah, I realize that... but still I have a situation where
I don't really know if the directory was opened (because
I open several directories on each iteration of a loop,
and opening some depends upon successful opening
of the ones preceding them).

--
Eli Bendersky




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

Date: Tue, 21 Aug 2001 10:02:53 +0200
From: Philippe PERRIN <philippe.perrin@sxb.bsf.alcatel.fr>
Subject: Re: Basic question about "closedir"
Message-Id: <3B8215AD.6282EAB8@sxb.bsf.alcatel.fr>

Eli Bendersky wrote:
> Yeah, I realize that... but still I have a situation where
> I don't really know if the directory was opened (because
> I open several directories on each iteration of a loop,
> and opening some depends upon successful opening
> of the ones preceding them).

perldoc -f closedir :
Closes a directory opened by C<opendir()> and returns the success of
that
system call.

It implies that the call to closedir() can fail, and therefore, the
worst you can get is a non-true return value. So yes, you can call this
function without making your script stop.

-- 
PhP

($r1,$r2,$r3,$r4)=("19|20","0|1","28|29","5|24");($r5,$r6)=("9|10|15|16|$r1|$r2","9|10|$r3");%h=("1|",$r6,"1=","[1-5]|2[0-4]","1/","0|19","1\\","6|25","2|","0|6|19|25|$r6","2/","1|20","2\\",$r4,"3|","$r2|6|$r1|25|$r6","3/",$r4,"4|","$r2|$r1|$r6","4=","2|3|4|11|12|13|14|21|22|23","4/",$r4,"4\\",15,"5|","$r2|9|15|$r1|20|$r3","5/",10,"6|",$r5,"7|",$r5,"7/",$r3);for($l=1;$l<8;$l++){b:for($i=0;$i<30;$i++){c:foreach(keys
%h){next c if(!(/^$l(.*)$/));$a=$1;if($i=~/^($h{$_})$/){print $a;next
b;}}print " ";}print "\n";}


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

Date: Tue, 21 Aug 2001 14:23:04 +0930
From: Andrew Speer <andrew.speer+usenet@isolutions.com.au>
Subject: File handle persistance across fork() ?
Message-Id: <3b81e931@duster.adelaide.on.net>

Hi,

I have always wondered what is the proper way to treat filehandles that are 
have been the subject of a fork() on *nix systems (Linux particularly), as 
per the simplified example below:



use IO::File;
 
my $temp_fh=IO::File->new('temp.log', O_WRONLY|O_CREAT|O_TRUNC) ||
        die ("unable to open temp file, $!");

print $temp_fh "$$ parent\n";
 
if (my $pid=fork()) {
        print $temp_fh "$$ parent spawned child $pid\n";
        wait();
}
else {
        print $temp_fh "$$ child\n";
        sleep 2;
        exit 0;
}
 
print $temp_fh  "$$ done\n";

$temp_fh->close();


Which produces the output:

835 parent
836 child
835 parent spawned child 836
835 done

Have I just been extremely lucky that the child pid did not tromp all over 
the log file output, or is this the designed behaviour ?

If the above snippet is considered "poor" in terms of its treatment of 
filehandles across processes, is there a "good" way ? Do I need to lock the 
log file across pid's, or should I be using a different log file for each 
pid (blech..) ?  

And for bonus points - what happens if the child gives up the privileges 
that were needed to open the log file anywhay (eg changes from "root" -> 
"nobody" in the child for example where the log file is in a dir where 
"nobody" is not allowed to write) ? Or is it a case that since the file is 
already open, OS privileges are now moot ?

To put this into context, the "real world" example where I would like to do 
this is in an Apache mod_perl module, where the pseudo code looks something 
like this:

my uber_module;
use blah, blah, blah;
open log file;
sub handler {
        do stuff
        write to log file
}

If the "uber_module" is loaded in httpd.conf via a "PerlModule uber_module" 
statement, then Apache will load it with the "master" httpd process which 
is running under the "root" uid. The module handler will then be called by 
lots of "child" httpd processes which run under the "apache" uid.

I do not quite "get" what happens there - I believe that there is one 
version of the module loaded into memory, and that it is shared across all 
processes. This is why I am asking the question - I want to make sure I try 
and do the "right" thing.

Although I have mentioned Apache here, I a posting this to c.l.p.m because 
I believe that this is a question that applies to "generic" perl as in the 
code snipped above.

And thanks to anyone who may mention using Syslog for the above purpose. I 
am aware of Syslog, but would like to avoid the use of it in this 
particular peice of code.

Any information or advice would be appreciated ...

Thanks,

Andrew Speer






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

Date: Tue, 21 Aug 2001 04:50:32 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Help with Piped command, capturing output
Message-Id: <mbudash-008F99.21503120082001@news.sonic.net>

In article <4d6fd5fc.0108200828.f61ce5b@posting.google.com>, 
dawfun@seanet.com (Jonathan Cunningham) wrote:

> Michael Budash <mbudash@sonic.net> wrote in message 
> news:<mbudash-060499.16190617082001@news.sonic.net>...
> 
> > 
> > open2 is the way. here's a snippet to get you started:
> > 
> > # We need a library function
> >    use IPC::Open2;
> > 
> > # Set up the pgp command
> >   $pgpcmd = "/full/path/to/pgp -feast -m -z $passphrase $pubKey";
> > 
> > # Open the PGP program for bidirectional I/O
> >    $pid = open2(\*READPGP, \*WRITEPGP, $pgpcmd) or die("oops!: $!");
> > 
> > # Send data to be encrypted to PGP
> >    print WRITEPGP $text_to_encrypt;
> > 
> > # Encrypt the data
> >    close(WRITEPGP);
> > 
> > # Get the encrypted data from PGP
> >    $encrypted_text = do { local $/; <READPGP> };
> >    close(READPGP);
> > 
> > hth-
> 
> Excellent suggestion...the explaination of Open2 was very clear,
> however the script hangs (deadlocks?) immediately after the PGP
> program starts...right when it normally would return "Passphrase is
> good" it just sits there.  :(
> 
> Here is your example applied to my code:
> 
>   use strict;
>   use IPC::Open2;
> 
>   my $passphrase = shift;
>   my $pubKey = shift;
>   my $message = shift;
> 
> # Set up the pgp command
>   my $pgpcmd = "pgp -feast -m -z $passphrase $pubKey";
> 
> # Open the PGP program for bidirectional I/O
>   my $pid = open2(\*READPGP, \*WRITEPGP, $pgpcmd) or die("oops!: $!");
> 
> # Send data to be encrypted to PGP
>   print WRITEPGP $message;
> 
> # Encrypt the data
>   close(WRITEPGP);
> 
> # Get the encrypted data from PGP
>   my $encrypted_text = do { local $/; <READPGP> };
>   close(READPGP);
>   print $encrypted_text;
> 
> 
> Any idears on what might be causing the hang (and better still, how to
> get around it)?
> 
> Thanks again,
> 
> Jonathan

hmmm... you might try appending " 2>&1" to $pgpcmd and see what you 
get... also: it may not be able to find your config file: as i recall, i 
had to put it (pgp.cfg) in the same dir as the script with this is in it:

PubRing="/full/path/to/pubring.pkr"
Verbose=2
 
this was with pgp 5.x for irix 6.x, so YMMV...

hth-
-- 
Michael Budash ~~~~~~~~~~ mbudash@sonic.net


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

Date: 20 Aug 2001 22:27:29 -0700
From: gineric2@mindspring.com (ED)
Subject: How do I sort a 2D-array by three different columns with perl
Message-Id: <88105013.0108202127.768e0fb4@posting.google.com>

I need a way to sort a 2D-array by one column, then by a second, and
finally by a third.  This is similar to the sort function found in
Excel.

I know how to sort by a single column using:
@sorted_result = sort { $a->[5] cmp $b->[5] } @result;
where 5 is the column to sort the rows by.  However, how do I expand
on this to then sort by a second and third column without comprimising
the original sort?

I have been thinking about breaking up the first sort results into new
arrays and then sorting them by the second column and so forth.

Is there an easier way to do this?

Thanks for any help.


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

Date: Tue, 21 Aug 2001 08:06:23 -0000
From: "Brendon Caligari" <bcaligari@fireforged.com>
Subject: Re: How do I sort a 2D-array by three different columns with perl
Message-Id: <9lstdj024ot@enews2.newsguy.com>


"ED" <gineric2@mindspring.com> wrote in message
news:88105013.0108202127.768e0fb4@posting.google.com...
> I need a way to sort a 2D-array by one column, then by a second, and
> finally by a third.  This is similar to the sort function found in
> Excel.
>
> I know how to sort by a single column using:
> @sorted_result = sort { $a->[5] cmp $b->[5] } @result;
> where 5 is the column to sort the rows by.  However, how do I expand
> on this to then sort by a second and third column without comprimising
> the original sort?

you can join the comparisons with 'or'.

cmp / <=> return 0 (false) if the comparison matches, -1 or 1 (true) if the
match fails (i.e. larger or smaller)

 (sort {
   ($a->[0] cmp $b->[0]) ||
   ($a->[1] <=> $b->[1]) ||
   ($a->[2] cmp $b->[2])
}@arr);

B





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

Date: Tue, 21 Aug 2001 02:37:25 -0400
From: Samneric <samneric@tigerriverOMIT-THIS.com>
Subject: Re: How do I sort a 2D-array by three different columns with perl
Message-Id: <MPG.15ebcb9d4e58b9fe98968e@news.usit.net>

ED wrote:
> I need a way to sort a 2D-array by one column, then by a second, and
> finally by a third.  This is similar to the sort function found in
> Excel.
> I know how to sort by a single column using:
> @sorted_result = sort { $a->[5] cmp $b->[5] } @result;
> where 5 is the column to sort the rows by.  However, how do I expand
> on this to then sort by a second and third column without comprimising
> the original sort?

@sorted_result = sort {
   $a->[0] cmp $b->[0] or   # if equal, sort by 2nd element
   $a->[1] cmp $b->[1] or   # if STILL equal, sort by 3rd element
   $a->[2] cmp $b->[2] } @LoL;                       


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

Date: Tue, 21 Aug 2001 17:35:51 +1000
From: "Ron Savage" <ron@savage.net.au>
Subject: Re: How to upload files
Message-Id: <meog7.315$vI2.11905@ozemail.com.au>

Do you wish to use a CGI script to upload?

If so, see tut 37: http://savage.net.au/Perl-tutorials.html


--
Cheers
Ron Savage
ron@savage.net.au
http://savage.net.au/index.html
Fan Hongtao <fanh@mail.noh.gov> wrote in message news:3B81166B.EB1186F1@mail.noh.gov...
> I am trying to upload files to a web server outside, The pm that I am
> using is socket and html.   But it always give me the error message as
> file is empty? what should I do?
>
> Thanks
>
> Hongtao
>




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

Date: Tue, 21 Aug 2001 09:12:32 +0100
From: Simon Andrews <simon.andrews@bbsrc.ac.uk>
Subject: Re: new modules - looking for (sort-of-)pre-release input
Message-Id: <3B8217F0.B630A00C@bbsrc.ac.uk>

[cc'd to original poster]

Sean Quinlan wrote:
> 
> Thanks in advance to anyone who takes the time to look this over!
> 
> I'm in the process of  'creating' a few modules that I will be or have
> uploaded to CPAN and hope to have listed in the modules list. They are
> currently developed under the CompBio namespace. 

[snip]

> I also welcome any suggestions for appropriate additions to these
> modules, modules that would fit well in this group, and/or if interested
> in collaborating on this project. Please feel free to e-mail me at
> seanq@darwin.bu.edu.

Sean,

You asked for suggestions for what to do with these modules, you seem to
have done quite a bit already! If you are interested, my suggestion
would be to integrate some of the stuff you are doing into the existing
bioperl project (www.bioperl.org).  They already have modules to perform
some of the tasks that yours do, as well as many you may be considering
writing.  They are always keen to get more people involved, and you may
find that many of the tools you would find useful are already available
(there is already a Bioperl DB module for instance).  If nothing else,
then their code is free, so you can save yourself some development!

Bioperl is a pretty big project, and is the backend for large projects
such as www.ensembl.org.  You can look through their latest release
either by getting from their site, or from CPAN 

(http://search.cpan.org/search?mode=module&query=bioperl)


I hope this is of interest

	TTFN

	Simon.


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

Date: Tue, 21 Aug 2001 10:21:01 +0200
From: Eli Bendersky <eliben@il.ibm.comNOSPAM>
Subject: Passing file handles to subroutines
Message-Id: <3B8219EC.3792AE42@il.ibm.comNOSPAM>

    Hi,

Can I just pass a file handle to a subroutine like
I do with any other variable ?

--
Eli Bendersky




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

Date: 21 Aug 2001 07:35:16 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Passing file handles to subroutines
Message-Id: <slrn9o4440.65j.rgarciasuarez@rafael.kazibao.net>

Eli Bendersky wrote in comp.lang.perl.misc:
> 
> Can I just pass a file handle to a subroutine like
> I do with any other variable ?

Yes. perlsub gives this example, that uses a typeglob :

	splutter(\*STDOUT);
	sub splutter {
	   my $fh = shift;
	   print $fh "her um well a hmmm\n";
	}

You can also use prototypes (new style, cleaner to the eye) :

	sub splutter (*) {
	   my $fh = shift;
	   print $fh "her um well a hmmm\n";
	}
	splutter STDOUT;

The perlsub doc has all the details.

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/


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

Date: Tue, 21 Aug 2001 10:49:11 +0200
From: Eli Bendersky <eliben@il.ibm.comNOSPAM>
Subject: Re: Passing file handles to subroutines
Message-Id: <3B822087.B7143742@il.ibm.comNOSPAM>

> Eli Bendersky wrote in comp.lang.perl.misc:
> >
> > Can I just pass a file handle to a subroutine like
> > I do with any other variable ?
>
> Yes. perlsub gives this example, that uses a typeglob :
>
>         splutter(\*STDOUT);
>         sub splutter {
>            my $fh = shift;
>            print $fh "her um well a hmmm\n";
>         }

Why do you say \*STDOUT ? Can't you just say STDOUT ?

What is shift ? Is it like saying:

my $fh = $_[0];

?

Thanks in advance

--
Eli Bendersky




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

Date: 21 Aug 2001 08:08:17 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Passing file handles to subroutines
Message-Id: <slrn9o461u.6ae.rgarciasuarez@rafael.kazibao.net>

Eli Bendersky wrote in comp.lang.perl.misc:
: > > Can I just pass a file handle to a subroutine like
: > > I do with any other variable ?
: >
: > Yes. perlsub gives this example, that uses a typeglob :
: >
: >         splutter(\*STDOUT);
: >         sub splutter {
: >            my $fh = shift;
: >            print $fh "her um well a hmmm\n";
: >         }
: 
: Why do you say \*STDOUT ? Can't you just say STDOUT ?

Well, in fact you can... if you don't "use strict;".
Thats why \*STDOUT (taking a reference on the typeglob) is the
recommended idiom.

: What is shift ? Is it like saying:
: 
: my $fh = $_[0];

More or less, in this context, yes: this puts the first element of @_
into $fh -- but this also shifts it off from @_. See the documentation
for shift() in perlfunc. (Learn to use the Perl standard documentation:
it is very helpful.)

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/


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

Date: Tue, 21 Aug 2001 04:46:36 GMT
From: cfedde@fedde.littleton.co.us (Chris Fedde)
Subject: Re: Perl OO needs the opposite of SUPER::
Message-Id: <MIlg7.347$V3.190639616@news.frii.net>

In article <a73bcad1.0108201810.6ee8fb4a@posting.google.com>,
John Lin <johnlin@chttl.com.tw> wrote:
>Ren Maddox wrote
>> On 18 Aug 2001, John Lin wrote:
>> > But, with current Perl's restriction, using your solution:
>> > 
>> > Report::
>> >     sub  pre_gen;             # You need to modify the original class
>> >     sub      gen;             # breaking down subroutines into pieces
>> >     sub post_gen;             # which I consider to be ugly  :)
>> >     sub  pre_save_as_html;
>> >     sub      save_as_html;
>> >     sub post_save_as_html;
>> >     sub  pre_print_to_printer;
>> >     sub      print_to_printer;
>> >     sub post_print_to_printer;
>> > 
>> [snipped ExtendedReport skeleton]
>> 
>> At first blush, I'm not visualizing why you would need all of this.
>> If you are expecting virtual report to handle everything, then
>> pre_report and post_report can handle it.  There is no need for
>> pre_gen, post_gen, etc.
>
>Sorry, I meant:
>
>sub gen {
>    $self->pre_report_in_gen;
>    $self->report;
>    $self->post_report_in_gen;
>}
>
>and also
>pre_report_in_save_as_html;
>post_report_in_save_as_html;
>pre_report_in_print_to_printer;
>post_report_in_print_to_printer;
>...
>

It seems to me that you are throwing too much behavior into the
report class.  Why does anything inheriting from Report need to
know about HTML? or any other output format for that matter.  The
report object has report like atributes like parts and  headings
and items and subheadings and sums and other such data.  An
output class would translate this into an output format.

    my $report = Inherits::From::Report->new(%subject_data);
    my $report->header(%HeaderParameters);
    my $report->footer(%FooterParameters);

    ...

    print STDOUT Output->as_HTML($report);

    my $p = Output->print(
	$report,
    	printer => 'robs_color_laser_jet',
    );

Because all the Report objects adhere to the interface then the Output class
just has to be aware of that interface and do the right thing with it.

Am I still missing something?
-- 
    This space intentionally left blank


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

Date: 21 Aug 2001 07:15:37 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: Perl script to update smbpasswd
Message-Id: <slrn9o42v5.62i.rgarciasuarez@rafael.kazibao.net>

Mr. Chow Wing Siu wrote in comp.lang.perl.misc:
> 
> I would like to know if there's any perl script to update
> samba's smbpasswd directly?  Thanks in advance.

Why don't you use smbpasswd(8) directly ? (with the -s option: be silent
and read passwords from stdin). Perl is also a glue language.

-- 
Rafael Garcia-Suarez / http://rgarciasuarez.free.fr/


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

Date: Tue, 21 Aug 2001 08:47:16 +0200
From: Philippe PERRIN <philippe.perrin@sxb.bsf.alcatel.fr>
Subject: Re: select question
Message-Id: <3B8203F4.3DC16837@sxb.bsf.alcatel.fr>

Benjamin Goldberg wrote:
> This should probably go to a unix group, or something, but...
> If I have a pipe [made with, eg, pipe()], and there's no data currently
> in it [ie, the writing end is writable, but the reading end is not
> readable, and would block, 'cause there's nothing there to read], and I
> close the writing end, does that cause the reading end's state to change
> to be "readable" or "exceptional"?

The following deals with UNIX pipes in general, it should be true for
Perl pipes on UNIX platforms too...
If you try to read from an empty pipe whose writable end is not used by
any process (closed), read() will return 0 instantly. Otherwise, a
read() call will block until there's something to read if a process has
opened the writable end.

-- 
PhP

($r1,$r2,$r3,$r4)=("19|20","0|1","28|29","5|24");($r5,$r6)=("9|10|15|16|$r1|$r2","9|10|$r3");%h=("1|",$r6,"1=","[1-5]|2[0-4]","1/","0|19","1\\","6|25","2|","0|6|19|25|$r6","2/","1|20","2\\",$r4,"3|","$r2|6|$r1|25|$r6","3/",$r4,"4|","$r2|$r1|$r6","4=","2|3|4|11|12|13|14|21|22|23","4/",$r4,"4\\",15,"5|","$r2|9|15|$r1|20|$r3","5/",10,"6|",$r5,"7|",$r5,"7/",$r3);for($l=1;$l<8;$l++){b:for($i=0;$i<30;$i++){c:foreach(keys
%h){next c if(!(/^$l(.*)$/));$a=$1;if($i=~/^($h{$_})$/){print $a;next
b;}}print " ";}print "\n";}


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

Date: 20 Aug 2001 22:54:42 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: SQL group by statement?
Message-Id: <3b81f7a2@news.victoria.tc.ca>

Yew (andy_yew@iocsasia.com) wrote:
: Is there a SQL group by statement and count functions using regex in perl?
: Forgive me if this is a newbie question but I'm mucking around looking for
: answers to this.

: AAH2-6-pw-100     PPKBSBAO     6     2.8 M          PIPE
: AAH2-6-pw-100     EAKBSBAO     6     2                 FITTINGS

: This is a sample raw data that I have. I would like to do a perl equivalent
: of a select statement and then
: - group them by the second column
: - sum the third column value
: - and sort (but this is not as essential)

: Problem I'm facing is that this output file from the program dumps the data
: not in tab,comma delimited format, but just fills them up accordingly with
: whitespaces (space) only. I tried looking at some common cgi scripts but
: have come up short.

: Any help would be appreciated, even if it were just some pointers. Thanks.

: Andy

Mass confusion.

If the data is in a file, (or is the output from another program, same
difference)  then perl could easily read the data line by line.  It is
usually easy to use a regular expression to break the data into its
fields.

The fields would often be saved in a hash so each field can be accessed by
name.

The hash is often pushed or otherwise added into an arry (of hashes).

That array is easy to sort.

It is also easy to scan through the data using a hash to store category
specific values, totals, etc.

e.g.

	my @field_names = qw( f1 f2 f3 f4 );

	my @rows;

	while (<>)	# read lines from the files passed as arguments
	{
		my %h;
		if (! ( @h{ @fields_names } 
		 	= m/(regular) (expression) (goes) (here)/ ) )
		{ warn "Unparsed row: $_";
		  next;
		}
		push @rows , \%h;
	}

	@sorted_rows = sort { sort functionality } @rows;

	my $grand_total = 0;
	my $categories  = 0;
	my $prev_category = '';
	my %alt_categories;
	foreach $row (@sorted_rows)
	{
		$grand_total += $row->{'f3'} ; # total up field three;	

		# old fashioned merge style grouping
		$categories += 1 if $prev_category ne $row->{'f1'} ;
		$prev_category = $row->{'f1'} ;

		# general perl technique, use the power of hashes
		$alt_categories{$row->{'f1'}} ++;

	}

	print "There were $categories categories.\n";
	print "The sum of field f3 was $grand_total\n;

	print "the number of categories calculated using the alternate\n";
	print "(and easier method in perl) was" , 
		scalar keys %alt_categories , "\n";

etc etc etc


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

Date: Tue, 21 Aug 2001 14:18:04 +0800
From: "Yew" <andy_yew@iocsasia.com>
Subject: Re: SQL group by statement?
Message-Id: <9lsu36$358$1@clematis.singnet.com.sg>

Thanks.

You're right, the item in question is gotten from a flat file, so there isnt
any need to parse calls to a db or whatever.

I'll try your recommendations and see what I can come up with.

Andy




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

Date: 21 Aug 2001 09:12:58 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: subroutines in @INC ?
Message-Id: <slrn9o49r6.6l5.rgarciasuarez@rafael.kazibao.net>

I can't find anymore in the docs where is documented the use of
subroutines in @INC. In fact I don't remember if this is documented at
all. I think this feature was introduced in Perl 5.6.0. Any help?

-- 
use lib sub{$_=$_[1];y;_.mp; ,\n;d;print;
open+0;*0};require Just_another_Perl_hacker;1


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

Date: Tue, 21 Aug 2001 11:25:34 +0200
From: Tassilo von Parseval <Tassilo.Parseval@post.rwth-aachen.de>
Subject: Re: subroutines in @INC ?
Message-Id: <3B82290E.1020401@post.rwth-aachen.de>

Rafael Garcia-Suarez wrote:

> I can't find anymore in the docs where is documented the use of
> subroutines in @INC. In fact I don't remember if this is documented at
> all. I think this feature was introduced in Perl 5.6.0. Any help?

Oh, it is documented alright. :-)

Look that up in perlvar.pod. In short: @INC contains the places where 
the Perl interpreter will look for modules that have been included in 
your script via do(), require() or use(). The docs also tell you how you 
can cleanly manipulate @INC.


Tassilo--
$a=[(74,116)];$b=[($a->[1]-1,$a->[1]++,0x20)];$c=[(97,110)];$d=[($c->
[1]+1,$b->[1],"her")];for(@{[$a,$b,$c,$d]}){for(@{$_}){$_=~/\d+/?print
(chr($_)):print;}}$c=sub{$l=shift;[(0x20+$l-1,0x50,0x65,0x73-0x01,108
),(0x20,0x68,0x61,)]};print(map{chr($_)}@{($c->(1))});$h={a=>33*3,b=>
10**2+7,c=>"1"."0"."1",d=>0162};@h=sort(keys(%$h));for(@h){print(chr(
ord(chr($h->{$_}))))};



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

Date: 21 Aug 2001 09:34:37 GMT
From: rgarciasuarez@free.fr (Rafael Garcia-Suarez)
Subject: Re: subroutines in @INC ?
Message-Id: <slrn9o4b3p.6pt.rgarciasuarez@rafael.kazibao.net>

Tassilo von Parseval wrote in comp.lang.perl.misc:
} 
} > I can't find anymore in the docs where is documented the use of
} > subroutines in @INC. In fact I don't remember if this is documented at
} > all. I think this feature was introduced in Perl 5.6.0. Any help?
} 
} Look that up in perlvar.pod. In short: @INC contains the places where 
} the Perl interpreter will look for modules that have been included in 
} your script via do(), require() or use(). The docs also tell you how you 
} can cleanly manipulate @INC.

Yes I know ;-) perlvar describes that @INC contains names of
directories. But @INC may also contain references to subroutines that
return a filehandle from which the module should be read. Look at my
sig. I grep'd the pods but I can't find a reference for this.

-- 
use lib sub{$_=$_[1];y;_.mp; ,\n;d;print;
open+0;*0};require Just_another_Perl_hacker;1


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

Date: Tue, 21 Aug 2001 08:10:26 +0200
From: Tassilo von Parseval <Tassilo.Parseval@post.rwth-aachen.de>
Subject: Re: Warning using copy from File::Copy
Message-Id: <3B81FB52.7020700@post.rwth-aachen.de>

Nomade wrote:

> Hi all
> 
> I do a foreach loop in an array with file names, for each one i have
> to copy to some other place, but after the 2nd interarion of the loop
> i get the following message :
> 
> Use of uninitialized value at /usr/lpp/ssp/perl5/lib/File/Copy.pm line
> 84.
> 
> 
> However, all files get copied with no major problems...
> 
> But how can i avoid that message ????

It is a warning hence you could add a signalhandler for that.

$SIG{__WARN__} = sub { }; # will make them vanish

see 'perldoc -q undefined.*variables' for a bit on that.


Tassilo

-- 
$a=[(74,116)];$b=[($a->[1]-1,$a->[1]++,0x20)];$c=[(97,110)];$d=[($c->
[1]+1,$b->[1],"her")];for(@{[$a,$b,$c,$d]}){for(@{$_}){$_=~/\d+/?print
(chr($_)):print;}}$c=sub{$l=shift;[(0x20+$l-1,0x50,0x65,0x73-0x01,108
),(0x20,0x68,0x61,)]};print(map{chr($_)}@{($c->(1))});$h={a=>33*3,b=>
10**2+7,c=>"1"."0"."1",d=>0162};@h=sort(keys(%$h));for(@h){print(chr(
ord(chr($h->{$_}))))};



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

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 1575
***************************************


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