[19669] in Perl-Users-Digest
Perl-Users Digest, Issue: 1864 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 3 18:05:55 2001
Date: Wed, 3 Oct 2001 15:05:12 -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: <1002146711-v10-i1864@ruby.oce.orst.edu>
Content-Type: text
Perl-Users Digest Wed, 3 Oct 2001 Volume: 10 Number: 1864
Today's topics:
Re: $x =~ s/search/replace/ on temporary w/o changing $ nobull@mail.com
Re: $x =~ s/search/replace/ on temporary w/o changing $ <mbudash@sonic.net>
append column upon match (newbie) (Yugal Sharma)
Re: append column upon match (newbie) (Malcolm Dew-Jones)
Re: append column upon match (newbie) <mbudash@sonic.net>
Re: append column upon match (newbie) <skuo@mtwhitney.nsc.com>
Re: append column upon match (newbie) <jeff@vpservices.com>
Re: carriage return <goldbb2@earthlink.net>
Re: CPAN module and local directory <randy@theoryx5.uwinnipeg.ca>
Crypt::OpenPGP (GL)
Re: filehandles between functions (Mark Jason Dominus)
Re: filehandles between functions <uri@sysarch.com>
Re: func prototype enforcement with sub refs <pne-news-20011003@newton.digitalspace.net>
Help <jclover@nati.org>
Re: How to add date stamp to a log file (Venkatesh Babu Sira)
Re: How to add date stamp to a log file <mbudash@sonic.net>
Re: Joining 3-D arrays nobull@mail.com
Re: Passing arguments to subs <darkon@one.net>
patches to Net::* (James O'Brien)
Re: Pattern Matching <goldbb2@earthlink.net>
Re: Pattern Matching <goldbb2@earthlink.net>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: 03 Oct 2001 18:49:19 +0100
From: nobull@mail.com
Subject: Re: $x =~ s/search/replace/ on temporary w/o changing $x
Message-Id: <u9lmiszk9s.fsf@wcl-l.bham.ac.uk>
johngooglegroups@kumpf.org (anonimus cowerd) finds me (Nobull) in an
uncharacteristically good mood when writes:
> I tried to search deja (google) on this, but coming up with search terms
> was quite difficult.
Yes, I know. But I have to say your subject line is really rather
good and will greatly help people search in future.
A lot better I think than the last time I saw this question "perl,
lvalues, rvalues, and operators" (2 weeks ago).
Probably even better than John Lin's classic "Idiom: the expression of
a copied & substituted string" (March 2001). The ensuing thread was,
I think, the best treatment of this question I've seen here.
In fact I strongly recommend going to Google and browsing John's
contributions to this newsgroup as he has asked (clearly and consisely)
many of the questions that are not in the FAQ but which a Perl learner
of above average ability will find they want to ask.
johngooglegroups@kumpf.org (anonimus cowerd) writes:
> I suppose i could do this;
>
> some_func scalar (($tmp=$x) =~ s/some/another,$tmp)
>
> but it's still awkward.
It also leaves $tmp existing...
some_func do { (my $tmp=$x) =~ s/some/another/; $tmp };
Or if you value beauty over speed...
some_func apply { s/some/another/ } $x;
...where apply() is as defined (by me, Nobull) in the aforementioned
John Lin thread.
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 03 Oct 2001 18:28:45 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: $x =~ s/search/replace/ on temporary w/o changing $x
Message-Id: <mbudash-254F8A.11285103102001@news.sonic.net>
In article <u9lmiszk9s.fsf@wcl-l.bham.ac.uk>, nobull@mail.com wrote:
> johngooglegroups@kumpf.org (anonimus cowerd) finds me (Nobull) in an
> uncharacteristically good mood when writes:
>
> > I tried to search deja (google) on this, but coming up with search terms
> > was quite difficult.
>
> Yes, I know. But I have to say your subject line is really rather
> good and will greatly help people search in future.
>
> A lot better I think than the last time I saw this question "perl,
> lvalues, rvalues, and operators" (2 weeks ago).
>
> Probably even better than John Lin's classic "Idiom: the expression of
> a copied & substituted string" (March 2001). The ensuing thread was,
> I think, the best treatment of this question I've seen here.
>
> In fact I strongly recommend going to Google and browsing John's
> contributions to this newsgroup as he has asked (clearly and consisely)
> many of the questions that are not in the FAQ but which a Perl learner
> of above average ability will find they want to ask.
> johngooglegroups@kumpf.org (anonimus cowerd) writes:
>
> > I suppose i could do this;
> >
> > some_func scalar (($tmp=$x) =~ s/some/another,$tmp)
> >
> > but it's still awkward.
>
> It also leaves $tmp existing...
>
> some_func do { (my $tmp=$x) =~ s/some/another/; $tmp };
>
> Or if you value beauty over speed...
>
> some_func apply { s/some/another/ } $x;
>
> ...where apply() is as defined (by me, Nobull) in the aforementioned
> John Lin thread.
thanks greatly for that tip, nobull. for those who wish to go straight
to your mentioned google area (beware word wrap):
http://groups.google.com/groups?hl=en&threadm=9boir8%242ra%40netnews.hine
t.net&rnum=1&prev=/groups%3Fq%3DIdiom:%2Bthe%2Bexpression%2Bof%26hl%3Den%
26rnum%3D1%26selm%3D9boir8%25242ra%2540netnews.hinet.net
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: 3 Oct 2001 11:22:40 -0700
From: yus_punju@hotmail.com (Yugal Sharma)
Subject: append column upon match (newbie)
Message-Id: <30688683.0110031022.3baabefb@posting.google.com>
Hi,
I've been struggling with this one for a while. I'd really appreciate
any thoughts I could get on it.
Assuming I have a data_file that looks like this (space delimited):
1AKK LYS 27 88 CA 3.7847 NZ 10.1088
1AKK LYS 72 73 CA 3.8272 NZ 13.8547
1AKK LYS 99 100 CA 3.8160 NZ 10.6881
.
.
And I have an input_file that looks like this (space delimited):
72 73 16.4(BS3)
27 34 6.4(erg)
99 100 4.3(gsh)
What I would like to do is compare the 1st and 2nd fields of the
input_file to the 3rd and 4th fields of the data_file on a
line-by-line basis. If there is a match (on both fields), append the
third field of the input_file as a last field to the data_file. If
there is no match, do not append anything.
So, for example, comparing the input_file to the data_file above would
yield the final_file:
1AKK LYS 27 88 CA 3.7847 NZ 10.1088
1AKK LYS 72 73 CA 3.8272 NZ 13.8547 16.4(BS3)
1AKK LYS 99 100 CA 3.8160 NZ 10.6881 4.3(gsh)
.
.
All my attempts so far have failed miserably. Thanks for your help in
advance!
Best regards,
Yugal
------------------------------
Date: 3 Oct 2001 12:31:24 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: append column upon match (newbie)
Message-Id: <3bbb678c@news.victoria.tc.ca>
Yugal Sharma (yus_punju@hotmail.com) wrote:
: Hi,
: I've been struggling with this one for a while. I'd really appreciate
: any thoughts I could get on it.
: Assuming I have a data_file that looks like this (space delimited):
: 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
: 1AKK LYS 72 73 CA 3.8272 NZ 13.8547
: 1AKK LYS 99 100 CA 3.8160 NZ 10.6881
: .
: .
: And I have an input_file that looks like this (space delimited):
: 72 73 16.4(BS3)
: 27 34 6.4(erg)
: 99 100 4.3(gsh)
: What I would like to do is compare the 1st and 2nd fields of the
: input_file to the 3rd and 4th fields of the data_file on a
: line-by-line basis. If there is a match (on both fields), append the
: third field of the input_file as a last field to the data_file. If
: there is no match, do not append anything.
: So, for example, comparing the input_file to the data_file above would
: yield the final_file:
: 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
: 1AKK LYS 72 73 CA 3.8272 NZ 13.8547 16.4(BS3)
: 1AKK LYS 99 100 CA 3.8160 NZ 10.6881 4.3(gsh)
: .
: .
The following is *untested*, and it tests a *single* field from each file.
You would need to extent this to handle the two fields. Probably the hash
should have two levels of keys ( $updates{$id1}{$id2} ) and the m// needs
to pull out both fields, but I haven't thought it through.
#!perl
# usage: script data updates
my ($data_file,$update_file) = @ARGV;
open DATA , "$data_file" or die $!;
open UPDATES , "$updates_file" or die $!;
my %updates;
while (<UPDATES>)
{
warn("bad format:$.:$_",next
unless my ($id,$rest_of_line) = m/^(\S+)(\s.*)/;
warn("multiple update:$.:$_"),next
if defined $updates{$id};
$updates{$id} = $rest_of_line;
}
# e.g. now we have something like
# %updates = (
# 72 => "73 16.4(BS3)" ,
# 27 => "34 6.4(erg)" ,
# 99 => "100 4.3(gsh)" ,
# );
while (<DATA>)
{
warn("bad format:$.:$_",next
unless my (undef,$id) = m/^(\S+\s+){2}(\S+)/;
if (defined $updates{$id})
{ # $_ is the data, so append the updates, and watch
# out for end of line chars
# maybe you need to remove a previous update also ?
chomp $_;
$_ .= $updates{$id} . "\n";
}
print;
}
($0.03)
------------------------------
Date: Wed, 03 Oct 2001 19:38:33 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: append column upon match (newbie)
Message-Id: <mbudash-D085EB.12383503102001@news.sonic.net>
In article <30688683.0110031022.3baabefb@posting.google.com>,
yus_punju@hotmail.com (Yugal Sharma) wrote:
> Hi,
>
> I've been struggling with this one for a while. I'd really appreciate
> any thoughts I could get on it.
>
> Assuming I have a data_file that looks like this (space delimited):
>
> 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
> 1AKK LYS 72 73 CA 3.8272 NZ 13.8547
> 1AKK LYS 99 100 CA 3.8160 NZ 10.6881
> .
> .
>
> And I have an input_file that looks like this (space delimited):
>
> 72 73 16.4(BS3)
> 27 34 6.4(erg)
> 99 100 4.3(gsh)
>
> What I would like to do is compare the 1st and 2nd fields of the
> input_file to the 3rd and 4th fields of the data_file on a
> line-by-line basis. If there is a match (on both fields), append the
> third field of the input_file as a last field to the data_file. If
> there is no match, do not append anything.
>
> So, for example, comparing the input_file to the data_file above would
> yield the final_file:
>
> 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
> 1AKK LYS 72 73 CA 3.8272 NZ 13.8547 16.4(BS3)
> 1AKK LYS 99 100 CA 3.8160 NZ 10.6881 4.3(gsh)
> .
> .
>
assuming enough ram is available to load both files into memory, here's
one way:
#-------------------------------
use strict;
my $data = 'data_file';
open (DATAFILE, $data) or die ("Can't open $data for reading: $!");
my @data = <DATAFILE>;
close DATAFILE;
my $input = 'input_file';
open (INPUTFILE, $input) or die ("Can't open $input for reading: $!");
my @input = <INPUTFILE>;
close INPUTFILE;
chomp @input;
foreach my $inputrcd (@input) {
my @inputflds = split(/\s+/, $inputrcd);
for my $i (0..$#data) {
$datarcd = $data[$i];
@dataflds = split(/\s+/, $datarcd);
if ( ($inputflds[0] eq $dataflds[2]) &&
($inputflds[1] eq $dataflds[3]) ) {
chomp $datarcd;
$data[$i] = $datarcd . " " . $inputflds[2] . "\n";
}
}
}
open (DATAFILE, ">$data") or die ("Can't open $data for writing: $!");
print DATAFILE @data;
close DATAFILE;
#-------------------------------
you may want to turn the 'for' loops inside out if there are many more
data records than input records.
hope this helps,
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: Wed, 3 Oct 2001 13:32:41 -0700
From: Steven Kuo <skuo@mtwhitney.nsc.com>
Subject: Re: append column upon match (newbie)
Message-Id: <Pine.GSO.4.21.0110031325150.9556-100000@mtwhitney.nsc.com>
On 3 Oct 2001, Yugal Sharma wrote:
> Hi,
>
> I've been struggling with this one for a while. I'd really appreciate
> any thoughts I could get on it.
>
> Assuming I have a data_file that looks like this (space delimited):
>
> 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
> 1AKK LYS 72 73 CA 3.8272 NZ 13.8547
> 1AKK LYS 99 100 CA 3.8160 NZ 10.6881
> .
> .
>
> And I have an input_file that looks like this (space delimited):
>
> 72 73 16.4(BS3)
> 27 34 6.4(erg)
> 99 100 4.3(gsh)
>
> What I would like to do is compare the 1st and 2nd fields of the
> input_file to the 3rd and 4th fields of the data_file on a
> line-by-line basis. If there is a match (on both fields), append the
> third field of the input_file as a last field to the data_file. If
> there is no match, do not append anything.
>
> So, for example, comparing the input_file to the data_file above would
> yield the final_file:
>
> 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
> 1AKK LYS 72 73 CA 3.8272 NZ 13.8547 16.4(BS3)
> 1AKK LYS 99 100 CA 3.8160 NZ 10.6881 4.3(gsh)
> .
> .
Use a hash.
In this example below, a multi-keyed hash (%append) stores the field to be
added from the input_file. Then check for exist(ence) while examining
the data_file...
#! /usr/local/bin/perl
use strict;
use warnings;
my %append;
open (INF,'input_file') or die "Couldn't open input: $!";
while (<INF>) {
my ($key1, $key2, $value) = split;
$append{$key1,$key2} = $value;
}
close INF or die "Couldn't close input: $!";
open (DATAF,'data_file') or die "Couldn't open data file :$!";
while (<DATAF>) {
chomp;
my @fields = split;
if (exists $append{$fields[2],$fields[3]}) {
$_ .= " $append{$fields[2],$fields[3]}";
}
print $_, "\n";
}
close DATAF or die "Could not close data file: $!";
--
Cheers,
Steve
------------------------------
Date: Wed, 03 Oct 2001 13:48:22 -0700
From: Jeff Zucker <jeff@vpservices.com>
Subject: Re: append column upon match (newbie)
Message-Id: <3BBB7996.4957F7F5@vpservices.com>
Michael Budash wrote:
>
> In article <30688683.0110031022.3baabefb@posting.google.com>,
> yus_punju@hotmail.com (Yugal Sharma) wrote:
>
> > Assuming I have a data_file that looks like this (space delimited):
> >
> > 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
> > 1AKK LYS 72 73 CA 3.8272 NZ 13.8547
> > 1AKK LYS 99 100 CA 3.8160 NZ 10.6881
> >
> > And I have an input_file that looks like this (space delimited):
> >
> > 72 73 16.4(BS3)
> > 27 34 6.4(erg)
> > 99 100 4.3(gsh)
> >
> > What I would like to do is compare the 1st and 2nd fields of the
> > input_file to the 3rd and 4th fields of the data_file on a
> > line-by-line basis. If there is a match (on both fields), append the
> > third field of the input_file as a last field to the data_file. If
> > there is no match, do not append anything.
> >
> > So, for example, comparing the input_file to the data_file above would
> > yield the final_file:
> >
> > 1AKK LYS 27 88 CA 3.7847 NZ 10.1088
> > 1AKK LYS 72 73 CA 3.8272 NZ 13.8547 16.4(BS3)
> > 1AKK LYS 99 100 CA 3.8160 NZ 10.6881 4.3(gsh)
>
> assuming enough ram is available to load both files into memory, here's
> one way:
[Snip Michael's fine answer]
Or if you don't want to slurp either file into ram, or want to have a
method that will work for any number of fields, or one that will update
multiple rows, or you want to include flock() protection, use AnyData.
Here is a complete script to do what you want using the AnyData tied
hash interface. If you know SQL you could accomplish the same thing
with DBD::AnyData using SQL. Both are available on CPAN.
use AnyData;
my $main = adTie('CSV',$main_file,'u',{
trim=>1,
field_sep=>' ',
cols=>'c1,c2,c3,c4,c5,c6,c6,c8,c9'
});
my $input = adTie('CSV',$input_file,'r',{
trim=>1,
field_sep=>' ',
cols=>'i1,i2,i3'
});
while (my $row = each %$input) {
$main->{{c3=>$row->{i1},c4=>$row->{i2}}} = {c9=>$row->{i3}};
}
__END__
The result is that your $main_file is exactly as the results you
requested above with.
--
Jeff
------------------------------
Date: Wed, 03 Oct 2001 16:18:06 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: carriage return
Message-Id: <3BBB727E.BB4A9EC3@earthlink.net>
Michael wrote:
>
> "Benjamin Goldberg" <goldbb2@earthlink.net> wrote in message
> news:3BB664A3.9FD73CC1@earthlink.net...
[snip]
> > I would advise against doing roll-your-own. Use a library.
> >
> > use URI::Escape;
> > my $encoded = uri_escape( $textarea );
>
> That module is not included in my library, where can I download
> the code?
Download it from CPAN.
http://www.cpan.org/
Or from one of CPAN's mirrors:
http://www.cpan.org/SITES.html
Or for searching:
http://search.cpan.org
http://theoryx5.uwinnipeg.ca/CPAN/cpan-search.html
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: 3 Oct 2001 19:52:11 GMT
From: Randy Kobes <randy@theoryx5.uwinnipeg.ca>
Subject: Re: CPAN module and local directory
Message-Id: <9pfq9b$dd0$1@canopus.cc.umanitoba.ca>
In comp.lang.perl.misc, Miko O'Sullivan <miko@idocs.com> wrote:
> I know how to install something to a local directory.
> I know how to use the CPAN module (at least minimally).
> I DON'T know how to use the CPAN module to install a module to a local
> directory.
You could put a file MyConfig.pm under $HOME/.cpan/CPAN/ - this
would have the same form as the system's CPAN Config.pm, but
stick in the appropriate Makefile.PL args, as such:
$CPAN::Config = {
'build_cache' => q[10],
.
.
'makepl_arg' => q[PREFIX=/home/me/perllib
INSTALLMAN1DIR=/home/me/man1 INSTALLMAN3DIR=/home/me/man3],
.
.
};
best regards,
randy kobes
------------------------------
Date: 3 Oct 2001 14:23:32 -0700
From: gkl200@yahoo.com (GL)
Subject: Crypt::OpenPGP
Message-Id: <4fe9e2bd.0110031323.53fef7ba@posting.google.com>
I'm trying to set up a Perl script on a linux system to do file
PGP encrypt via a CGI perl script.
When I run the script from the shell the encryption works fine,
but from my browser it fails and gives me:
errstr: "No known recipients for encryption".
the .pgp directory can be read by webserver's user id. and I've tried
using PubRing => $pubring, to point to other keyring directories set to
be readable by the webserver's user id. Any help or hints would
be appreciated. thanks.
Pretty Good Privacy(tm) Version 6.5.8
(c) 1999 Network Associates Inc.
Uses the RSAREF(tm) Toolkit, which is copyright RSA Data Security, Inc.
Export of this software may be restricted by the U.S. government.
#!/usr/local/bin/perl
use Crypt::OpenPGP;
$pubring = "/home/apache/.pgp";
#pubring = "/home/a_user/.pgp";
$file = "/WWW/test/foo.txt";
$key_id = "XYZ <xyz\@domain.com>";
print "Content-type: text/html\n\n";
$pgp = Crypt::OpenPGP->new;
$ciphertext = $pgp->encrypt (
Compat => 'PGP5',
Filename => $file,
PubRing => $pubring,
Recipients => $key_id,
Armour => 1,
) or die "can not encrypt ", $pgp->errstr;
print "$ciphertext\n";
------------------------------
Date: Wed, 03 Oct 2001 19:17:20 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: filehandles between functions
Message-Id: <3bbb6440.1a15$214@news.op.net>
In article <1002110362.18804@itz.pp.sci.fi>,
Ilmari Karonen <usenet11598@itz.pp.sci.fi> wrote:
>Another variant, which works for pipes and other funny things too:
>
> sub slurp ($) {
> my $fh = shift;
> return <$fh> if wantarray;
> local $/;
> return <$fh> unless -s $fh;
> read $fh, my $buf, -s _;
> return $buf;
> }
I just found (to my surprise) that this doesn't always work for pipes.
I wrote a program to make a pipe and fork a child. The child writes
data into the pipe and flushes it, and later exits, but at no time
does (-s PIPE) return anything other than 0 to the parent.
Of course, I may have made an error.
pipe R, W or die;
my $pid = fork;
die unless defined $pid;
if ($pid) { # parent
close W;
for (1..7) {
my $l;
my $b = -s R;
print "parent sees $b bytes waiting\n";
sleep 1;
}
} else { # child
close R;
select W;
$|=1;
sleep 2;
print W "blah!\n";
print STDERR "Child wrote and flushed.\n";
sleep 2;
print STDERR "Child exits.\n";
}
The output is:
parent sees 0 bytes waiting
parent sees 0 bytes waiting
parent sees 0 bytes waiting
Child wrote and flushed.
parent sees 0 bytes waiting
Child exits.
parent sees 0 bytes waiting
parent sees 0 bytes waiting
parent sees 0 bytes waiting
If you add a line to the parent that actually reads R, it gets the
data from the child just fine, so the problem is with the -s operator,
not with the way the pipe is set up. 'stat' yields similar results.
--
@P=split//,".URRUU\c8R";@d=split//,"\nrekcah xinU / lreP rehtona tsuJ";sub p{
@p{"r$p","u$p"}=(P,P);pipe"r$p","u$p";++$p;($q*=2)+=$f=!fork;map{$P=$P[$f^ord
($p{$_})&6];$p{$_}=/ ^$P/ix?$P:close$_}keys%p}p;p;p;p;p;map{$p{$_}=~/^[P.]/&&
close$_}%p;wait until$?;map{/^r/&&<$_>}%p;$_=$d[$q];sleep rand(2)if/\S/;print
------------------------------
Date: Wed, 03 Oct 2001 21:32:15 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: filehandles between functions
Message-Id: <x71ykk9zpc.fsf@home.sysarch.com>
>>>>> "MJD" == Mark Jason Dominus <mjd@plover.com> writes:
MJD> In article <1002110362.18804@itz.pp.sci.fi>,
MJD> Ilmari Karonen <usenet11598@itz.pp.sci.fi> wrote:
>> Another variant, which works for pipes and other funny things too:
>>
>> sub slurp ($) {
>> my $fh = shift;
>> return <$fh> if wantarray;
>> local $/;
>> return <$fh> unless -s $fh;
>> read $fh, my $buf, -s _;
>> return $buf;
>> }
MJD> I just found (to my surprise) that this doesn't always work for
MJD> pipes. I wrote a program to make a pipe and fork a child. The
MJD> child writes data into the pipe and flushes it, and later exits,
MJD> but at no time does (-s PIPE) return anything other than 0 to the
MJD> parent.
MJD> If you add a line to the parent that actually reads R, it gets the
MJD> data from the child just fine, so the problem is with the -s operator,
MJD> not with the way the pipe is set up. 'stat' yields similar results.
that makes perfect sense to me. stat reads info from the file inode and
-s is just a wrapper around stat and just gets the size field. a pipe
inode has no size field to get. there are ways to get the number of
bytes you can read on a pipe but they are not directly mapped into perl
IIRC. SYSV streams supports the I_NREAD ioctl and there are others like
it which will tell you how many bytes can be read. the more common way
is to do a select/poll call on the pipe. or you do a non-blocking
sysread to get whatever bytes you can.
also on most modern unix systems, a pipe is really a socketpair
underneath. so you have to think about socket vs file styles of i/o.
so why people thought -s will work on a pipe is a mystery to me. -s
doesn't have any pipe related semantics, it just gets the file size from
the stat structure.
uri
--
Uri Guttman --------- uri@sysarch.com ---------- http://www.sysarch.com
SYStems ARCHitecture and Stem Development ------ http://www.stemsystems.com
Search or Offer Perl Jobs -------------------------- http://jobs.perl.org
------------------------------
Date: Wed, 03 Oct 2001 20:07:00 +0200
From: Philip Newton <pne-news-20011003@newton.digitalspace.net>
Subject: Re: func prototype enforcement with sub refs
Message-Id: <drkmrtckjbde6jjdu9jn84fme97v5gok2k@4ax.com>
On 3 Oct 2001 10:41:02 -0700, paul.a.balyoz@intel.com (Paul Balyoz)
wrote:
> Is there any way to get a reference to a sub without using the "&" syntax?
> That would probably fix it....
No (unless you have 'no strict "refs"'). But you can call the sub
without using '&': $ref->(1, 2, 3, 4). I still don't think it checks the
prototype when you call it through the subref, though. Come to think of
it, it can't, because you can't tell at compile time which sub is going
to be called, and prototype checking happens at compile time. (For a
similar reason, object methods are not prototype-checked.)
Cheers,
Philip
--
Philip Newton <nospam.newton@gmx.li>
That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.
------------------------------
Date: Wed, 3 Oct 2001 16:49:47 -0500
From: "Jonathan Clover" <jclover@nati.org>
Subject: Help
Message-Id: <trn1ff4dubh4f2@corp.supernews.com>
Well, I am getting this error:
syntax error at ./admin.cgi line 1252, near "for "
syntax error at ./admin.cgi line 1260, near "else"
when I run this program(excerpt below). I am using strict and have look at
all of the for and else statements for missing ;'s, brackets and the such
prior to this code and can not find anything. I find it weird that any for
or else code would stop on this lines seeing as they have no particular
characters that would normally show an error in an earlier of that type. If
anyone has any idea as to what other type of things I should look for, it
would be greatly appreciated.
Jonathan Clover
NATI
1246:####PRINT EVALUATION####
1247: print TEMP <<HTML;
1248:<table style="page-break-after: always">
1249: <tr>
1250: <td colspan="2">
1251: <p>$course User Analysis</p>
1252: <p>$date</p>
1253: <br>
1254: <p>Summary: Users of the $course Online Training Course tend to
use an computer using the $top{"OS} Operating System, while browsing most
often with $top{"Browser"}. THey tended to access the course most often
during the hour of $top{"Time"} while spending the most time in
$top{"Session"}</p>
1255: </td>
1256: </tr>
1257: <tr>
1258: <td valign="top">";
1259:HTML
1260:###TIMES COUNT PRINTING###
------------------------------
Date: 3 Oct 2001 12:03:14 -0700
From: vsira@hotmail.com (Venkatesh Babu Sira)
Subject: Re: How to add date stamp to a log file
Message-Id: <d5b8dfd2.0110031103.2500563@posting.google.com>
Michael Budash <mbudash@sonic.net> wrote in message news:<mbudash-CAB4B9.13105302102001@news.sonic.net>...
> In article <d5b8dfd2.0110021136.3c5c31e7@posting.google.com>,
> vsira@hotmail.com (Venkatesh Babu Sira) wrote:
>
> > I have code like,
> > $LOGFILENAME = "logfile.txt";
> > I want to add time stamp like
> > $LOGFILENAME = "logfile.txt.$time";
> > ex: that it may look like logfile.txt.2oct2001 .
> > Tx
> > -B
>
> this does it with a format you probly _oughta_ use, if you ever wanna
> sort (or view a dir listing of) these files:
>
> NOTE: beware of word wrap below!
>
> use POSIX;
> $LOGFILENAME = 'logfile.txt.' . POSIX::strftime("%Y%m%d", 0, 0, 0,
> (localtime)[3..5]);
>
> the above (currently) yields;
>
> logfile.txt.20011002
>
> this version does it exactly as you requested:
>
> use POSIX;
> my @t = localtime;
> $LOGFILENAME = 'logfile.txt.' . $t[3], lc(POSIX::strftime("%b%Y\n", 0,
> 0, 0, @t[3..5]));
>
> the above (currently) yields;
>
> logfile.txt.2oct2001
>
> hth-
Tx a lot Michael,second one i had to remove $t[3],other wise it will
create logfile.txt.3 .
-B
------------------------------
Date: Wed, 03 Oct 2001 19:47:22 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: How to add date stamp to a log file
Message-Id: <mbudash-5266EF.12472303102001@news.sonic.net>
In article <d5b8dfd2.0110031103.2500563@posting.google.com>,
vsira@hotmail.com (Venkatesh Babu Sira) wrote:
> Michael Budash <mbudash@sonic.net> wrote in message
> news:<mbudash-CAB4B9.13105302102001@news.sonic.net>...
> > In article <d5b8dfd2.0110021136.3c5c31e7@posting.google.com>,
> > vsira@hotmail.com (Venkatesh Babu Sira) wrote:
> >
> > > I have code like,
> > > $LOGFILENAME = "logfile.txt";
> > > I want to add time stamp like
> > > $LOGFILENAME = "logfile.txt.$time";
> > > ex: that it may look like logfile.txt.2oct2001 .
> > > Tx
> > > -B
> >
> > this does it with a format you probly _oughta_ use, if you ever wanna
> > sort (or view a dir listing of) these files:
> >
> > NOTE: beware of word wrap below!
> >
> > use POSIX;
> > $LOGFILENAME = 'logfile.txt.' . POSIX::strftime("%Y%m%d", 0, 0, 0,
> > (localtime)[3..5]);
> >
> > the above (currently) yields;
> >
> > logfile.txt.20011002
> >
> > this version does it exactly as you requested:
> >
> > use POSIX;
> > my @t = localtime;
> > $LOGFILENAME = 'logfile.txt.' . $t[3], lc(POSIX::strftime("%b%Y\n", 0,
> > 0, 0, @t[3..5]));
> >
> > the above (currently) yields;
> >
> > logfile.txt.2oct2001
> >
> > hth-
>
> Tx a lot Michael,second one i had to remove $t[3],other wise it will
> create logfile.txt.3 .
> -B
that actually looks like a typo to me. replace the comma after $t[3]
with a space-dot. i.e., should be:
$LOGFILENAME = 'logfile.txt.' . $t[3] . lc(POSIX::strftime("%b%Y\n", 0,
0, 0, @t[3..5]));
(tested)
hth-
--
Michael Budash ~~~~~~~~~~ mbudash@sonic.net
------------------------------
Date: 03 Oct 2001 19:07:09 +0100
From: nobull@mail.com
Subject: Re: Joining 3-D arrays
Message-Id: <u9g090zjg2.fsf@wcl-l.bham.ac.uk>
mauroid@csi.forth.gr (Dimitri) writes:
> I want to join the elements of this array. Something faster than but
> equivalent to this loop :
>
> $out = "";
> for ($i = 0; $i < @array; $i++) {
> for ($j = 0; $j < @{$array[$i]}; $j++) {
> $out .= join("", @{$array[$i][$j]});
> }
> }
You are writing C in Perl! Check out the non-C-like form of the
for() statement in perlsyn.
It won't be a lot faster to run but it's neater to write:
my $out = '';
for (@array) {
for (@$_) {
$out .= join("", @$_);
}
}
BTW: If you really seek brevity rather than speed:
my $out = join '' => map {@$_} map {@$_} @array;
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
------------------------------
Date: Wed, 03 Oct 2001 19:37:23 -0000
From: David Wall <darkon@one.net>
Subject: Re: Passing arguments to subs
Message-Id: <Xns912F9ED6AFA03darkononenet@207.126.101.97>
nobull@mail.com wrote on 03 Oct 2001:
> David Wall <darkon@one.net> writes:
>
>> "David K. Wall" <darkon@one.net> wrote on 30 Sep 2001:
>> > Isn't a standard way to do this is to use a dispatch table,
>>
>> I gather from the echoing silence that my post was either (a) a post
>> not needing a reply
>
> Yes. We all asumed the "isn't a standard way?" was rhetorical. Yes
> it is the standard way.
Oh. In that case I should have made it clearer that it wasn't a rhetorical
question, and that I was asking for informational purposes. Randal was
showing someone how to make a different approach work, and I wondered why
he didn't recommend something prettier.
> Although you'll see some detractors like
> myself who believe that since a Perl symbol table is just a hash
> there's sometimes (but not often) a case for using a symbol table as a
> dispatch table i.e. using symbolic references but prefixing them with
> a package name.
I don't think I completely understand that. I keep rereading it, but every
time I think I have a handle on it, it wiggles away. Could you point me at
a more concrete example?
If I do understand it correctly, then maybe you mean something like this?
use strict;
use warnings;
package NoBull; # :-)
sub test {
print "This test worked!\n";
}
package main;
my $action = 'test'; # input from somewhere....
my $subroutine = "NoBull::$action";
if ( defined &{$subroutine} ) {
no strict 'refs';
&{ $subroutine }();
}
--
David Wall
darkon@one.net
------------------------------
Date: 3 Oct 2001 12:52:12 -0700
From: jobrien@techtarget.com (James O'Brien)
Subject: patches to Net::*
Message-Id: <47a3b6fd.0110031152.3b1e244e@posting.google.com>
Is anyone aware of a non-official patch to the Net package of tools,
namely Net::Cmd? I'm noticing problems due either to not clearing
buffers or not checking return values of syswrite in Cmd::datasend()
and Cmd::dataend(). These are causing hang time when sending a number
of emails via Net::SMTP.
Wondering if anyone else has come across this problem and has a
solution.
Thanks,
--Jim
------------------------------
Date: Wed, 03 Oct 2001 15:12:12 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Pattern Matching
Message-Id: <3BBB630C.C69F06D5@earthlink.net>
shaz wrote:
>
> Thanks, for the help.
>
> I also want to be able to display the longer string. The solution that
> you have provided returns a value (0 or 1).
>
> $small = 'foo';
> $y_or_n = grep { index($_, $small) != -1 } keys %strings;
>
> This will try to find the small string within a longer string.
>
> I want to be able to display both the small string and the longer
> string. The small string is easy (ie $small) but how could I display
> the longer string?
Here's one way:
@longer_strings = grep { index($_, $small) != -1 } keys %strings;
print "These strings have $small as a substring: @longer_strings\n";
Here's another:
while( defined(my $long_string = each %strings) ) {
next if index($long_string, $small) == -1;
print "$long_string has $small as a substring.\n";
}
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
Date: Wed, 03 Oct 2001 15:25:20 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Pattern Matching
Message-Id: <3BBB6620.95BCF41@earthlink.net>
shaz wrote:
>
> The problem now is that I am using three hashes (each contain
> different length strings). I will call them %one(longer sentences),
> %two() and %three(shorter sentences).
>
> If I am currently scanning %three and I want to see if the strings are
> present in %one and %two, I will need to use LOTS of foreach
> statements.( This slows the program A LOT).
>
> I am using the following
>
> foreach $small(keys %three){
> if (grep { index($_, $small) != -1 } keys %two){
> $seen{$_}=1;
> }
> if (grep { index($_, $small) != -1 } keys %one){
> $seen{$_}=1;
> }
> }
I think you're a bit confused. What do you think $_ is going to be when
you assign $seen{$_}=1 ?
Anyway, I suspect you mean one of the following two things:
SMALL: while( defined( my $small = each %three ) ) {
while( defined( my $long = each %one ) ) {
next if index($long,$small) == -1;
$seen{$small} = 1; next SMALL;
}
while( defined( my $long = each %two ) ) {
next if index($long,$small) == -1;
$seen{$small} = 1; next SMALL;
}
}
or this:
while( defined( my $small = each %three ) ) {
while( defined( my $long = each %one ) ) {
$seen{$long} = 1 if index($long,$small) != -1;
}
while( defined( my $long = each %two ) ) {
$seen{$long} = 1 if index($long,$small) != -1;
}
}
>
> Adding the following in the if loop, will become TOO time consuming.
>
> foreach my $key (keys %strings){
> $longer = $key, last if index($key, $small) != -1;
> }
> print "$longer contains $small.\n" if defined $longer;
The "if" statement does not produce a loop.
Anyway, I can't see where you are proposing to add this foreach loop in
your code, so I can't help you.
> Is there an easier way to do this?
Yes, probably.
> I basically need to find the number of different longer strings that
> the smaller strings occur in.
Ahh. So it's a *number* you want, for each small string.
my %count;
while( defined( my $small = each %three ) ) {
while( defined( my $long = each %one ) ) {
++$count{$short} if index($long,$small) != -1;
}
while( defined( my $long = each %two ) ) {
++$count{$short} if index($long,$small) != -1;
}
}
while( my ($small,$count) = each %count ) {
print "Small string '$small' occurs $count times\n";
}
--
"I think not," said Descartes, and promptly disappeared.
------------------------------
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 1864
***************************************