[24818] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 6971 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Sep 7 18:06:18 2004

Date: Tue, 7 Sep 2004 15:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Tue, 7 Sep 2004     Volume: 10 Number: 6971

Today's topics:
    Re: GD.pm make test failed, help please? (Philip M. Gollucci)
    Re: Help with POD in Win98 (activeperl) <warewolfe@hotmail.com>
    Re: list of numbers <tore@aursand.no>
    Re: list of numbers <abigail@abigail.nl>
    Re: list of numbers (gumby)
    Re: list of numbers <mritty@gmail.com>
    Re: list of numbers <tore@aursand.no>
        LWP::UserAgent and 8bit characters (T Conti)
        Newbie 777/ 755 only allowed on server (javatiger)
    Re: Newbie 777/ 755 only allowed on server <mritty@gmail.com>
    Re: Newbie 777/ 755 only allowed on server <noreply@gunnar.cc>
    Re: RE-Redirecting STDOUT (aisarosenbaum)
        search & replace (Venkatesh Babu Sira)
    Re: search & replace <mritty@gmail.com>
    Re: search & replace <tadmc@augustmail.com>
    Re: Shared memory for data structures <clandos@yahoo.com>
    Re: Shared memory for data structures <NOmustardSPAMsauce@NOwarmroomSPAM.com>
        splice or split an array <clandos@yahoo.com>
    Re: splice or split an array <nobull@mail.com>
    Re: splice or split an array <sbryce@scottbryce.com>
        Taking a reference to a calculated value <chris@cjx.com>
    Re: Taking a reference to a calculated value <uri@stemsystems.com>
    Re: Taking a reference to a calculated value <dwall@fastmail.fm>
    Re: Taking a reference to a calculated value <mritty@gmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: 7 Sep 2004 13:56:50 -0700
From: pgollucci@gmail.com (Philip M. Gollucci)
Subject: Re: GD.pm make test failed, help please?
Message-Id: <58be0b3d.0409071256.5a75f58a@posting.google.com>

Hi,
> ok 8 # Skip, FreeType changes too frequently to be testable
This is okay
> ok 9 # Skip, no XPM support
This is okay
> not ok 10
This is a problem with GD::Image->newFromJpeg('frog.jpg')


you can always skip make test
and install it... Then figure out if what doesn't work is even something you need.



"bopew2000" <bopew2000@yahoo.com> wrote in message news:<2T7%c.101853$A8.1617@edtnps89>...
> Hi guys,
> 
> I am trying to install GD.pm 2.15 on a Fedora 2 box. The make went fine, but
> I got this when I do make test.
> Any help please? Thanks!
> 
> 
> # make test TEST_VERBOSE=1
> PERL_DL_NONLAZY=1 /usr/bin/perl "-MExtUtils::Command::MM" "-e"
> "test_harness(1, 'blib/lib', 'blib/arch')" t/*.t
> t/GD..........1..10
> ok 1
> not ok 2
> not ok 3
> not ok 4
> not ok 5
> not ok 6
> not ok 7
> ok 8 # Skip, FreeType changes too frequently to be testable
> ok 9 # Skip, no XPM support
> not ok 10
> FAILED tests 2-7, 10
>         Failed 7/10 tests, 30.00% okay (less 2 skipped tests: 1 okay,
> 10.00%)
> t/Polyline....1..1
> # Running under perl version 5.008003 for linux
> # Current time local: Mon Sep  6 17:59:25 2004
> # Current time GMT:   Tue Sep  7 00:59:25 2004
> # Using Test.pm version 1.24
> ok 1
> ok
> Failed Test Stat Wstat Total Fail  Failed  List of Failed
> ----------------------------------------------------------------------------
> ---
> t/GD.t                    10    7  70.00%  2-7 10
> 2 subtests skipped.
> Failed 1/2 test scripts, 50.00% okay. 7/11 subtests failed, 36.36% okay.
> make: *** [test_dynamic] Error 255


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

Date: Tue, 07 Sep 2004 20:34:28 GMT
From: "Harris" <warewolfe@hotmail.com>
Subject: Re: Help with POD in Win98 (activeperl)
Message-Id: <oXo%c.23003$N77.1051167@news.xtra.co.nz>

Paul Lalli wrote:

> Harris wrote:
> 
> > I'm trying to use the POD translators...

> Did you try the syntax specified by both
> perldoc pod2text
> and
> pod2text -h
> ?
> 
> They say that the output file should be specified on the command
> line, right after the input file...
> 
> Let us know the results of that command, please.
> 
> Paul Lalli

Thanks a lot for that, my mistake was to try and use '|' and '>' to
direct the output of Pod2text to the input file. The correct
commandline call was "pod2text input.pl output.txt" rather than
"pod2text input.pl > output.txt".


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

Date: Tue, 07 Sep 2004 17:31:50 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: list of numbers
Message-Id: <pan.2004.09.07.15.31.49.599445@aursand.no>

On Tue, 07 Sep 2004 15:19:57 +0100, Andrew Moffat wrote:
> I have lots of files with three columns of numbers e.g.
> 
> 1    2    3
> 4.1   5    6
> 7    8    9
> .....etc
> 
> I want to extract the third column e.g.
> 
> 3
> 6
> 9
> ....etc
> 
> The problem is that I can't just use a colrm type function because the 
> numbers in the first 2 columns aren't always the same number of figures (as 
> above). Each number is seperated by a tab. Can anybody think of a way around 
> this please.

Hmmm.  This is a tough one.  How can I use Perl to split() something?  I
know I've read about that somewhere, but where...?  Was it in the Perl
documentation?  Nah - that would've been too obvious, don't you think? 
Yeah, I won't even bother to check...


-- 
Tore Aursand <tore@aursand.no>
"Nothing is certain but death and taxes. Of the two, taxes happen
 annually." (Joel Fox)


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

Date: 07 Sep 2004 19:01:29 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: list of numbers
Message-Id: <slrncjs1c9.j2h.abigail@alexandra.abigail.nl>

Andrew Moffat (ajmoffat@soton.ac.uk) wrote on MMMMXXV September MCMXCIII
in <URL:news:chkg25$ajd$1@aspen.sucs.soton.ac.uk>:
??  Hi,
??  
??  I have lots of files with three columns of numbers e.g.
??  
??  1    2    3
??  4.1   5    6
??  7    8    9
??  .....etc
??  
??  I want to extract the third column e.g.
??  
??  3
??  6
??  9
??  ....etc
??  
??  The problem is that I can't just use a colrm type function because the 
??  numbers in the first 2 columns aren't always the same number of figures (as 
??  above). Each number is seperated by a tab. Can anybody think of a way around
??  this please. TIA.

    $ cut  -f 3                                      file1 file2 file3 ...
    $ awk  -f '{print $3}'                           file1 file2 file3 ...
    $ perl -F/\t/ -alne 'print $F [2]'               file1 file2 file3 ...
    $ sed  -e 's/^[^\t]*\t[^\t]*\t//' -e 's/\t.*//'  file1 file2 file3 ...



Abigail
-- 
perl -wle 'print prototype sub "Just another Perl Hacker" {};'


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

Date: 7 Sep 2004 12:18:49 -0700
From: georgeziv@hotmail.com (gumby)
Subject: Re: list of numbers
Message-Id: <878baabf.0409071118.2c938977@posting.google.com>

Sandman <mr@sandman.net> wrote in message news:<mr-12FACF.16420807092004@individual.net>...
> In article <chkg25$ajd$1@aspen.sucs.soton.ac.uk>,
>  "Andrew Moffat" <ajmoffat@soton.ac.uk> wrote:
> 
> > Hi,
> > 
> > I have lots of files with three columns of numbers e.g.
> > 
> > 1    2    3
> > 4.1   5    6
> > 7    8    9
> > .....etc
> > 
> > I want to extract the third column e.g.
> > 
> > 3
> > 6
> > 9
> > ....etc
> > 
> > The problem is that I can't just use a colrm type function because the 
> > numbers in the first 2 columns aren't always the same number of figures (as 
> > above). Each number is seperated by a tab. Can anybody think of a way around 
> > this please. TIA.
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> 
> my $data = "1\t2\t3
> 4.1\t5\t6
> 7\t8\t9";
> 
> foreach (split /\n/, $data){
>     my @cols = split /\t/;
>     print $cols[2] . "\n";
> }
> 
> __END__
> 3
> 6
> 9


Here is another of of attacking the problem.  I realize this may get
out of hand if you have quite a few columns but its just a thought.
the test file contains the following data:

1	2	3
4.1	5	6
7	8	9

The Code:

unless(open FILE, test)
{
    print "Failed to open the test file ($!).\n";
}

while(<FILE>)
{
    my $line = $_;
    $line =~ /([0-9.]+)\s+([0-9.]+)\s+([0-9]+)/;
    print "$1 and $2 and $3\n";
}

close FILE;

The Output:
1 and 2 and 3
4.1 and 5 and 6
7 and 8 and 9

As  you can see $(number) is the column you want as you rip through
the file.  You can push the column onto an array using something like
push(@array, $3) and that would push the contents of each line in
column three onto @array.  There is probably a \(letter) that will do
decimal numbers i know \d does digits.  Anyway hope this helps
-gumby


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

Date: Tue, 07 Sep 2004 19:40:24 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: list of numbers
Message-Id: <I8o%c.1489$sS4.1101@trndny03>

"gumby" <georgeziv@hotmail.com> wrote in message
news:878baabf.0409071118.2c938977@posting.google.com...
> Sandman <mr@sandman.net> wrote in message
news:<mr-12FACF.16420807092004@individual.net>...
> > In article <chkg25$ajd$1@aspen.sucs.soton.ac.uk>,
> >  "Andrew Moffat" <ajmoffat@soton.ac.uk> wrote:
> >
> > > Hi,
> > >
> > > I have lots of files with three columns of numbers e.g.
> > >
> > > 1    2    3
> > > 4.1   5    6
> > > 7    8    9
> > > .....etc
> > >
> > > I want to extract the third column e.g.
> > >
> > > 3
> > > 6
> > > 9
> > > ....etc
> Here is another of of attacking the problem.  I realize this may get
> out of hand if you have quite a few columns but its just a thought.

That's precisely why you should use split instead of RegExps in this
case.  Someone wise once said "Use Regexps when you know what you want
to save, use split when you know what you want to throw away."  You
don't (necessarily) know what you want to save - how many columns there
are.  You do know what you want to throw away - all the space between
the fields, regardless of how many there are.

> the test file contains the following data:
>
> 1 2 3
> 4.1 5 6
> 7 8 9
>
> The Code:
>
> unless(open FILE, test)
> {
>     print "Failed to open the test file ($!).\n";
> }

If the file fails to open, you print a message to STDOUT, and then keep
going with the program?  This should be

open my $file, 'test' or die "Failed to open the test file ($!).";

>
> while(<FILE>)
while (<$file>)
> {
>     my $line = $_;
>     $line =~ /([0-9.]+)\s+([0-9.]+)\s+([0-9]+)/;

why reassign $_ to $line if all you're doing is pattern matching it one
statement later?

>     print "$1 and $2 and $3\n";

Never, ever use $1, $2, $3 unless you know the pattern match succeeded.
You never check for that.

Another reason to not use regexps here:  What if the numbers are in the
format such as -0.9?  How about 5e4?  Again, you know what you want to
throw away, you don't know what you want to keep.

> }
>
> close FILE;
>
> The Output:
> 1 and 2 and 3
> 4.1 and 5 and 6
> 7 and 8 and 9
>
> As  you can see $(number) is the column you want as you rip through
> the file.  You can push the column onto an array using something like
> push(@array, $3) and that would push the contents of each line in
> column three onto @array.  There is probably a \(letter) that will do
> decimal numbers i know \d does digits.

No, there's not.  There is a module on the CPAN that will allow you to
specify exactly what kinds of numbers you want, however.
Regexp::Common, I believe.

> Anyway hope this helps
> -gumby

Paul Lalli




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

Date: Tue, 07 Sep 2004 21:49:19 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: list of numbers
Message-Id: <pan.2004.09.07.19.49.18.351438@aursand.no>

On Tue, 07 Sep 2004 12:18:49 -0700, gumby wrote:
> Here is another of of attacking the problem.  I realize this may get out
> of hand if you have quite a few columns but its just a thought. the test
> file contains the following data:
> 
> 1	2	3
> 4.1	5	6
> 7	8	9
> 
> The Code:
> 
> unless(open FILE, test)
> {
>     print "Failed to open the test file ($!).\n";
> }

Why continue if there's no point in continuing?  Why should you even _try_
to read from the file if you didn't succeed in opening it?

> while(<FILE>)
> {
>     my $line = $_;
>     $line =~ /([0-9.]+)\s+([0-9.]+)\s+([0-9]+)/;
>     print "$1 and $2 and  $3\n";
> }

Blah.  A hairy regular expression instead of the easy split() function?
And why assign $_ to $line, instead of directly?  And what happens if the
regular expression doesn't match?

  while ( <FILE> ) {
      if ( ... ) {
          # match
      }
  }


-- 
Tore Aursand <tore@aursand.no>
"I didn't have time to write a short letter, so I wrote a long one
 instead." (Mark Twain)


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

Date: 7 Sep 2004 14:07:19 -0700
From: tconti@hotmail.com (T Conti)
Subject: LWP::UserAgent and 8bit characters
Message-Id: <5e0d3eda.0409071307.772957c1@posting.google.com>

Hello:

I am using LWP::UserAgent to make a vanilla request to a web service. 
The return value is an XML file that may contain utf-8 characters. 
However when I debug my module I see that the return test is getting
mangled.  I can call the web service from IE and everything works
fine.  Here is the code I am using to make the HTTP call:

my $ua = new LWP::UserAgent;
my $request = new HTTP::Request('GET', $queryURL);
my $response = $ua->request($request);
my $string = $response->content;

The resulting string slams umlates among other things.  Here is a
sample of the garbled return data:

<Location>DE-Baden W&#9500;&#9565;rttemberg-76137 Karlsruhe</Location>

The proper return data should be:
 <Location>DE-Baden Württemberg-Obersontheim</Location>

The return XML is utf-8 encoded. The headers of the response object
seem to reflect this:

$VAR1 = bless( {
                 'client-response-num' => '1',
                 'x-aspnet-version' => '1.1.4322',
                 'content-type' => 'text/xml; charset=utf-8',
                 'connection' => 'close',
                 'date' => 'Tue, 07 Sep 2004 20:54:00 GMT',
                 'server' => 'Microsoft-IIS/5.0',
                 'client-date' => 'Tue, 07 Sep 2004 20:54:00 GMT',
                 'content-length' => '18835',
                 'cache-control' => 'private',
                 'x-powered-by' => 'ASP.NET'
               }, 'HTTP::Headers' );


Any thoughts on how to handle this?

Thanks,
T


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

Date: 7 Sep 2004 11:09:13 -0700
From: tigermott@yahoo.com (javatiger)
Subject: Newbie 777/ 755 only allowed on server
Message-Id: <962e2a34.0409071009.4b064343@posting.google.com>

My server only allows 755 permissions I'm not sure exactly what it
means.

I want to setup a forum, will users be able to write to the fourum or
just read what I have put in?


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

Date: Tue, 07 Sep 2004 18:23:09 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Newbie 777/ 755 only allowed on server
Message-Id: <h0n%c.15040$Cr5.7024@trndny08>

"javatiger" <tigermott@yahoo.com> wrote in message
news:962e2a34.0409071009.4b064343@posting.google.com...
> My server only allows 755 permissions I'm not sure exactly what it
> means.
>
> I want to setup a forum, will users be able to write to the fourum or
> just read what I have put in?

This really has absolutely nothing to do with Perl, and everything to do
with your server's configuration.  You should probably talk to your
system administrator for assistance.  In general, however, you would
never want 'group' and 'world' to have full write permissions.

Paul Lalli




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

Date: Tue, 07 Sep 2004 21:44:21 +0200
From: Gunnar Hjalmarsson <noreply@gunnar.cc>
Subject: Re: Newbie 777/ 755 only allowed on server
Message-Id: <2q6hjmFrj6cgU1@uni-berlin.de>

javatiger wrote:
> My server only allows 755 permissions I'm not sure exactly what it 
> means.
> 
> I want to setup a forum, will users be able to write to the fourum
> or just read what I have put in?

If the program running the forum is running as you, 755 is sufficient.
(If the server does not allow 777, that's probably the case.)

But, as Paul said, this is not the right place for discussing (or
speculating about) it. Talk to your hosting provider.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl


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

Date: 7 Sep 2004 13:06:35 -0700
From: aisarosenbaum@gmail.com (aisarosenbaum)
Subject: Re: RE-Redirecting STDOUT
Message-Id: <d844a2d1.0409071206.5bf19143@posting.google.com>

Brian McCauley <nobull@mail.com> wrote in message news:<ch7r2j$q1e$1@slavica.ukpost.com>...
> aisarosenbaum@yahoo.com wrote:
> > No I'm not a stuttering typist.  ;^)
> > 
> > I'm in the pecular position of working in an environment in which
> > STDOUT has been rudely redirected by a script (A) that runs my 
> > scripts (B).  I want to take STDOUT back without knowing the 
> > handle to which it was redirected.  I've found a lot of advise 
> > on redirecting STDOUT, but I need to re-redirect it back to the 
> > console from 'B' without hacking 'A'.
> > 
> > (This is on Solaris)
> 
> On Unix-like OS opening the virual device /dev/tty opens the current 
> session's controling terminal.
> 
> This, of course, has nothing to do with Perl.

Thanks, this works:

open( STDOUT, ">/dev/tty" );

Any ideas for a MS-portable solution?

Aisa


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

Date: 7 Sep 2004 11:27:15 -0700
From: vsira@hotmail.com (Venkatesh Babu Sira)
Subject: search & replace
Message-Id: <d5b8dfd2.0409071027.460d99bb@posting.google.com>

Greats,

I want to replace basepath="   to   basepath="${archive.path} 

How can i achieve this.

It would be pretty easy if there are no special characters.

TIA
-Venakt


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

Date: Tue, 07 Sep 2004 18:35:04 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: search & replace
Message-Id: <sbn%c.2877$2H5.2501@trndny07>

"Venkatesh Babu Sira" <vsira@hotmail.com> wrote in message
news:d5b8dfd2.0409071027.460d99bb@posting.google.com...
> Greats,
>
> I want to replace basepath="   to   basepath="${archive.path}
>
> How can i achieve this.
>
> It would be pretty easy if there are no special characters.

You haven't given anywhere near enough information to help you.  You
don't have any actual Perl variables, you haven't told us what method
you're using to determine what needs to be searched, nor what needs to
be the replacement.

Have you read the Posting Guidelines which are posted to this group
regularly?

Regardless, I shall make use of the Psi::ReadMind module to determine
that you want to make use of the quotemeta function, which is usually
used by way of the \Q and \E escape sequences.  You can read about at
perldoc -f quotemeta

Paul Lalli




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

Date: Tue, 7 Sep 2004 16:52:09 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: search & replace
Message-Id: <slrncjsbc9.41f.tadmc@magna.augustmail.com>

Venkatesh Babu Sira <vsira@hotmail.com> wrote:

> I want to replace basepath="   to   basepath="${archive.path} 


   s/basepath="/\Qbasepath="${archive.path}/;
or
   s/basepath="/basepath="\${archive.path}/;


> It would be pretty easy if there are no special characters.


It is pretty easy _with_ special characters too, simply
remove their specialness via backslashing.


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


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

Date: Tue, 7 Sep 2004 18:07:03 +0200
From: "Peter" <clandos@yahoo.com>
Subject: Re: Shared memory for data structures
Message-Id: <413ddcab$0$290$4d4ebb8e@read.news.de.uu.net>


"Yash" <yashgt@yahoo.com> schrieb im Newsbeitrag
news:5a373b1d.0409070657.427ece20@posting.google.com...
> In our Active Perl 5.8 script on Windows 2000, we cache a number of
> database tables in memory in hashes, and use them for lookups to be
> performed on records being read from a number of files. To make use of
> a multiprocessor environment, we spawn multiple processes using fork.
> This causes multiple copies of the cache being maintained. We have to
> update the cache during the processing in certain cases and hence
> would like to keep a single copy so that all processes see the same
> lookup data.
> Is there a way to let Perl keep some of the hash tables in shared
> memory? Can somebody provide relevant pointers/suggestions?
>
> Thanks
> Yash

I have the same problem and I looked around.
Ther is a shared memory modul in cpan called "Net-Shared". As far as I
eveluated it, it can share scalars only.
I solved my problem using pipes and recreate the senders-hases on the
receivers side.
Another possibility is to share a text file using open.

If you ever get a good solution, let me know!

Peter




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

Date: Tue, 07 Sep 2004 16:45:06 GMT
From: Ray Baxter <NOmustardSPAMsauce@NOwarmroomSPAM.com>
Subject: Re: Shared memory for data structures
Message-Id: <mAl%c.434$xA1.189@newsread3.news.pas.earthlink.net>

Peter wrote:

> "Yash" <yashgt@yahoo.com> schrieb im Newsbeitrag
> news:5a373b1d.0409070657.427ece20@posting.google.com...
> 
>>In our Active Perl 5.8 script on Windows 2000, we cache a number of
>>database tables in memory in hashes, and use them for lookups to be
>>performed on records being read from a number of files. To make use of
>>a multiprocessor environment, we spawn multiple processes using fork.
>>This causes multiple copies of the cache being maintained. We have to
>>update the cache during the processing in certain cases and hence
>>would like to keep a single copy so that all processes see the same
>>lookup data.
>>Is there a way to let Perl keep some of the hash tables in shared
>>memory? Can somebody provide relevant pointers/suggestions?
>>
>>Thanks
>>Yash
> 
> 
> I have the same problem and I looked around.
> Ther is a shared memory modul in cpan called "Net-Shared". As far as I
> eveluated it, it can share scalars only.
> I solved my problem using pipes and recreate the senders-hases on the
> receivers side.
> Another possibility is to share a text file using open.
> 
> If you ever get a good solution, let me know!

Yash,

Why not short circuit all of this and use the database as your cache? 
The database designers have dealt with all of the problems that you are 
going to have to deal with otherwise.  What are you going to do about 
multiple processes accessing the same data? How are you going to perform 
the updates? What about reading the date while the update is in process? 
How is this going to scale?

If you use the database, all of this comes for free; otherwise you are 
going to have to reinvent many wheels.

If your database lookups are expensive (really the only justification 
for the approach you are taking with the hash caches) then you probably 
need to redesign your data model or your queries.

--
Ray


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

Date: Tue, 7 Sep 2004 18:12:58 +0200
From: "Peter" <clandos@yahoo.com>
Subject: splice or split an array
Message-Id: <413de382$0$290$4d4ebb8e@read.news.de.uu.net>

Hello,
I try to split an array into a couple of arrays the same size.

This is my code and I think its very ugly ;-)

Does anoyone has a better idea?

@list =
("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17"
,"18");

$pack =5;

$avg = $#list / $pack + 1;

for(1..$pack+1)
{
 @newlist = splice(@list,0,$avg) if @list;

 print "@newlist\n";
}




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

Date: Tue, 07 Sep 2004 17:56:59 +0100
From: Brian McCauley <nobull@mail.com>
Subject: Re: splice or split an array
Message-Id: <chkp0q$mme$1@sun3.bham.ac.uk>

Peter wrote:
> Hello,
> I try to split an array into a couple of arrays the same size.

"A couple" is 2.  I think you mean "an arbitrary number".

On course this is only possible if there are an exact multiple of that 
number of elements in the original array.  What do you want to happen if 
this condition doesn't hold?  I assume from your code you want the last 
sublist to be short.

> This is my code and I think its very ugly ;-)

I don't think it's _very_ ugly.  It seems basically right - you use 
splice(@list,0,$sublist_size).

I would scatter in a few 'my's in appropriate places.

I'd remove the the redundant 'if @list'.  In general you should avoid 
having an if that skips over a bit of code in the condition where the 
bit of code wouldn't do anything at anyhow (unless there is a 
significant performance issue).

I'd probably use a while rather than a for.

I'd use more mnemonic variables.


> Does anoyone has a better idea?
> 
> @list =
> ("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17"
> ,"18");
> 
> $pack =5;
> 
> $avg = $#list / $pack + 1;
> 
> for(1..$pack+1)
> {
>  @newlist = splice(@list,0,$avg) if @list;
> 
>  print "@newlist\n";
> }

while( my @newlist = splice(@list,0,$sublist_size)) {
  print "@newlist\n";
}

I can't really comment if your calculation is right since we don't know 
your objectives.



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

Date: Tue, 07 Sep 2004 11:24:35 -0600
From: Scott Bryce <sbryce@scottbryce.com>
Subject: Re: splice or split an array
Message-Id: <10jrrmjq5o342e1@corp.supernews.com>

Peter wrote:

> @list =
> ("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17"
> ,"18");

Double quotes are neither necessary, nore desireable here.

my @list = qw(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18);

or

my @list = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18);

or

my @list = (1 .. 18);



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

Date: Tue, 07 Sep 2004 19:00:06 +0100
From: Chris Allen <chris@cjx.com>
Subject: Taking a reference to a calculated value
Message-Id: <pan.2004.09.07.18.00.06.390361@cjx.com>

I'm really stuck on the syntax for this one. 

I want to do the equivalent of:

%a=('a'=>1,'b'=>2,'c'=>3);
@temp=keys %a;
$b=\@temp;

But I can't find a single statement that will
do it. I've tried:

$b= \(keys %a); 
@$b = keys %a;
$b=\@{keys %a};

They don't do the above - and actually I don't understand
why they do what they do...

Can anybody help?






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

Date: Tue, 07 Sep 2004 18:11:09 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Taking a reference to a calculated value
Message-Id: <x7oekij6gm.fsf@mail.sysarch.com>

>>>>> "CA" == Chris Allen <chris@cjx.com> writes:

  CA> I'm really stuck on the syntax for this one. 
  CA> I want to do the equivalent of:

  CA> %a=('a'=>1,'b'=>2,'c'=>3);
  CA> @temp=keys %a;
  CA> $b=\@temp;

  CA> But I can't find a single statement that will
  CA> do it. I've tried:

  CA> $b= \(keys %a); 
  CA> @$b = keys %a;
  CA> $b=\@{keys %a};

  CA> They don't do the above - and actually I don't understand
  CA> why they do what they do...

anonymous arrays are your friend:

	$b = [ keys %a ] ;

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs  ----------------------------  http://jobs.perl.org


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

Date: Tue, 07 Sep 2004 18:14:15 -0000
From: "David K. Wall" <dwall@fastmail.fm>
Subject: Re: Taking a reference to a calculated value
Message-Id: <Xns955D90D4AA2E3dkwwashere@216.168.3.30>

Chris Allen <chris@cjx.com> wrote in message 
<news:pan.2004.09.07.18.00.06.390361@cjx.com>:

> I'm really stuck on the syntax for this one. 
> 
> I want to do the equivalent of:
> 
> %a=('a'=>1,'b'=>2,'c'=>3);
> @temp=keys %a;
> $b=\@temp;

$b = [ keys %a ];

will do it.

See perlref for detailed information, and perlreftut for a tutorial 
intro to references.

I realize that the above is just sample code, but just in case, here 
is a reference about why enabling strictures ('use strict;') is a 
Good Thing: http://perlmonks.org/index.pl?node_id=111088

Also, $a and $b are used as the default input variables to Perl's 
sort function, so it's best to avoid them. (Besides that, $a and $b 
aren't very descriptive names.)



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

Date: Tue, 07 Sep 2004 18:21:34 GMT
From: "Paul Lalli" <mritty@gmail.com>
Subject: Re: Taking a reference to a calculated value
Message-Id: <O_m%c.1470$sS4.1165@trndny03>

"Chris Allen" <chris@cjx.com> wrote in message
news:pan.2004.09.07.18.00.06.390361@cjx.com...
> I'm really stuck on the syntax for this one.
>
> I want to do the equivalent of:
>
> %a=('a'=>1,'b'=>2,'c'=>3);
> @temp=keys %a;
> $b=\@temp;

$b = [ keys %a ];

The above creates an anonymous array reference, and populates the
referenced array with the list of values returned by the keys function

> But I can't find a single statement that will
> do it. I've tried:
>
> $b= \(keys %a);

Here you're generating a list of values, in a (seemingly) random order.
On my machine, that list came out to ('c', 'a', 'b').  The \ operator is
actually being applied to all three elements, effectively creating this
statement:

$b = (\'c', \'a', \'b');

The comma operator used in scalar context returns the last element
evaluated, so you're left with $b being set equal to a reference to the
string 'b'.

> @$b = keys %a;

This should work, but is a bit ugly.  You're telling Perl $b is an array
reference, and then assigning the list of values returned by keys to the
array referenced by $b.   How did this "not work" for you?

> $b=\@{keys %a};

This is trying to evaluate whatever is returned by the keys function as
an array reference, dereference it, and then take reference to the
result.  I'm not surprised that didn't work.  I am surprised it didn't
cause a fatal error, however.  I suspect Perl is trying to do something
similar to the first case above, but I'm not positive.

> They don't do the above - and actually I don't understand
> why they do what they do...

Hope this helps,
Paul Lalli




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

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.  

NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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


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