[19671] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1866 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 3 21:05:31 2001

Date: Wed, 3 Oct 2001 18:05:11 -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: <1002157511-v10-i1866@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 3 Oct 2001     Volume: 10 Number: 1866

Today's topics:
    Re: Accurate timing <mgilfix@eecs.tufts.edu>
    Re: append column upon match (newbie) <goldbb2@earthlink.net>
    Re: Calling function with hash values as key-value argu <jboes@nexcerpt.com>
    Re: Can't unlink file under Perl for NT <glenn@surveystar.com>
    Re: Can't unlink file under Perl for NT <goldbb2@earthlink.net>
    Re: Can't unlink file under Perl for NT (Garry Williams)
    Re: Can't unlink file under Perl for NT <glenn@surveystar.com>
    Re: Can't unlink file under Perl for NT <glenn@surveystar.com>
    Re: Can't unlink file under Perl for NT <glenn@surveystar.com>
    Re: carriage return <iltzu@sci.invalid>
        Exists and Hash References <news@27b-6.de>
    Re: Exists and Hash References <s_grazzini@hotmail.com>
    Re: filehandles between functions (Mark Jason Dominus)
    Re: filehandles between functions (Mark Jason Dominus)
    Re: filehandles between functions <iltzu@sci.invalid>
    Re: filehandles between functions (Garry Williams)
        grep matching numbers with a dot? (mdsohn)
    Re: grep matching numbers with a dot? <goldbb2@earthlink.net>
    Re: grep matching numbers with a dot? <davidhilseenews@yahoo.com>
    Re: Joining 3-D arrays <goldbb2@earthlink.net>
    Re: opening directories (Damian James)
    Re: Pattern Matching <iltzu@sci.invalid>
        perl cgi javascript <smarx@i2000.com>
    Re: Pixel Color at particular coordinate <iltzu@sci.invalid>
    Re: Question about split (Dmitry Epstein)
    Re: Question about split <goldbb2@earthlink.net>
    Re: Socket closes early on local machine <bart.lateur@skynet.be>
    Re: Socket closes early on local machine <goldbb2@earthlink.net>
    Re: socket robustness <goldbb2@earthlink.net>
        Trapping sendmail errors (with eval?) (Bill Dykstra)
    Re: vvp:Perl DBI error <goldbb2@earthlink.net>
        Whither Palm Perl? <DrDebug@att.net>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 4 Oct 2001 00:23:38 GMT
From: Michael Gilfix <mgilfix@eecs.tufts.edu>
Subject: Re: Accurate timing
Message-Id: <slrn9rnavr.pp3.mgilfix@bofh.concordia.ca>


Hash: SHA1

  Hi Neb. You might want to try something like this:

    use Time::HiRes;
    my $mark = [ Time::HiRes::gettimeofday ];
    # ... Do cool stuff
    my $benchmark = Time::HiRes::tv_interval(
                        $mark,
                        [ Time::HiRes::gettimeofday ]
                    );
    print "Elapsed time: $benchmark seconds\n";

  That'll give you floating point seconds.

              -- Mike

In comp.lang.perl.misc, you wrote:
> I have a script that I want to calculate its total running time from start
> to finish.  It takes less than a second to run, so I can't use the time()
> function to calcule it.  Is there a way to get milliseconds?

- --
Michael Gilfix
mgilfix@eecs.tufts.edu

Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE7u6vyLZA6dOoLC0ERAllIAJ48MfeGfM1onjS9uMCE3fdBgfd+dACeNeLc
8yCdRpBZ53EBHRs9eXCHIHU=
=jYsM
-----END PGP SIGNATURE-----


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

Date: Wed, 03 Oct 2001 19:10:30 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: append column upon match (newbie)
Message-Id: <3BBB9AE6.66944C3E@earthlink.net>

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.

#!/usr/local/bin/perl -w
use strict;

my %input_file;
open( INFILE, "<input_file" )
	or die "Couldn't open input_file: $!";
while( <INFILE> ) {
	my @x = split;
	$input_file{$x[0]}{$x[1]} = $x[2];
}
close INFILE;

open( DATAFILE, "<data_file" )
	or die "Couldn't open data_file: $!";
while( <DATAFILE> ) {
	my @x = split;
	push @x, $input_file{$x[2]}{$x[3]}
		if defined $input_file{$x[2]}{$x[3]};
	print join(" ", @x), "\n";
}
close DATAFILE;

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Wed, 03 Oct 2001 14:27:07 GMT
From: "Jeff Boes" <jboes@nexcerpt.com>
Subject: Re: Calling function with hash values as key-value arguments
Message-Id: <%eFu7.38663$h%.9229@telenews.teleline.es>

In article <3BBB0698.2AD1F96@olen.to>, "Joonas Paalasmaa" <joonas@olen.to>
wrote:

>> > function("first" => "1",
>> >          "second" => "2");
>> 
>> function( %hash ); #that's why i love Perl
> 
> What should I do if I the first argument is normal argument and the
> following arguments are
> key-value pairs.
> Like:
> 
> function(firstarg, "first" => "1", "second" => "2");
> 
> 
> Btw I am using this to pass arguments to XML-Writer's startTag function,
> where key-values are
> pairs for xml tags' attributes.

Just 

  function(firstarg, %hash);

Read up on "list context" in your Perl docs.  Any time a %hash is
evaluated in list context (like in the argument list of a function), it
gets "expanded" into a list of key-value pairs (really, nothing more than
"key1, value1, key2, value2, ...").



-- 
Jeff Boes                                             vox 616.226.9550
Database Engineer                                     fax 616.349.9076
Nexcerpt, Inc.                                      jboes@nexcerpt.com


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

Date: Wed, 03 Oct 2001 23:50:30 GMT
From: Glenn <glenn@surveystar.com>
Subject: Re: Can't unlink file under Perl for NT
Message-Id: <3BBBA1EF.96C8A8A0@surveystar.com>

Changing permissions did the trick... BUT, while I am now able to delete a
file, i am NOT able to delete multiple files, as in...

   $numdeleted =  unlink "e:/somedir/testfile.*";
   print "deleted $numdeleted files";


Anyone have a thought on this?  I have files named testfile.1 testfile.2, etc.



Lynn wrote:

> Is your perl script running under a different id (e.g. cgi under the iis
> service)?
> Try giving the group "everyone" full control permissions to the directory
> which will contain the temp file.
>
> "Glenn" <glenn@surveystar.com> wrote in message
> news:3BB91332.152A5049@surveystar.com...
> > I can't seem to be able to delete a file via a Perl script under Win
> > NT.  Have tried many things -- most recently this simple test...
> >
> >     open (OUTFILE,"> e:/somedir/testfile.zzz");   #open it
> >     printf OUTFILE "some test data\n";                # write something
> >     close (OUTFILE);                                         # be sure
> > to close it
> >     unlink("e:/somedir/testfile.zzz") || print "could not delete  $!";
> > # then delete it
> >
> > The file gets created (it is there), but not deleted.  I get a
> > "Permission denied" error.  Don't ask why I am creating a file just to
> > delete it -- my real use involves use of temporary files which I want to
> > clean up when done. Any thoughts appreciated.  Thanks!
> >
> >



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

Date: Wed, 03 Oct 2001 20:30:21 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Can't unlink file under Perl for NT
Message-Id: <3BBBAD9D.ED89B411@earthlink.net>

Glenn wrote:
> 
> i am NOT able to delete multiple files, as in...
> 
>    $numdeleted =  unlink "e:/somedir/testfile.*";
>    print "deleted $numdeleted files";
> 
> Anyone have a thought on this?  I have files named testfile.1
> testfile.2, etc.

That's because unlink is the system call, not a command.  If you want
shell-type globbing, then use glob() :
	$numdeleted = unlink glob('e:/somedir/testfile.*');

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Thu, 04 Oct 2001 00:37:41 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: Can't unlink file under Perl for NT
Message-Id: <slrn9rnbql.d4u.garry@zfw.zvolve.net>

On Wed, 03 Oct 2001 23:50:30 GMT, Glenn <glenn@surveystar.com> wrote:
> ... BUT, while I am now able to delete a
> file, i am NOT able to delete multiple files, as in...
> 
>    $numdeleted =  unlink "e:/somedir/testfile.*";
>    print "deleted $numdeleted files";
> 
> Anyone have a thought on this?  I have files named testfile.1 testfile.2, etc.

Yes.  

The perlfunc manual page says unlink takes a LIST of files.  You pass
it a single string expecting it to magically be a list of all of your
files.  unlink tries to unlink the file with the name
"e:/somedir/testfile.*" and not unexpectedly cannot find a file with
that name.  

You are probably looking for the glob() function: 

  $numdeleted = unlink glob("e:/somedir/testfile.*");

-- 
Garry Williams


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

Date: Thu, 04 Oct 2001 00:39:28 GMT
From: Glenn <glenn@surveystar.com>
Subject: Re: Can't unlink file under Perl for NT
Message-Id: <3BBBAD66.FFCD2718@surveystar.com>

Just tried that -- interesting result... each time I execute the script,
it deletes 1 file, until they're all gone.

Benjamin Goldberg wrote:

> Glenn wrote:
> >
> > i am NOT able to delete multiple files, as in...
> >
> >    $numdeleted =  unlink "e:/somedir/testfile.*";
> >    print "deleted $numdeleted files";
> >
> > Anyone have a thought on this?  I have files named testfile.1
> > testfile.2, etc.
>
> That's because unlink is the system call, not a command.  If you want
> shell-type globbing, then use glob() :
>         $numdeleted = unlink glob('e:/somedir/testfile.*');
>
> --
> "I think not," said Descartes, and promptly disappeared.



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

Date: Thu, 04 Oct 2001 00:44:55 GMT
From: Glenn <glenn@surveystar.com>
Subject: Re: Can't unlink file under Perl for NT
Message-Id: <3BBBAEB6.D3C3482B@surveystar.com>

See my post from a few minutes ago about removing one file at a time.  By the way,
my original code worked fine under UNIX; I am having the problem now that I'm trying
to get the same thing to work under NT.    Call me confused.

Garry Williams wrote:

> On Wed, 03 Oct 2001 23:50:30 GMT, Glenn <glenn@surveystar.com> wrote:
> > ... BUT, while I am now able to delete a
> > file, i am NOT able to delete multiple files, as in...
> >
> >    $numdeleted =  unlink "e:/somedir/testfile.*";
> >    print "deleted $numdeleted files";
> >
> > Anyone have a thought on this?  I have files named testfile.1 testfile.2, etc.
>
> Yes.
>
> The perlfunc manual page says unlink takes a LIST of files.  You pass
> it a single string expecting it to magically be a list of all of your
> files.  unlink tries to unlink the file with the name
> "e:/somedir/testfile.*" and not unexpectedly cannot find a file with
> that name.
>
> You are probably looking for the glob() function:
>
>   $numdeleted = unlink glob("e:/somedir/testfile.*");
>
> --
> Garry Williams



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

Date: Thu, 04 Oct 2001 00:58:15 GMT
From: Glenn <glenn@surveystar.com>
Subject: Re: Can't unlink file under Perl for NT
Message-Id: <3BBBB1D5.91385892@surveystar.com>

The following worked...

    @list= glob ("e:/somedir/testfile.*");
    $numdeleted=unlink @list;

Thanks to all for your help!



Glenn wrote:

> Changing permissions did the trick... BUT, while I am now able to delete a
> file, i am NOT able to delete multiple files, as in...
>
>    $numdeleted =  unlink "e:/somedir/testfile.*";
>    print "deleted $numdeleted files";
>
> Anyone have a thought on this?  I have files named testfile.1 testfile.2, etc.
>
> Lynn wrote:
>
> > Is your perl script running under a different id (e.g. cgi under the iis
> > service)?
> > Try giving the group "everyone" full control permissions to the directory
> > which will contain the temp file.
> >
> > "Glenn" <glenn@surveystar.com> wrote in message
> > news:3BB91332.152A5049@surveystar.com...
> > > I can't seem to be able to delete a file via a Perl script under Win
> > > NT.  Have tried many things -- most recently this simple test...
> > >
> > >     open (OUTFILE,"> e:/somedir/testfile.zzz");   #open it
> > >     printf OUTFILE "some test data\n";                # write something
> > >     close (OUTFILE);                                         # be sure
> > > to close it
> > >     unlink("e:/somedir/testfile.zzz") || print "could not delete  $!";
> > > # then delete it
> > >
> > > The file gets created (it is there), but not deleted.  I get a
> > > "Permission denied" error.  Don't ask why I am creating a file just to
> > > delete it -- my real use involves use of temporary files which I want to
> > > clean up when done. Any thoughts appreciated.  Thanks!
> > >
> > >



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

Date: 3 Oct 2001 22:56:10 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: carriage return
Message-Id: <1002149200.27421@itz.pp.sci.fi>

In article <3BB664A3.9FD73CC1@earthlink.net>, Benjamin Goldberg wrote:
>
>use URI::Escape;
>my $encoded = uri_escape( $textarea );

Please don't.  If you insist on using URI::Escape, always use the
two-argument form of uri_escape().

  use URI::Escape;
  my $properly_encoded = uri_escape( $textarea, "^0-9A-Za-z" );

Actually the list of safe characters contains some non-alphanumerics
too, but excessive escaping is not a problem.  Insufficient escaping,
which is what URI::Escape does by default, is.

I'm not sure if this has been fixed recently or not, but in any case
most versions of URI::Escape still in use are broken.

FWIW, if the OP is using CGI.pm, it contains its own non-broken URI
escaping function named imaginatively escape().	 It's not exported by
default, or in any of the standard function sets, but one can export it
explicitly.

  use CGI qw( escape );
  my $properly_encoded = escape( $textarea );

-- 
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real!  This is a discussion group, not a helpdesk.  You post something,
we discuss its implications.  If the discussion happens to answer a question
you've asked, that's incidental."           -- nobull in comp.lang.perl.misc



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

Date: Thu, 04 Oct 2001 01:25:37 +0200
From: Magnus Schmidt <news@27b-6.de>
Subject: Exists and Hash References
Message-Id: <9pg6pk$icm13$1@ID-71174.news.dfncis.de>

Hi there,

 I thought that I understood perl references, however I'm stuck now with 
the following problem:

#!/usr/bin/perl -w
use strict;

my %zoneData;
my %records;
my $data;

# Not really needed to demonstrate my problem
$records{'level2'} = 'level3';
$zoneData{'level1'} = \%records;
$data = \%zoneData;

print "test 1 exists\n" if exists $$data{'level1'}->{'level2'};
# test 2 will fail
print "test 2 exists\n" if exists $$data{'unknown'};
print "test 3 exists\n" if exists $$data{'unknown'}->{'test'};
# test 4 will succeed
print "test 4 exists\n" if exists $$data{'unknown'};

Output:
test 1 exists
test 4 exists

The problem is, that test 2 fails as expected, but test 4 succeeds. It 
seems that the exists() statement introduced a new hash element. 

Can anyone help me ?

Thanks
Magnus Schmidt




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

Date: Thu, 04 Oct 2001 00:38:14 GMT
From: "Steve Grazzini" <s_grazzini@hotmail.com>
Subject: Re: Exists and Hash References
Message-Id: <WbOu7.1317$ZJ1.550093@typhoon.nyc.rr.com>

"Magnus Schmidt" <news@27b-6.de> wrote in message
news:9pg6pk$icm13$1@ID-71174.news.dfncis.de...

[..]
> print "test 3 exists\n" if exists $$data{'unknown'}->{'test'};
> # test 4 will succeed
> print "test 4 exists\n" if exists $$data{'unknown'};
>
> Output:
> test 1 exists
> test 4 exists
>
> The problem is, that test 2 fails as expected, but test 4 succeeds. It
> seems that the exists() statement introduced a new hash element.

It does :)

check perldoc -f exists




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

Date: Wed, 03 Oct 2001 22:06:40 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: filehandles between functions
Message-Id: <3bbb8bef.1f47$f7@news.op.net>

In article <x71ykk9zpc.fsf@home.sysarch.com>,
Uri Guttman  <uri@sysarch.com> wrote:
>a pipe inode has no size field to get. 

On many Unix systems, a pipe inode's size is the number of bytes
available for reading in the pipe.    

>so why people thought -s will work on a pipe is a mystery to me. 

Because it *does* work on a pipe.  Whether this is standard behavior
or not, I don't know, but it certainly is widespread.  Linux appears
to be an exception.


-- 
@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 22:12:13 GMT
From: mjd@plover.com (Mark Jason Dominus)
Subject: Re: filehandles between functions
Message-Id: <3bbb8d3c.1f71$181@news.op.net>

In article <x71ykk9zpc.fsf@home.sysarch.com>,
Uri Guttman  <uri@sysarch.com> wrote:
>>>>>> "MJD" == Mark Jason Dominus <mjd@plover.com> writes:
>  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.
>
>a pipe inode has no size field to get. 

As an addendum, I'll note that when run on Solaris 5.8, this same
program produces the following output:

        parent sees 0 bytes
        parent sees 0 bytes
        Child wrote and flushed.
        parent sees 6 bytes
        parent sees 6 bytes
        Child exits.
        parent sees 6 bytes
        parent sees 6 bytes
        parent sees 6 bytes

demonstrating that -s is in fact working on the pipe as Ilmari said it
would, contrary to your assertion that there is no size field to get.

>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.

-- 
@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: 3 Oct 2001 22:33:45 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: filehandles between functions
Message-Id: <1002147953.25598@itz.pp.sci.fi>

In article <m33d50llye.fsf@mumonkan.sunstarsys.com>, Joe Schaefer wrote:
>Ilmari Karonen <iltzu@sci.invalid> writes:
>
>> 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;
>                            ^^^^^^
>
>-s on a stream typically returns the number of bytes in 
>the stream's buffer, which is rarely zero.

Oops, sorry, my mistake.  Here's a corrected version:

  sub slurp ($) {
      my $fh = shift;
      return <$fh> if wantarray;
      local $/;
      return <$fh> unless -f $fh;
      read $fh, my $buf, -s _;
      return $buf;
  }

Spot the one-character change.

-- 
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real!  This is a discussion group, not a helpdesk.  You post something,
we discuss its implications.  If the discussion happens to answer a question
you've asked, that's incidental."           -- nobull in comp.lang.perl.misc



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

Date: Wed, 03 Oct 2001 23:56:01 GMT
From: garry@ifr.zvolve.net (Garry Williams)
Subject: Re: filehandles between functions
Message-Id: <slrn9rn9ch.d4u.garry@zfw.zvolve.net>

On Wed, 03 Oct 2001 03:59:23 GMT, Uri Guttman <uri@sysarch.com> wrote:
>>>>>> "GW" == Garry Williams <garry@ifr.zvolve.net> writes:
> 
>  GW>   sub read_file {
>  GW>     open( my $fh, $infile ) || die "can't open '$infile': $!\n";
>  GW>     chomp( my @lines = <$fh> );
>  GW>     return join "", @lines;
>  GW>   }
> 
> if you want to slurp a file, this is a useful sub

Perhaps, but the OP wanted the line-endings removed.  (OP had chomp()
in his original code.)  

[snip]

> now, don't go starting that slurping is bad and line by line is good
> thread again. :)

I'd never dream of it.  

-- 
Garry Williams


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

Date: 3 Oct 2001 17:12:08 -0700
From: mdsohn@yahoo.com (mdsohn)
Subject: grep matching numbers with a dot?
Message-Id: <e4ddb5cc.0110031612.1530bb89@posting.google.com>

If I do:

@matches = grep {$value} @times

if $value = 1.800, the grep will match a value in @times like 10800 . 
How do I make the dot recognized as a litteral dot?

thanks,
Mike


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

Date: Wed, 03 Oct 2001 20:34:29 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: grep matching numbers with a dot?
Message-Id: <3BBBAE95.5C7FF4A5@earthlink.net>

mdsohn wrote:
> 
> If I do:
> 
> @matches = grep {$value} @times
> 
> if $value = 1.800, the grep will match a value in @times like 10800 .
> How do I make the dot recognized as a litteral dot?

Actually, grep {$value} @times will result in either all the items in
@times, if $value is true, or none of them if $value is false.  Perhaps
you are thinking of:
	@matches = grep /$value/, @times;

The solution, if you are dealing with numbers, would be this:
	@matches = grep $value == $_, @times;
Or, considering how floating point numbers are sometimes imprecise:
	@matches = grep abs($value-$_) < 1E-5, @times;

If you are dealing with strings, then your solution would probably be
this:
	@matches = grep $value eq $_, @times;

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Thu, 04 Oct 2001 00:32:24 GMT
From: "David Hilsee" <davidhilseenews@yahoo.com>
Subject: Re: grep matching numbers with a dot?
Message-Id: <s6Ou7.47657$Xz1.9529430@news1.rdc1.md.home.com>


"mdsohn" <mdsohn@yahoo.com> wrote in message
news:e4ddb5cc.0110031612.1530bb89@posting.google.com...
> If I do:
>
> @matches = grep {$value} @times
>
> if $value = 1.800, the grep will match a value in @times like 10800 .
> How do I make the dot recognized as a litteral dot?
>
> thanks,
> Mike

I'm ignoring that the code's incorrectness.

perldoc -f quotemeta

Side note: make sure you're not re-inventing eq.

--
David Hilsee




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

Date: Wed, 03 Oct 2001 18:19:18 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Joining 3-D arrays
Message-Id: <3BBB8EE6.E1832625@earthlink.net>

Dimitri wrote:
> 
> I have a 3-D array @array, i.e. the elements are accessed like this :
> 
> $element = $array[$i][$j][$k];
> 
> 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]});
>         }
>     }
> 
> Any ideas?

I dunno how much faster [if at all] this is, but try:

my $out = "";
for(@array){for(@$_){for(@$_){ $out .= $_ }}}

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: 4 Oct 2001 00:34:34 GMT
From: damian@qimr.edu.au (Damian James)
Subject: Re: opening directories
Message-Id: <slrn9rnbfk.jhd.damian@puma.qimr.edu.au>

On Wed, 3 Oct 2001 14:29:28 GMT, catherine mullen said:
>Hi kind people
>

WARNING: while most people here are trying to be kind, sometimes it
may be difficult to tell that :-).

>[snip program details]
>
>Another directory called 'structures' is created which in turn creates
>directories, which in turn have more objects

You might find it easier to describe things clearly if you forbid
yourself to use the passive voice. That way, you force yourself to be
clear about 'who is doing what to whom'. I don't understand how a
directory can create directories, and you don't say what is creating
$username/structures.

>my problem:
>
>when the code below checks the eLectures to find that username, if it finds
>it a home page is printed.

Okay, so the username is listed in a file. So far so good.

>I want to then check the 'eLectures\\structures'to see if the username has
>any structures, if they have the normal home page can be called, if not I
>want a different homepage to be invoked with more direction to uploading
>files.

I am not sure here whether you want to test for the existence of
$username/structures or you want to test for the existence or files
and/or subdirectories under $username/structures.

in case of the former, try 

  print "$username/structures exists!\n"
	if -d "$username/structures";

in case of the latter:

  print "$username/structures contains stuff!\n"
	if glob "$username/structures/*";

See:
	perldoc -f '-X'
	perldoc -f glob

>What do you think? Can I open more than one file and test for occupancy!?

I am not sure what you mean here.

>sub testLogin {
>
>my $username = param('username');
>my $password = param('password');
>open(FILE, "c:\\electures\\info\\lecturers.dat") or print "Can't DO it";

Note that you don't need to keep using escaped backwhacks in you
specified path: you will be fine using normal slashes. Also, if
open() does indeed fail, you will probably want to know why:

  my $file = 'c:/electures/info/lecturers.dat';
  open(FILE, "$file") or print "Can't open $file: $!\n";

See `perldoc perlvar` and look at $!. 

>@pwd = <FILE>;
>close FILE;
>@match = grep {/^$username/} @pwd;
>(my @results) = split(/\t/, $match[0]);

You could rewrite this to avoid slurping the whole file:

   my @results;
   while (my $line = <FILE>) {
      if ( /^$username/ ) {
         @results = split(/\t/, $line);
		 last;
      }
   }
   close FILE;

>if ($username eq $results[0]) {
>#makeCookie($username);
>printFrameset($username);
>}else {
>register();
>}
>}

You might try using indentation to make your code more readable.

If you are really using the cgi-supplied $username data to do things
to your file system, then you probably want to find out about taint
checking. See `perldoc perlsec` and `perldoc perlrun` and look at the
'-T' command line option.

Anyway, good luck and all that.

HTH,

Cheers,
Damian
-- 
@:=grep!(m!$/|#!..$|),split//,<DATA>;@;=0..$#:;while($:=@;){$;=rand
$:--,@;[$;,$:]=@;[$:,$;]while$:;push@|,shift@;if$;[0]==@|;select$,,
$,,$,,1/80;print qq x\bxx((@;+@|)*$|++),@:[@|,@;],!@;&&$/}  __END__
Just another Perl Hacker,### http://home.pacific.net.au/~djames.hub


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

Date: 3 Oct 2001 22:23:08 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: Pattern Matching
Message-Id: <1002146604.24422@itz.pp.sci.fi>

In article <23e71812.0110011546.551771bf@posting.google.com>, shaz wrote:
>
>I basically need to find the number of different longer strings that
>the smaller strings occur in.

Hmm... okay, then the solution I posted before, using study(), won't
work.  But I think it can be adapted...

  my $huge_string = join "\0", keys %one, keys %two;
  study $huge_string;
 
  for (keys %three) {
      $seen{$_}++ while $huge_string =~ /\Q$_\E[^\0]*/g;
  }

The [^\0]* part is there to make sure that the pattern won't match twice
inside the same longer string.  Again, I'm assuming that none of the
keys in any of the hashes contain \0 characters.

It's of course also possible to capture the list of longer strings that
contain each short string, instead of just counting them, but that gets
noticeably slower, besides consuming more memory.

-- 
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real!  This is a discussion group, not a helpdesk.  You post something,
we discuss its implications.  If the discussion happens to answer a question
you've asked, that's incidental."           -- nobull in comp.lang.perl.misc



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

Date: Wed, 3 Oct 2001 20:17:13 -0400
From: "Steven Marx" <smarx@i2000.com>
Subject: perl cgi javascript
Message-Id: <6ONu7.15701$S27.1104970@e420r-atl2.usenetserver.com>

i have several perl cgi generated client submit objects which i
differentiate by value and interpret in my server side perl cgi program. i
handle them with a single onSubmit() function which calls a javascript
function to insure that the form is valid before submission. this all works
well. my problem is that i need to communicate which submit object was
actually activated for a particular call. i cannot seem to find a way to do
this. any help would be appreciated.
thanks in advance
steve





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

Date: 3 Oct 2001 23:01:15 GMT
From: Ilmari Karonen <iltzu@sci.invalid>
Subject: Re: Pixel Color at particular coordinate
Message-Id: <1002149824.27711@itz.pp.sci.fi>

In article <t22nrto3qu3topjv6jbmiso0th5bpqp645@4ax.com>, Bart Lateur wrote:
>Jay wrote:
>
>>I already wrote a script which parses out GIF header and the colormap,
>>I was getting a bit confused about the next pixel storage part.
>
>Well, it's likely that this is the part that is compressed. So
>extracting it won't be, er, simple. Or maybe even allowed without a
>license.  ;-)

The FSF says that, due to a technicality in the patent, decoding LZW
compressed data does not require a license.  They've got real lawyers,
and they're pretty careful about this sort of stuff, so I'm inclined to
trust them about this.

It's still not simple.

- 
Ilmari Karonen -- http://www.sci.fi/~iltzu/
"Get real!  This is a discussion group, not a helpdesk.  You post something,
we discuss its implications.  If the discussion happens to answer a question
you've asked, that's incidental."           -- nobull in comp.lang.perl.misc



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

Date: 3 Oct 2001 22:34:57 GMT
From: mitia.nospam@northwestern.edu.invalid (Dmitry Epstein)
Subject: Re: Question about split
Message-Id: <Xns912FB2DC2B375mitianorthwesternedu@129.105.16.55>

Michael Carman <mjcarman@home.com> wrote in
news:3BBB7131.2EF68F46@home.com: 

> Dmitry Epstein wrote:
>> 
>> [W]hen I do this:
>> 
>> #!/bin/perl -w
>> use strict;
>> my $line = 'words, words, words...';
>> my ($word1, $word2) = split $line;
>> 
>> I get the warning: "Use of uninitialized value in split"
>> 
>> What am I doing wrong?  The synax of split is
>> 
>> split /PATTERN/,EXPR,LIMIT
> 
> Heh. You've answered your own question but apparently you can't
> see the forest for the trees right now. ;)
> 
> Look at the syntax again. You didn't supply a pattern to
> split(), so Perl used $line as the pattern and then defaulted
> to $_ for the expression. Since $_ hasn't been set yet, you get
> the "uninitialized value" error.
> 
> -mjc
> 

D'oh!  I must be really tired.  What makes it worse is that I have 
made the same mistake once before.  But I haven't been writing Perl 
for a while.

It's too bad though that I have to use the /\s/ pattern in this 
case, because it won't skip the leading space like I want it to.  
An alternative is probably to copy the string to $_ first, and then 
use split with no parameters.  Seems like a wastful way though.

-- 
Dmitry Epstein
Northwestern University, Evanston, IL.  USA
mitia(at)northwestern(dot)edu


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

Date: Wed, 03 Oct 2001 19:30:35 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Question about split
Message-Id: <3BBB9F9B.7CB5FB02@earthlink.net>

Dmitry Epstein wrote:
> 
> Michael Carman wrote:
[snip]
> >> my $line = 'words, words, words...';
> >> my ($word1, $word2) = split $line;
[snip comment that split is: split PATTERN, EXPR, LIMIT]
> It's too bad though that I have to use the /\s/ pattern in this
> case, because it won't skip the leading space like I want it to.

There's also a special split syntax: split ' ', EXPR, LIMIT, which will
have the same behavior as split with no arguments does [ie, skip leading
blanks], but using EXPR and LIMIT.

That is, you can do:

my ($word1, $word2) = split ' ', $line;

Note that only ' ' [a single space] is special for split, other 'foo'
things aren't -- they just get turned into a pattern, and it doesn't
skip empty fields.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Wed, 03 Oct 2001 23:05:00 GMT
From: Bart Lateur <bart.lateur@skynet.be>
Subject: Re: Socket closes early on local machine
Message-Id: <r86nrt0uv98t066rp03qtbghimhcif27fn@4ax.com>

Long Pham wrote:

>We're running on NT...

>unkown error from the script ... which may indicate that the listening
>process is sending some funky character regardless of our
>configuration

Funky character? NT? Have you used binmode() on your handles? Otherwise,
a chr(26) might flag a (premature) EOF.

-- 
	Bart.


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

Date: Wed, 03 Oct 2001 19:35:08 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: Socket closes early on local machine
Message-Id: <3BBBA0AC.DF84563A@earthlink.net>

Bart Lateur wrote:
> 
> Long Pham wrote:
> 
> >We're running on NT...
> 
> >unkown error from the script ... which may indicate that the
> >listening process is sending some funky character regardless of our
> >configuration
> 
> Funky character? NT? Have you used binmode() on your handles?
> Otherwise, a chr(26) might flag a (premature) EOF.

But that still shouldn't set $!

And I would expect a network socket to have binmode on by default.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Wed, 03 Oct 2001 18:28:44 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: socket robustness
Message-Id: <3BBB911C.74468C1B@earthlink.net>

perl misk wrote:
> 
> within a socket server, if one uses:

First, I'm going to assume that you mean by "socket server":
A server which has used accept() to get connections from clients, and
has one socket to each client.

>     104     while (defined (my $reply = <$sock>)) {
>     105
>     106         print "$reply\n";
>     107
>     108     }
> 
> is this a potential bug?

Yes.  You should use select and sysread, not <>.

> what if the client stops sending for whatever reason?

Then the server blocks until the client sends more.

> would the server be stuck in a state of limbo?

No.  A state of limbo is where it's stuck when it's stopped, and you
have no idea why or where it's stopped.  Here it would be stopped, and
you know exactly where (the line with the <>), and why (because the
client has stopped sending).  So it's stuck, but not in limbo.

> would sysread/syswrite help? or

It's normally safe to assume that writing doesn't block, but you should
use select or IO::Select, and sysread.

> would it suffer a similar flaw?

Not as long as you don't do more than one sysread for each occasion that
select indicates that the socket is readable.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: 3 Oct 2001 17:29:13 -0700
From: bill@ilap.com (Bill Dykstra)
Subject: Trapping sendmail errors (with eval?)
Message-Id: <fedb623e.0110031629.71534241@posting.google.com>

Greetings,

I'm having some trouble trapping a fatal sendmail error when trying to
send email messages.   Every now and then sendmail is not available
(ie: somebody stopped the sendmail process, which happens now and then
if the sendmail config files are being rebuilt).  If my program is
running, and is in the process of sending a message or messages when
sendmail is stopped, it crashes, usually with an error such as this:

No local mailer defined
QueueDirectory (Q) option must be set

Because of this sendmail error, my perl program dies.   I've tried
using eval to trap the error, but I can't get it to work.

Here's a sample of the code I'm trying to use:

   eval {
      if (open(MAIL, "| /usr/lib/sendmail -t -oi")) {
          print MAIL "To: $target_address\n";
          print MAIL "From: $source_address\n";
          print MAIL "Subject: Test Message\n";
          print MAIL "\n";
          print MAIL "blah blah blah.....\n";
          if (! close(MAIL)) {
             print "Message $counter not sent.  Cannot close mail: $!
\n";
          }
      }
      else {
         print "Cannot open mail: $! \n";
      }
   };
   warn $@ if $@;
}

Now, I don't really want to just "warn" when an error occurs.  What I
will be doing is looping and resending the message a few seconds later
if the error occurs, since sendmail is usually available within a
couple of seconds.   The problem is, I can't even trap the error in
the first place, so I have no way to loop and retry if sendmail fails.

Any suggestions?   Any help will be greatly appreciated!
Please respond via email to bill@ilap.com (and maybe post a reply here
as well, to benefit anyone else who may have similar troubles).

Thanks much!

Bill


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

Date: Wed, 03 Oct 2001 18:55:12 -0400
From: Benjamin Goldberg <goldbb2@earthlink.net>
Subject: Re: vvp:Perl DBI error
Message-Id: <3BBB9750.3E26BEDB@earthlink.net>

Prasad, Victor [FITZ:K500:EXCH] wrote:
> 
> Would anybody be able to help me with this error:
> 
> Can't call method "bind_columns" without a package or object
> reference:
> 
> What is it referencing?
[snip]
> $sth=$dbh->prepare("SELECT first_name,last_name from aaa_emp whe
> re global_id=$sogid");
> 
> $sth->bind_columns(undef,\$first,\$last) <----THIS IS LINE 189

If you get an error:
Can't call method "%s" without a package or object reference.
It generally means that the left hand side of $something->foo() neither
a valid object [$sth->foo()] nor a valid package name [DBI->foo()].

So, $sth was probably undef at the time this statement was reached,
producing the error.

Here's a script which should work:

#!/usr/local/bin/perl -w
use strict;
# never program without -w and 'use strict' unless you
# thouroughly understand why they are there and what problems
# they prevent.

my @signarray = (
    [qw/oid1 ogid1 os1 osd1 ot1/],
    [qw/oid2 ogid2 os2 osd2 ot2/],
);
my $dbh = DBI->connect( "DBI:...", "...", "...", {RaiseError=>1} );
# the RaiseError is very important!

my $sth = $dbh->prepare( q[
    SELECT first_name, last_name
    FROM aaa_emp
    WHERE global_id=?
]);

foreach my $signarray ( @signarray ) {
    $sth->execute( $signarray->[1] );
    my ($first, $last) = $sth->fetchrow_array;
    s/ //g for( $first, $last );
    print "Name: $first $last\n";
    my @args = @$signarray[ 0, 1 ], $first, $last,
        @$signarray[ 2, 3 ], $r_id[$rq], $rtotal[$rq], $cnt;
    printf "Sign off:%s, GID:%s %s %s, status: %s, date: %s, ".
        "req: %s, total:%s %s\n", @args;
    print FILE join("|", @args) . "\n";
    $sth->finish;
}

NB: this code is untested.

-- 
"I think not," said Descartes, and promptly disappeared.


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

Date: Thu, 04 Oct 2001 00:06:43 GMT
From: "DrDebug" <DrDebug@att.net>
Subject: Whither Palm Perl?
Message-Id: <nKNu7.21409$WW.1770310@bgtnsc05-news.ops.worldnet.att.net>

Is there some reason why Perl has not been ported to the PalmOS?

Sure it's big, but couldn't something be done?

Is there other reasons why not?

Thanks!






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

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


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