[22500] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4721 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Mar 17 18:05:43 2003

Date: Mon, 17 Mar 2003 15:05:08 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Mon, 17 Mar 2003     Volume: 10 Number: 4721

Today's topics:
    Re: Can I use session Variables in Perl <tore@aursand.no>
        Closure in a subroutine inside a subroutine <bernie@rev.net>
    Re: Closure in a subroutine inside a subroutine (Anno Siegel)
    Re: Closure in a subroutine inside a subroutine <bernie@rev.net>
        database connecting error (ramesh)
    Re: database connecting error <tore@aursand.no>
    Re: Deployment of semi-large-ish Perl project <tzz@lifelogs.com>
        Expression with varying number of conditions <Joly.Patrick@ic.gc.ca>
    Re: Expression with varying number of conditions <me@verizon.invalid>
    Re: Expression with varying number of conditions <krahnj@acm.org>
    Re: Generic way to work with mysql tables...? (Tad McClellan)
    Re: How to execute find -exec in perl? <tzz@lifelogs.com>
    Re: Mail::Sender <ben.i.glaser@boeing.com>
    Re: regexp and grouping (Anno Siegel)
    Re: Run perl script under Mac OS X (Tony L. Svanstrom)
    Re: splitting an array into two arrays <usenet@dwall.fastmail.fm>
    Re: splitting an array into two arrays <wksmith@optonline.net>
    Re: splitting an array into two arrays <usenet@dwall.fastmail.fm>
    Re: splitting an array into two arrays (Anno Siegel)
    Re: Where to find DBD::CSV help? <tore@aursand.no>
    Re: Win32 Perl newsgroup? (Tad McClellan)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 17 Mar 2003 22:53:54 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: Can I use session Variables in Perl
Message-Id: <pan.2003.03.17.21.31.39.161992@aursand.no>

On Mon, 17 Mar 2003 17:56:47 +0200, Yurij Nykon wrote:
> what I need is to use some Session variables on my web-Project.

Go to http://www.cpan.org/ and search for 'session'.


-- 
Tore Aursand <tore@aursand.no>


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

Date: Mon, 17 Mar 2003 14:54:52 -0500
From: Bernard Cosell <bernie@rev.net>
Subject: Closure in a subroutine inside a subroutine
Message-Id: <F5C414C023B58B41.61AB5980B9CE3637.EF87AD17AE16560D@lp.airnews.net>

I was trying to do a bit of a funny thing and I don't exactly
understand what is happening.  What I want to do is set up a
subroutine *INSIDE* a subroutine and have a closure allow that
subroutine to access one of the subroutine's lexicals.
-----------------------------
#!/usr/bin/perl -w
use strict ;

sub foo
{   my $x ;
    sub moo { return $x; }
}
------------------
Variable "$x" will not stay shared at t.pl line 6.

I don't understand why I got the warning nor what to do about it.
Can't I have an 'internal' subroutine like this access a lexical in
the parent subroutine??  What will actually happen when I call the
'moo' subroutine -- which '$x' will it actually reference, if not the
'shared' one?

  /Bernie\


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

Date: 17 Mar 2003 21:10:45 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Closure in a subroutine inside a subroutine
Message-Id: <b55dkl$2ar$2@mamenchi.zrz.TU-Berlin.DE>

Bernard Cosell  <bernie@rev.net> wrote in comp.lang.perl.misc:
> I was trying to do a bit of a funny thing and I don't exactly
> understand what is happening.  What I want to do is set up a
> subroutine *INSIDE* a subroutine and have a closure allow that
> subroutine to access one of the subroutine's lexicals.
> -----------------------------
> #!/usr/bin/perl -w
> use strict ;
> 
> sub foo
> {   my $x ;
>     sub moo { return $x; }
> }
> ------------------
> Variable "$x" will not stay shared at t.pl line 6.
> 
> I don't understand why I got the warning nor what to do about it.
> Can't I have an 'internal' subroutine like this access a lexical in
> the parent subroutine??  What will actually happen when I call the
> 'moo' subroutine -- which '$x' will it actually reference, if not the
> 'shared' one?

"use diagnostics" is the key here.  It explains this nicely.

The solution is to make the inner sub anonymous -- apparently the
compiler treats that differently.  To get your semantics, use

    sub foo
    {   my $x ;
        *moo = sub { return $x; }
    }

You may have to fight a warning or two.

Anno


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

Date: Mon, 17 Mar 2003 16:13:21 -0500
From: Bernard Cosell <bernie@rev.net>
Subject: Re: Closure in a subroutine inside a subroutine
Message-Id: <24D9EFB8671EC675.2FB8AFD6A28642F2.DF636DB9DE9A8CCE@lp.airnews.net>

On Mon, 17 Mar 2003 14:54:52 -0500, Bernard Cosell <bernie@rev.net>
wrote:

>... What I want to do is set up a
>subroutine *INSIDE* a subroutine and have a closure allow that
>subroutine to access one of the subroutine's lexicals.

I modified my program and I can made the error message go away

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

sub foo
{   my $x ;:

    my $moo = sub { return $x; } ;
    $x = "first" ;
    print $moo->();
    $x = "second" ;
    print $moo->() ;
}

and it appears that the '$x' is properly 'closured' and shared without
a problem.  if you'd have asked me tenminutes ago, I'd have said that
to first order:
    sub x {subroutine}
and
    my $x = sub { subroutine }
were pretty much the same -- modulo in one case being a reference to
the sub and the other being the actual sub, but in either case the
*subroutine* would be the same, but apparently that's not the case
when there's a closure inside the subroutine body...  Strange..

[BTW: this with 5.6.1]

  /Bernie\


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

Date: 17 Mar 2003 11:30:49 -0800
From: rpalla@kentlaw.edu (ramesh)
Subject: database connecting error
Message-Id: <2b0865a8.0303171130.2c1e672b@posting.google.com>

hi

when i am getting following error..........when i am trying to connect
the database and executing the code.

my code 

use DBI;
#use DBD::ODBC;
$dsname="hrvd1";
$absdsname= "dbi:Oracle:$dsname";
$objectnumber = 142;
$username = "harvest";
$password ="harvest";
# Making connection with the database. This call will automatically
load the

my $dbh = DBI->connect($absdsname, $username, $password, {RaiseError
=>1,
AutoCommit =>1,}) ;




my $sth =$dbh->prepare(q{ select ITEMNAME from HARVEST_HARITEMS }) or
die(" cann
ot prepare statement:", $dbh->errstr(),"\n");


$sth->execute() or
      die ("Could not execute SQL statement, maybe invalid?",
$sth->errstr(),"\n
");

my @array;
while (@array = $sth->fetchrow_array()){
write();
}

warn($DBI::errstr) if $DBI::err;
$dbh->disconnect();
$sth->finish();




errors:
DBD::Oracle::db prepare failed: ORA-00942: table or view does not
exist (DBD ERROR: OCIStmtExecute/Describe) at perlmodule5 line 19.

please help me. thanks


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

Date: Mon, 17 Mar 2003 22:53:54 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: database connecting error
Message-Id: <pan.2003.03.17.21.34.42.677390@aursand.no>

On Mon, 17 Mar 2003 11:30:49 -0800, ramesh wrote:
> DBD::Oracle::db prepare failed: ORA-00942: table or view does not
> exist [...]

Well?  _Does_ it exist?


-- 
Tore Aursand <tore@aursand.no>


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

Date: Mon, 17 Mar 2003 14:47:57 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Deployment of semi-large-ish Perl project
Message-Id: <4n4r61zseq.fsf@lockgroove.bwh.harvard.edu>

On Fri, 14 Mar 2003, nospam@nospam.net wrote:
> If not, how can I make sure users don't (accidentally) tamper with
> the files? Would Qt be a problem?

Store MD5 hashes of each file in a central place, and verify them when
you start up in a wrapper.  When they are OK, start your main
program.  It's hard to create matching MD5 hashes accidentally.

Ted


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

Date: Mon, 17 Mar 2003 15:37:24 -0500
From: "Joly, Patrick: IAC" <Joly.Patrick@ic.gc.ca>
Subject: Expression with varying number of conditions
Message-Id: <3E3612E8D41DD3118DCC0600000000000991A05F@pubgate-01.ic.gc.ca>

Hello all.  I have a module which sorts through the keys of a hash keys
based on its values, as follows:

# given:
#     ($var1, $var2, $var3) = @_
# and hashes such as %hash1, %hash2, %hash3, defined
# earlier in the code and containing integers as keys

        @keys = sort {
            ( ( $var1 =~ m|string| )
                    ?   $hash1{$a} cmp $hash1{$b}
                    :   $hash1{$a} <=> $hash1{$b}
            )
                                ||
            ( ($var2 =~ m|string|)
                    ?   $hash2{$a} cmp $hash2{$b}
                    :   $hash2{$a} <=> $hash2{$b}
            )
                                ||
            ( ($var3 =~ m|string|)
                    ?   $hash3{$a} cmp $hash3{$b}
                    :   $hash3{$a} <=> $hash3{$b}
            )

                } keys %hash1;       # by value

The code works as expected.  However, I need to generalize it to
situations where the length of the @_ array is different than 3 (could
be any length, anywhere between 2 to 20)

I was thinking of building the string of code contained within the -sort
{ ... }- using a foreach loop:

my $code;
for my $i (0..$#_) {
    my $j = $i + 1;
    if ($i==0) {
        $code =
            '( ( $var1 =~ m|string| )
                    ?   '$hash1{$a} cmp $hash1{$b}'
                    :   '$hash1{$a} <=> $hash1{$b}'
            )';
    }
    else {
        $code = $code . ' || ' .
            '( ( $var$j =~ m|string| )
                    ?   '$hash$j{$a} cmp $hash$j{$b}'
                    :   '$hash$j{$a} <=> $hash$j{$b}'
            )';

and then issue

        @keys = sort {
            qx/$code/
                } keys %hash1;       # by value

But this doesn't work for probably numerous reasons, the first one being
that $j will *not* interpolate as I want it to.  But even if it did
interpolate, the
            qx/$code/
part doesn't work -- I have tried it for the 2 variable case ($var1,
$var2) replacing the literal '$j' by '2' above and it still wouldn't
work.

Any idea how to tackle this?  Thanks! I am really stuck here, so any
pointers would be most appreciated.


Patrick Joly
joly.patrick@ic.gc.ca




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

Date: Mon, 17 Mar 2003 21:56:57 GMT
From: "dw" <me@verizon.invalid>
Subject: Re: Expression with varying number of conditions
Message-Id: <Jwrda.48510$68.18538@nwrdny01.gnilink.net>


"Joly, Patrick: IAC" <Joly.Patrick@ic.gc.ca> wrote in message
news:3E3612E8D41DD3118DCC0600000000000991A05F@pubgate-01.ic.gc.ca...
> my $code;
> for my $i (0..$#_) {
>     my $j = $i + 1;
>     if ($i==0) {
>         $code =
>             '( ( $var1 =~ m|string| )
>                     ?   '$hash1{$a} cmp $hash1{$b}'
>                     :   '$hash1{$a} <=> $hash1{$b}'
>             )';
>     }
>     else {
>         $code = $code . ' || ' .
>             '( ( $var$j =~ m|string| )
>                     ?   '$hash$j{$a} cmp $hash$j{$b}'
>                     :   '$hash$j{$a} <=> $hash$j{$b}'
>             )';
>
> and then issue
>
>         @keys = sort {
>             qx/$code/
>                 } keys %hash1;       # by value
>
> But this doesn't work for probably numerous reasons, the first one being
> that $j will *not* interpolate as I want it to.  But even if it did
> interpolate, the
>             qx/$code/
> part doesn't work -- I have tried it for the 2 variable case ($var1,
> $var2) replacing the literal '$j' by '2' above and it still wouldn't
> work.
>

First of all... qx/.../ will execute code using as a system command passing
the code to the shell for processing.  You probably want to do an eval on
the code.

The reason $j isn't interpolated is because you are using single quotes.
Change to double quotes and escape all your other meta characters, play
games with concat, or with substituting with regular expressions.
$code .= "|| (( \$var$j =~ m|string| ) ....."
   or
$code .= '|| (( $var' . $j . ' =~ m|string| ) .....'
   or
$codesnippet = '|| (( $var$j =~ m|string| ) ....';
$codesnippet =~ s/\$j/$j/g;
$code .= $codesnippet;

You could also remove the first case (for $j == 1) and use just remove the
'||' that will be at the beginning of your code so that if you have to
change the code, you don't have to change it twice.

dw




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

Date: Mon, 17 Mar 2003 22:07:00 GMT
From: "John W. Krahn" <krahnj@acm.org>
Subject: Re: Expression with varying number of conditions
Message-Id: <3E7646F0.CED6ED33@acm.org>

"Joly, Patrick: IAC" wrote:
> 
> Hello all.  I have a module which sorts through the keys of a hash keys
> based on its values, as follows:
> 
> # given:
> #     ($var1, $var2, $var3) = @_
> # and hashes such as %hash1, %hash2, %hash3, defined
> # earlier in the code and containing integers as keys
> 
>         @keys = sort {
>             ( ( $var1 =~ m|string| )
>                     ?   $hash1{$a} cmp $hash1{$b}
>                     :   $hash1{$a} <=> $hash1{$b}
>             )
>                                 ||
>             ( ($var2 =~ m|string|)
>                     ?   $hash2{$a} cmp $hash2{$b}
>                     :   $hash2{$a} <=> $hash2{$b}
>             )
>                                 ||
>             ( ($var3 =~ m|string|)
>                     ?   $hash3{$a} cmp $hash3{$b}
>                     :   $hash3{$a} <=> $hash3{$b}
>             )
> 
>                 } keys %hash1;       # by value
> 
> The code works as expected.  However, I need to generalize it to
> situations where the length of the @_ array is different than 3 (could
> be any length, anywhere between 2 to 20)

You have three different hashes with the same keys in each hash. 
Perhaps you need to use a different data structure like a HoA or HoH? 
Can't tell without seeing sample data or what else you're doing with the
hashes.


> I was thinking of building the string of code contained within the -sort
> { ... }- using a foreach loop:
> 
> my $code;
> for my $i (0..$#_) {
>     my $j = $i + 1;
>     if ($i==0) {
>         $code =
>             '( ( $var1 =~ m|string| )
>                     ?   '$hash1{$a} cmp $hash1{$b}'
>                     :   '$hash1{$a} <=> $hash1{$b}'
>             )';
>     }
>     else {
>         $code = $code . ' || ' .
>             '( ( $var$j =~ m|string| )
>                     ?   '$hash$j{$a} cmp $hash$j{$b}'
>                     :   '$hash$j{$a} <=> $hash$j{$b}'
>             )';
> 
> and then issue
> 
>         @keys = sort {
>             qx/$code/

qx// executes an EXTERNAL program.

perldoc -f eval


>                 } keys %hash1;       # by value
> 
> Patrick Joly
> joly.patrick@ic.gc.ca

Ah, our tax dollars at work!  :-)


John
-- 
use Perl;
program
fulfillment


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

Date: Mon, 17 Mar 2003 14:36:01 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Generic way to work with mysql tables...?
Message-Id: <slrnb7ccdh.4c3.tadmc@magna.augustmail.com>

Yossi Shani <yossi_shani@yahoo.com> wrote:


> I want to eliminate the hardcoded headers and use a generic way to work.


> Let's say I have the headers of two different tables in the following lists
> 
> @header = qw / platform ip gid .../
> @header1 = qw /one two three .../
> 
> Could I do something like
> 
> while (@header) = $sth->fetchrow_array) {       --->   instead of  while (
> $platform, $ip, $gid ... )


I will assume that the order in @headers is the same order as
in your SELECT statement...


Use a "hash slice" instead:

   my %results;
   while ( @results{@header} = $sth->fetchrow_array) {

The refer to $results{platform}, $results{ip} etc.


> I would like the header names to become the actual variables which holds the
> params.


You only _think_ you want that.

You don't want that (symbolic references):

   http://www.plover.com/~mjd/perl/varvarname.html
   http://www.plover.com/~mjd/perl/varvarname2.html
   http://www.plover.com/~mjd/perl/varvarname3.html

Particularly since you can "get what you want" without the
risks associated with using symrefs.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Mon, 17 Mar 2003 14:34:49 -0500
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: How to execute find -exec in perl?
Message-Id: <4nadftzt0m.fsf@lockgroove.bwh.harvard.edu>

On Mon, 17 Mar 2003, bernd.fischer@xignal.de wrote:
> Thank's to all, 
> I did it already with 'FILE::FIND' 

Save yourself time and effort, use find2perl.  For instance:

find2perl . -type f -exec chmod 664 {} \; 

Then you can fine-tune it later, but the skeleton is written for you.

Ted


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

Date: Mon, 17 Mar 2003 22:36:38 GMT
From: "Ben" <ben.i.glaser@boeing.com>
Subject: Re: Mail::Sender
Message-Id: <HBx050.93F@news.boeing.com>


"Gunnar Hjalmarsson" <noreply@gunnar.cc> wrote in message
news:b552ge$267reb$1@ID-184292.news.dfncis.de...
> Ben wrote:
> > Using Mail::Sender.  Can send file as attachement but would like to do
one
> > of two things.  Either:
> >
> > 1. set up the msg => as STDOUT so the body of the message is the ouput
of
> > the script.
>
> If you can use Mail::Sender for sending attachments, it cannot be that
> hard to pick one of the examples in the module docs for sending a simple
> message.
> What have you tried? Please post relevant code fragments.
> Which error messages did you receive?
>
> > 2. instead of attachement have the contents of the file become the body
of
> > a message (msg => read from STDIN filehandle?
>
> You find an example of that at
>
http://groups.google.com/groups?dq=&hl=en&lr=&ie=UTF-8&threadm=b527p9%2424qc
0e%241%40ID-184292.news.dfncis.de&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-
8%26group%3Dcomp.infosystems.www.authoring.cgi
>
> The original poster didn't show how he set the smtp parameter, but
> besides that, the example should work.
>
> > New at perl.
>
> You are obviously also new at posting to Usenet groups, since you posted
> exactly the same question to comp.lang.perl.modules a couple of days
> ago. Don't do so!
>
> / Gunnar
>
> --
> Gunnar Hjalmarsson
> Email: http://www.gunnar.cc/cgi-bin/contact.pl
>


Thanks! That example helped.




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

Date: 17 Mar 2003 20:57:52 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: regexp and grouping
Message-Id: <b55csg$2ar$1@mamenchi.zrz.TU-Berlin.DE>

Abigail  <abigail@abigail.nl> wrote in comp.lang.perl.misc:
> Anno Siegel (anno4000@lublin.zrz.tu-berlin.de) wrote on MMMCDLXXXV
> September MCMXCIII in <URL:news:b547i5$1hs$2@mamenchi.zrz.TU-Berlin.DE>:
> []  Bjørnar Libæk  <bjoernal@ifi.uio.no> wrote in comp.lang.perl.misc:
> [] > Given the string  "what is wrong with my brain", I want to get every 
> [] > two-letter composition that starts with an 'r' into the grouping-variables,
> [] > that is, $1='ro' and $2='ra' in this example. How do I do this?
> []  
> []  my @hits = 'what is wrong with my brain' =~ /(r.)/g;
> 
> 
> That doesn't set $2, and doesn't set $1 to 'ro'. It only sets $1,
> and sets it to 'ra'.

Right.

> The following will set $1 to 'ro', and $2 = 'ra':
> 
> 
>     $_ = 'what is wrong with my brain';
>     /@{[".*(r.)" x (() = m(r.)g)]}/; 
>     print "<$1> <$2>\n";         
>     __END__
>     <ro> <ra>

 ... or

    /@{[ do { local $_ = $_; s!r.!(r.)!g; $_}]}/;

Anno


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

Date: Mon, 17 Mar 2003 20:24:16 GMT
From: tony@svanstrom.com (Tony L. Svanstrom)
Subject: Re: Run perl script under Mac OS X
Message-Id: <1frzhz2.r3f9ys1e9yd30N%tony@svanstrom.com>

Ian.H [dS] <ian@WINDOZEdigiserv.net> wrote:

>   ./somescript.pl
> 
> 
> UNIX doesn't assume that you're executing from the current dir like
> windoze does.

 Which one can change by adding a dot to the correct dot-file in the ~,
so that the path includes the current dir. =)

 BUT... the prefered solution to his problem would be to include the
full path to the script in his crontab.

-- 
# Per scientiam ad libertatem! // Through knowledge towards freedom! #
# Genom kunskap mot frihet! =*= (c) 1999-2002 tony@svanstrom.com =*= #

    perl -e'print$_{$_} for sort%_=`lynx -source svanstrom.com/t`'


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

Date: Mon, 17 Mar 2003 19:05:08 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: splitting an array into two arrays
Message-Id: <Xns93418F497F626dkwwashere@216.168.3.30>

Steve Grazzini <grazz@nyc.rr.com> wrote on 17 Mar 2003:

> John W. Krahn <krahnj@acm.org> wrote:
>> "David K. Wall" wrote:
>>> 
>>> my @full_array = 1..9;
>>> my @second = @full_array;
>>> my @first = splice @second, 0, int(scalar(@second)/2 + 0.8);
>>> 
>>> [snipped] Just out of curiousity, is there a better way?
>> 
>> I don't know if this is "better" but it does what you want.
>> 
>> my @first  = @full_array[ 0 .. ( $#full_array / 2 ) ];
>> my @second = @full_array[ ( $#full_array / 2 + 1 ) .. $#full_array
>> ]; 
> 
> Maybe put the slice on the left?
> 
>   my (@first, @second);
>   (@first[0 .. $#full_array/2], @second) = @full_array;

I just wanted to see how other people would approach it.  It's a simple 
problem, but I knew there there were all sorts of ways to do it because 
I thought of several myself.  I didn't actually type any of them in, but 
one of my alternate solutions that I rejected was a for(;;) loop that 
pushed into either of two arrays depending on the array index.

What's interesting to me is that I didn't think of either of of the 
above two ways.  :-)

-- 
David K. Wall - usenet@dwall.fastmail.fm
WWJD? JWRTFM.


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

Date: Mon, 17 Mar 2003 19:33:35 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: splitting an array into two arrays
Message-Id: <jqpda.119212$b8.16565877@news4.srv.hcvlny.cv.net>


"David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
news:Xns93417A4BC8BE4dkwwashere@216.168.3.30...
>
> The purpose of this snippet of code is to split an array into halves.
If
> there are an odd number of elements, the first array should be the
> largest, e.g.; an array with 9 elements should be split into arrays
with 5
> and 4 elements.
>
> my @full_array = 1..9;
> my @second = @full_array;
> ###my @first = splice @second, 0, int(scalar(@second)/2 + 0.8);
> my @first = splice @second, 0, ceil(@second/2);

Your approach is not correct for an even number of elements.
The function ceil (available in the POSIX module) always does the
'rounding' correctly.  For details on the difference,
refer to perldoc -f int .

Bill





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

Date: Mon, 17 Mar 2003 21:35:55 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: splitting an array into two arrays
Message-Id: <Xns9341A8D9D9D59dkwwashere@216.168.3.30>

Bill Smith <wksmith@optonline.net> wrote on 17 Mar 2003:

> "David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
> news:Xns93417A4BC8BE4dkwwashere@216.168.3.30...
>>
>> The purpose of this snippet of code is to split an array into halves.
> If
>> there are an odd number of elements, the first array should be the
>> largest, e.g.; an array with 9 elements should be split into arrays
> with 5
>> and 4 elements.
>>
>> my @full_array = 1..9;
>> my @second = @full_array;
>> ###my @first = splice @second, 0, int(scalar(@second)/2 + 0.8);
>> my @first = splice @second, 0, ceil(@second/2);
> 
> Your approach is not correct for an even number of elements.
> The function ceil (available in the POSIX module) always does the
> 'rounding' correctly.  For details on the difference,
> refer to perldoc -f int .

Sure it is.  While it's not the most elegant approach possible**, it 
produces correct results.  Run it and see, or just look below.

** (I only picked 0.8 because I was in a hurry and didn't want to worry 
about how the computer represented the number.  Any x where 
0.5 <= x < 1.0 would work, since 0.5 has an exact representation in 
binary.)


Let n be a positive integer.

Array with even length of 2n
splice() will return int( 2n/2 + 0.8 ) = int( n + 0.8 ) = n elements

Array with odd length of 2n+1
splice will return int( (2n+1)/2 + 0.8 ) = int(n + 0.5 + 0.8)
    = int(n + 1.3) = n+1 elements

Just what I wanted.

-- 
David K. Wall - usenet@dwall.fastmail.fm
WWJD? JWRTFM.


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

Date: 17 Mar 2003 22:01:32 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: splitting an array into two arrays
Message-Id: <b55gjs$2ar$3@mamenchi.zrz.TU-Berlin.DE>

David K. Wall <usenet@dwall.fastmail.fm> wrote in comp.lang.perl.misc:
> Bill Smith <wksmith@optonline.net> wrote on 17 Mar 2003:
> 
> > "David K. Wall" <usenet@dwall.fastmail.fm> wrote in message
> > news:Xns93417A4BC8BE4dkwwashere@216.168.3.30...
> >>
> >> The purpose of this snippet of code is to split an array into halves.
> > If
> >> there are an odd number of elements, the first array should be the
> >> largest, e.g.; an array with 9 elements should be split into arrays
> > with 5
> >> and 4 elements.
> >>
> >> my @full_array = 1..9;
> >> my @second = @full_array;
> >> ###my @first = splice @second, 0, int(scalar(@second)/2 + 0.8);
> >> my @first = splice @second, 0, ceil(@second/2);
> > 
> > Your approach is not correct for an even number of elements.
> > The function ceil (available in the POSIX module) always does the
> > 'rounding' correctly.  For details on the difference,
> > refer to perldoc -f int .
> 
> Sure it is.  While it's not the most elegant approach possible**, it 
> produces correct results.  Run it and see, or just look below.
> 
> ** (I only picked 0.8 because I was in a hurry and didn't want to worry 
> about how the computer represented the number.  Any x where 
> 0.5 <= x < 1.0 would work, since 0.5 has an exact representation in 
> binary.)
> 
> 
> Let n be a positive integer.
> 
> Array with even length of 2n
> splice() will return int( 2n/2 + 0.8 ) = int( n + 0.8 ) = n elements
> 
> Array with odd length of 2n+1
> splice will return int( (2n+1)/2 + 0.8 ) = int(n + 0.5 + 0.8)
>     = int(n + 1.3) = n+1 elements
> 
> Just what I wanted.

So that's half the array length if it's even and 0.5 more than half the
length when it's odd: (@array + @array%2)/2

Anno


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

Date: Mon, 17 Mar 2003 22:22:56 +0100
From: "Tore Aursand" <tore@aursand.no>
Subject: Re: Where to find DBD::CSV help?
Message-Id: <pan.2003.03.17.14.00.11.532268@aursand.no>

On Mon, 17 Mar 2003 18:01:12 +0800, James Hou wrote:
> I searched the document about DBD::CSV in www.cpan.org, but I found it
> is too simple. I don't know how can I open a CSV file and retire data
> form it.

That is actually explained with a few examples in the DBD::CSV
documentation, so I expect that you didn't read it good enough;

  my $dbh = DBI->connect('DBI:CSV:csv_sep_char=,');
  $dbh->{'csv_tables'}->{'users'} = { 'file' => 'users.csv' };
  my $stUsers = $dbh->prepare('SELECT * FROM user');


-- 
Tore Aursand <tore@aursand.no>


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

Date: Mon, 17 Mar 2003 14:41:41 -0600
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Win32 Perl newsgroup?
Message-Id: <slrnb7cco5.4c3.tadmc@magna.augustmail.com>

AK <aknntp@yahoo.com> wrote:

> Is there a newsgroup dedicated to Perl on Win32?


Not that I know of. 

But there _is_ a mailing list:

   http://aspn.activestate.com/ASPN/Mail/About/perl-win32-users/


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

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


Administrivia:

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

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

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

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

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


------------------------------
End of Perl-Users Digest V10 Issue 4721
***************************************


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