[23323] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5543 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Mon Sep 22 18:05:59 2003

Date: Mon, 22 Sep 2003 15:05:14 -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           Mon, 22 Sep 2003     Volume: 10 Number: 5543

Today's topics:
    Re: "my" declarations, only matter of taste? <REMOVEsdnCAPS@comcast.net>
    Re: "my" declarations, only matter of taste? <ebohlman@earthlink.net>
    Re: () questions <grazz@pobox.com>
    Re: () questions <REMOVEsdnCAPS@comcast.net>
    Re: () questions (Anno Siegel)
    Re: () questions <ddunham@redwood.taos.com>
    Re: () questions ctcgag@hotmail.com
        Apache session <lionel.valero@polymtl.ca>
    Re: Apache session <abigail@abigail.nl>
    Re: CGI.pm <tcurrey@no.no.no.i.said.no>
    Re: CGI.pm (Malcolm Dew-Jones)
    Re: CGI.pm <ebohlman@earthlink.net>
    Re: CGI.pm <tore@aursand.no>
    Re: File Locking Follow-up ctcgag@hotmail.com
    Re: fixed width fields <abigail@abigail.nl>
    Re: Form problems, post method yeilds 0 at end of value <REMOVEsdnCAPS@comcast.net>
    Re: How fast is seek? <REMOVEsdnCAPS@comcast.net>
    Re: Insert lines in a file <REMOVEsdnCAPS@comcast.net>
    Re: length of the longest $_ in @_ <samj2@austarmetro.com.au>
    Re: length of the longest $_ in @_ <samj2@austarmetro.com.au>
    Re: length of the longest $_ in @_ <ebohlman@earthlink.net>
    Re: length of the longest $_ in @_ <mpapec@yahoo.com>
    Re: length of the longest $_ in @_ <mpapec@yahoo.com>
    Re: list-parsing problem <tore@aursand.no>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Mon, 22 Sep 2003 13:18:21 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: "my" declarations, only matter of taste?
Message-Id: <Xns93FE9166C186Fsdn.comcast@206.127.4.25>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

tadmc@augustmail.com (Tad McClellan) wrote in 
news:slrnbms9bt.1rs.tadmc@magna.augustmail.com:

> Eric J. Roode <REMOVEsdnCAPS@comcast.net> wrote:
>> Matija Papec <mpapec@yahoo.com> wrote in 
> 
>> Case (b) is generally preferred, since it can potentially save you 
>         ^
>         ^
>> headaches.
> 
> 
> I'm pretty sure Eric meant to type an "a" instead of a "b" there. 
> 
> Eric?
> 

Yes, absolutely.  My fingers choked up on the keyboard :-)

Thanks for pointing out my mistake, Tad.  I apologize, Matija.

- -- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP288vmPeouIeTNHoEQIkyQCgrkfLpDANmCVvxKfvapEfrIIlle0An2ym
sMxMxHkoML7aTPNzfFSiauff
=YHPl
-----END PGP SIGNATURE-----


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

Date: 22 Sep 2003 20:30:16 GMT
From: Eric Bohlman <ebohlman@earthlink.net>
Subject: Re: "my" declarations, only matter of taste?
Message-Id: <Xns93FE9F33F51CEebohlmanomsdevcom@130.133.1.4>

crichmon@filc8533.fm.intel.com (Chris Richmond - MD6-FDC ~) wrote in 
news:bkn5cp$qmi$1@news01.intel.com:

> Point taken.  My secondary complaint is that with strict you
> have to put 'my' or some global scope on every variable you
> use. Its more typing and just that much more line noise.

For *tiny* programs (I'd define "tiny" here as "small enough that you can 
comprehend the entire program in one glance") that might be true.  But as 
soon as you're writing anything bigger, having to think about the scope of 
your variables is a Good Thing because it saves you from many hard-to-debug 
problems.  I've seen, for example, code where a variable gets changed each 
time around a loop, and then code below the loop uses the value of the 
variable in a situation where it's clear that the very last setting of the 
variable wasn't what was wanted.  Proper scoping protects you from that 
kind of error.

And code in which all variables are essentially global in scope is 
particularly difficult to maintain, because when you change one section of 
the code you can't be sure how it will affect the rest of the code.  
"Action at a distance" is something to be avoided.  The hassle involved in 
properly scoping your variables up front is considerably less than the 
hassle of later trying to read through unrelated sections of your code in 
order to figure out whether you can change a variable without stepping on 
something else.


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

Date: Mon, 22 Sep 2003 18:07:45 GMT
From: Steve Grazzini <grazz@pobox.com>
Subject: Re: () questions
Message-Id: <RTGbb.13438$Uv2.10294@nwrdny02.gnilink.net>

Matija Papec <mpapec@yahoo.com> wrote:
> Eric J. Roode wrote
> >Matija Papec <mpapec@yahoo.com> wrote:
> 
> Well, map is a function which transforms list of values, so one could
> expect[1] at least one value on the left side of map, for each element
> coming from right. In case of empty list, () nothing gets through..?

That's right.  The block (or expression) yields a *list* of values
for each element in the input list.  A common idiom for creating
hashes transforms each input element into a key/value pair:

  my %hash = map { $_ => 1 } @input;

Returning one element is probably more common, but returning an empty
list makes sense too.  It has the effect of "filtering"... in your case,
though, grep() would work just as well, since you're not actually 
transforming the elements you keep.

> >> my $count = () = $data =~ /match/g;
> >
> >This is a semi-cheesy way of getting a list context on the right side of 
> >the expression and a scalar context on the left side.  In a list context, 
> >m//g gives a list of all matching substrings.  The left side, $count, is 
> >in scalar context, which yields the number of substrings.
> 
> This forcing of list context looks easier then @{[]} but who can tell if
> newer perls will support it?

All bets are off with Perl 6, but the current behavior of list 
assignment in scalar context is documented in perlop and widely used
(although not necessarily well-understood) so it's extremely unlikely
to disappear from Perl 5.

-- 
Steve



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

Date: Mon, 22 Sep 2003 13:16:53 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: () questions
Message-Id: <Xns93FE9126BF502sdn.comcast@206.127.4.25>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

Matija Papec <mpapec@yahoo.com> wrote in
news:d68umv4a6l4ebcp3b6r4bqppqphouddi8i@4ax.com: 

> X-Ftn-To: Eric J. Roode 
> 
>>> 1) () stands afaik for "empty list", so why it doesn't pass a map
>>> function? Is such behavior accidental or intentional?
>>> 
>>> print join ',', map $_%2 ? () : $_, 1..20;
>>
>>I do not understand what you mean by "pass a map function".  The above
> 
> "Passing a map" is what happens to $_ when it gets transformed by map
> (from right to left).
> 
>>statement prints "2,4,6,8,10,12,14,16,18,20", which makes perfect
>>sense to me.  What is puzzling you about it?
> 
> Well, map is a function which transforms list of values, so one could
> expect[1] at least one value on the left side of map, for each element
> coming from right. In case of empty list, () nothing gets through..?

Right.  Try this:

    @a = (1, 2, 3, (), 5, 6, (), 7);

The ()s disappear, because the list on the right-hand side is 
"flattened" as it is evaluated.  Were you perhaps expecting them to be 
undef?  undef is a scalar value.  Or perhaps an empty anonymous array 
ref, like []?  That too is a scalar value.

- -- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP288ZWPeouIeTNHoEQIsUwCfRs4cEA/hYF/HVB6JmlXiqOdI/HIAnAnx
ULswnFpR+7mOCcDku+M2zIHY
=CFV0
-----END PGP SIGNATURE-----


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

Date: 22 Sep 2003 18:17:47 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: () questions
Message-Id: <bknecb$ak3$1@mamenchi.zrz.TU-Berlin.DE>

Matija Papec  <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
> X-Ftn-To: Eric J. Roode 
> 
> >> 1) () stands afaik for "empty list", so why it doesn't pass a map
> >> function? Is such behavior accidental or intentional?
> >> 
> >> print join ',', map $_%2 ? () : $_, 1..20;
> >
> >I do not understand what you mean by "pass a map function".  The above 
> 
> "Passing a map" is what happens to $_ when it gets transformed by map (from
> right to left).
> 
> >statement prints "2,4,6,8,10,12,14,16,18,20", which makes perfect sense 
> >to me.  What is puzzling you about it?
> 
> Well, map is a function which transforms list of values, so one could
> expect[1] at least one value on the left side of map, for each element
> coming from right. In case of empty list, () nothing gets through..?

The idea that list elements "pass map" in some sense to build the result
list is not a useful visualization of the process.

The map operator evaluates the block (or expression) in list context, once
for each list element, and flattens the resulting lists into one big one.
If the block returns an empty list on some occasions, the result list doesn't
grow on that step.

"map" also sets $_ to each list element in turn, and the block can make
use of that, but that's no requirement.  It can entirely ignore the original
list elements.  Seen like that, they certainly don't "pass through map".

> >> 2) how to grok this expression, what exactly perl does here?
> >> 
> >> my $count = () = $data =~ /match/g;
> >
> >This is a semi-cheesy way of getting a list context on the right side of 
> >the expression and a scalar context on the left side.  In a list context, 
> >m//g gives a list of all matching substrings.  The left side, $count, is 
> >in scalar context, which yields the number of substrings.
> 
> This forcing of list context looks easier then @{[]} but who can tell if
> newer perls will support it?

It's documented behavior (in perldata), and fundamental idioms rely on
it, like "while ( my( $key, $val) = each %hash ) {".  It isn't likely
to go away.

Anno


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

Date: Mon, 22 Sep 2003 18:17:20 GMT
From: Darren Dunham <ddunham@redwood.taos.com>
Subject: Re: () questions
Message-Id: <Q0Hbb.37$IU7.9130122@newssvr21.news.prodigy.com>

Matija Papec <mpapec@yahoo.com> wrote:
> X-Ftn-To: Eric J. Roode 

> Well, map is a function which transforms list of values, so one could
> expect[1] at least one value on the left side of map, for each element
> coming from right. In case of empty list, () nothing gets through..?

An empty list does not take up a slot in a list.

The list ("a", (), "b", (), "c") has 3 elements.
The list ("a", "", "b", "", "c") has 5 elements.

perl -e '@a = ("a", (), "b", "c"); print "$a[1]\n"

-- 
Darren Dunham                                           ddunham@taos.com
Unix System Administrator                    Taos - The SysAdmin Company
Got some Dr Pepper?                           San Francisco, CA bay area
         < This line left intentionally blank to confuse you. >


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

Date: 22 Sep 2003 20:14:59 GMT
From: ctcgag@hotmail.com
Subject: Re: () questions
Message-Id: <20030922161459.490$sT@newsreader.com>

Matija Papec <mpapec@yahoo.com> wrote:
> X-Ftn-To: Eric J. Roode
>
> >> 1) () stands afaik for "empty list", so why it doesn't pass a map
> >> function? Is such behavior accidental or intentional?
> >>
> >> print join ',', map $_%2 ? () : $_, 1..20;
> >
> >I do not understand what you mean by "pass a map function".  The above
>
> "Passing a map" is what happens to $_ when it gets transformed by map
> (from right to left).
>
> >statement prints "2,4,6,8,10,12,14,16,18,20", which makes perfect sense
> >to me.  What is puzzling you about it?
>
> Well, map is a function which transforms list of values, so one could
> expect[1] at least one value on the left side of map, for each element
> coming from right. In case of empty list, () nothing gets through..?

Exactly right.  Nothing, i.e. the empty list, gets through.  If you
want something to get through, you should pass something, like undef.


Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB


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

Date: Mon, 22 Sep 2003 19:40:58 GMT
From: Lionel Valero <lionel.valero@polymtl.ca>
Subject: Apache session
Message-Id: <efIbb.24582$C92.18359@charlie.risq.qc.ca>

Hello,

I would like to handle http request session, may you give me some useful advices ?

Regards.
-- 



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

Date: 22 Sep 2003 20:12:58 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: Apache session
Message-Id: <slrnbmulua.sig.abigail@alexandra.abigail.nl>

Lionel Valero (lionel.valero@polymtl.ca) wrote on MMMDCLXXIV September
MCMXCIII in <URL:news:efIbb.24582$C92.18359@charlie.risq.qc.ca>:
==  Hello,
==  
==  I would like to handle http request session, may you give me some useful advices ?


Always wear dry socks.



Abigail
-- 
sub J::FETCH{Just   }$_.='print+"@{[map';sub J::TIESCALAR{bless\my$J,J}
sub A::FETCH{Another}$_.='{tie my($x),$';sub A::TIESCALAR{bless\my$A,A}
sub P::FETCH{Perl   }$_.='_;$x}qw/J A P';sub P::TIESCALAR{bless\my$P,P}
sub H::FETCH{Hacker }$_.=' H/]}\n"';eval;sub H::TIESCALAR{bless\my$H,H}


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

Date: Mon, 22 Sep 2003 12:11:11 -0700
From: "Trent Curry" <tcurrey@no.no.no.i.said.no>
Subject: Re: CGI.pm
Message-Id: <bknhkq$s31$1@news.astound.net>

Jeff 'japhy' Pinyan wrote:
> On Mon, 22 Sep 2003, D Borland wrote:

>
>> Maybe PHP was the better choice.
>
> If what you need is web-based stuff, by all means, use PHP.  That's
> what it's there for.

Perl works quite well for web apps too you know ;-p

-- 
Trent Curry

perl -e
'($s=qq/e29716770256864702379602c6275605/)=~s!([0-9a-f]{2})!pack("h2",$1)!eg
;print(reverse("$s")."\n");'




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

Date: 22 Sep 2003 13:36:25 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: CGI.pm
Message-Id: <3f6f5d49@news.victoria.tc.ca>

Trent Curry (tcurrey@no.no.no.i.said.no) wrote:
: Jeff 'japhy' Pinyan wrote:
: > On Mon, 22 Sep 2003, D Borland wrote:

: >
: >> Maybe PHP was the better choice.
: >
: > If what you need is web-based stuff, by all means, use PHP.  That's
: > what it's there for.

: Perl works quite well for web apps too you know ;-p

Yes, but it's at least as good to use php if you don't want to read the
perl manuals. 



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

Date: 22 Sep 2003 20:58:32 GMT
From: Eric Bohlman <ebohlman@earthlink.net>
Subject: Re: CGI.pm
Message-Id: <Xns93FEA3FDD275Bebohlmanomsdevcom@130.133.1.4>

"D Borland" <notavailable@nospamplease.com> wrote in
news:4WEbb.734$xr.21@newsfep1-gui.server.ntli.net: 

> How do i go about get a parameter list from a form, without having to
> use CGI.pm?
> 
> I was going to use this, but it's just a waste of time learning how to
> use it correctly - i mean if i wanted to read, read, read and read i
> would have just got a job in a library.  It's something that i have

Well, the alternative [1] to using CGI.pm is to read all the relevant RFCs 
that deal with HTTP, as well as the entire HTML specification.  And then 
code, code, code.  And then test, test, test.  And code some more.  And 
read some more.  Lather, rinse, and repeat.  Seems to me like a lot more 
effort than just reading the documentation for CGI.pm.

Lincoln Stein did all the work I just mentioned.  And he chose to share the 
results of that work with others, rather than keeping it to himself, so 
others wouldn't have to repeat that work.  In the Perl community we call 
this "the virtue of Laziness"; one person does some extra work, but it cuts 
down on the total amount of work the entire community has to do.  Refusing 
to put in the effort to learn something like CGI.pm is what we call "the 
vice of False Laziness" because it *increases* the total amount of work 
everybody, including yourself, winds up doing.

So take the Lazy route and learn CGI.pm!  You'll be glad you did.

[1] I'm using "alternative" to mean "a realistic different choice that 
accomplishes the same thing as the choice in question."  There are pseudo-
alternatives like cutting and pasting some badly-written code you don't 
understand from something like one of Matt Wright's early CGI scripts and 
coming up with something that might work when the phase of the moon is just 
right and you're very careful about sacrificing the goat properly.  
Building a house out of wood rather than brick is an alternative; building 
it out of cardboard is a pseudo-alternative.


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

Date: Mon, 22 Sep 2003 23:01:04 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: CGI.pm
Message-Id: <pan.2003.09.22.20.30.05.716336@aursand.no>

On Mon, 22 Sep 2003 16:53:15 +0100, D Borland wrote:
> How do i go about get a parameter list from a form, without having to use
> CGI.pm?

You don't want to _not_ use CGI.pm.  Reading - and parsing - web requests
is quite hard, but CGI.pm makes it extremely easy.

Read the CGI.pm documentation.  It takes you 15 minutes to browse through
it and learn what you're looking for.  Later on, you will have a clue
where to start reading when you encounter a CGI-related problem.


-- 
Tore Aursand <tore@aursand.no>

"You know the world is going crazy when the best rapper is white, the best
 golfer is black, France is accusing US of arrogance and Germany doesn't
 want to go to war."


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

Date: 22 Sep 2003 21:02:35 GMT
From: ctcgag@hotmail.com
Subject: Re: File Locking Follow-up
Message-Id: <20030922170235.303$9k@newsreader.com>

"\"Dandy\" Randy" <ducott@hotmail.com> wrote:
> Hey, after going through several responses to my previous enquires about
> file locking, I have opted to go with the following. Please have a look
> and advise me if i'm on the right track. Basically the script is sort of
> a hit counter, but with a re-direct. The main critisism of my previous
> scripts was having the read and write file open's non contained within a
> single lock sequence. So ... at least in my mind ... the following script
> groups the read and write within a single lock.
>
> CODE WITH COMMENTS
>
> #!/usr/bin/perl
>
> use Fcntl qw(:DEFAULT :flock);
>
> # opens a simple a dummy file that contains no data
> open (LOCKIT, ">data/lockfile.txt") or die "Can't open: $!";
> # starts main lock
> flock (LOCKIT, LOCK_EX) or die "Can't lock: $!";

I think this will work, but I can't guarantee that on some system
it won't.  I'd worry that some system may decide that, since you
are clobbering the file anyway, that it "clobbers" the old lock
along with it, so happily grants a new one.  This isn't the case
on my system (but it would be if someone happened to delete
lockfile.txt while it it was locked).

>
> # open the real data file for READ
> open (DATA, "<data/data.txt") or die "Can't open file: $!";
> # starts data READ lock
> flock (DATA, LOCK_EX) or die "Can't lock: $!";

Assuming that all programs that wish to operate on data.txt first
obtain the lock on lockfile.txt, then I can see no need to also
lock data.txt at all.  Of course, it doesn't hurt, it just seems
unnecessary.  Also, this obtains an exclusive lock on a file open
for reading only, which was the objection that someone raised to my
suggestion that you use the file as it's own semaphore, and that objection
seems equally valid here.

> # get data contents
> $data=<DATA>;
> # closes data READ lock and closes data file
> close(DATA);
>
> # advance data
> $data = $data + 1;
>
> # open the real data file for WRITE
> open (DATA, ">data/data.txt") or die "Can't open file: $!";
> # starts data WRITE lock
> flock (DATA, LOCK_EX) or die "Can't lock: $!";

Again, unless other programs lock the file itself rather than the
semaphore, there is no reason for this program to lock both files.


> # writes the advanced data to file
> print DATA $data;
> # closes data WRITE lock and closes data file
> close(DATA);
>
> # closes the main lock
> close(LOCKIT);
>
> # redirects user to the required page
> $webview="http://www.mysite.com";
> print "Location: $webview\n\n";
> exit;
>

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service              New Rate! $9.95/Month 50GB


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

Date: 22 Sep 2003 20:18:49 GMT
From: Abigail <abigail@abigail.nl>
Subject: Re: fixed width fields
Message-Id: <slrnbmum99.sig.abigail@alexandra.abigail.nl>

Jim (jg_x30@iwon.com) wrote on MMMDCLXXIV September MCMXCIII in
<URL:news:b15b026d.0309220736.571cbaae@posting.google.com>:
~~  This should be an easy one but I'm having a brain cramp.
~~  
~~  I have a string: 2-s1.0-49FR-WSJ0-TWC1-XXXX-12345-00.  I want to
~~  replace the 12345 with $i.  When $i equals "1" how can I make it
~~  "00001"?


   $i   =  1;
   $str = "2-s1.0-49FR-WSJ0-TWC1-XXXX-12345-00";
   substr $str => 27, 5 => sprintf "%05d" => $i;


Abigail
-- 
BEGIN {$^H {join "" => ("a" .. "z") [8, 13, 19, 4, 6, 4, 17]} = sub
           {["", "Just ", "another ", "Perl ", "Hacker\n"] -> [shift]};
       $^H = hex join "" => reverse map {int ($_ / 2)} 0 .. 4}
print 1, 2, 3, 4;


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

Date: Mon, 22 Sep 2003 14:41:37 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Form problems, post method yeilds 0 at end of values
Message-Id: <Xns93FE9FA6FE782sdn.comcast@206.127.4.25>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

"David Mills" <mills_nospam_@free.fr> wrote in
news:pan.2003.09.22.14.40.29.44104@free.fr: 

> First of all hello to the group

Hi.  Welcome.


> #get info from form, lifted from http://www.cgi101.com, great site

It can't possibly be a great site if it gave you this shitty code.


> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

What if the form is submitted via GET method?

> print "$buffer\n";
> print "\n";
> @pairs = split(/\n/, $buffer);

CGI variables are not separated by newlines. They are separated by 
ampersands or semicolons.

> foreach $pair (@pairs) {
>     ($name, $value) = split(/=/, $pair);

What if the value contains an equals sign?

>     $value =~ tr/+/ /;
>     $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
>     $FORM{$name} = $value;
>     $FORM{$name} =~ s/\0//eg;
> print "$name = $FORM{$name}t<br>";#Puts a 0 between the variable and
> the t }


Do this instead:

    use CGI;
    my $q = new CGI;
    foreach my $p ($q->param)
    {
        print "[$p] => [", $q->param($p), "]<br />\n";
    }

and see if those zeroes are still there.  If so, they came in from the 
form submission, and are not the fault of your program.

- -- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP29QcmPeouIeTNHoEQICeACg3aRlw3wpuBCj+qUgKZ0eT0GX9rkAoLEt
RfupQUyWFvruaRKK6hYt5R7f
=eHYM
-----END PGP SIGNATURE-----


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

Date: Mon, 22 Sep 2003 14:47:27 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: How fast is seek?
Message-Id: <Xns93FEA0A428FAsdn.comcast@206.127.4.25>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

altalingua@hotmail.com (David Morel) wrote in 
news:60c4a7b1.0309211108.7034f6a7@posting.google.com:

> seek FILEHANDLE,POSITION,WHENCE
> 
> How fast is seek?
> 
> I am using the seek function on some large files, for simplicity. Are
> there any benchmarks on the speed of this function (WHENCE = 0)?

In my experience (unix), seek just modifies an internal counter and an 
internal flag, and doesn't touch the file at all.  So it has next to zero 
execution time.  If a filehandle is open for write, it may or may not need 
to do a flush before the seek, so that may slow down certain seeks.

What's your real question?

- -- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP29R0GPeouIeTNHoEQK07QCdEBiUoCy2IXnzoV48fRK/9/v5pjYAoKTO
K0ZGMnmhbNQ76NgSgZzYmDmm
=67RF
-----END PGP SIGNATURE-----


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

Date: Mon, 22 Sep 2003 14:51:05 -0500
From: "Eric J. Roode" <REMOVEsdnCAPS@comcast.net>
Subject: Re: Insert lines in a file
Message-Id: <Xns93FEA141C9885sdn.comcast@206.127.4.25>

-----BEGIN xxx SIGNED MESSAGE-----
Hash: SHA1

tabletdesktop@yahoo.com (MJS) wrote in 
news:f0171209.0309212212.43eb5cbc@posting.google.com:

> I can't figure out with whats wrong with this code. Please help.
> It doesn't produce the required result.
> 
 ...
> # open for update
> open(FILE, '+<', 'data.txt') or die "Can't open the file for update:
> $!";
> 
> # tie @array to filename using Tie::File
> tie @array, 'Tie::File', data.txt or die $!;

Either tie the file *or* open it yourself; don't do both.

- -- 
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN xxx SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP29SqmPeouIeTNHoEQIPWwCggZyRUmAIJyJppeLRgRf2p4SpgVsAoPjt
WdxbEprGPHnVjtW/XsTfXxCb
=CD4n
-----END PGP SIGNATURE-----


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

Date: Mon, 22 Sep 2003 18:51:41 GMT
From: Sam <samj2@austarmetro.com.au>
Subject: Re: length of the longest $_ in @_
Message-Id: <3F6F4697.5050302@austarmetro.com.au>

Matija Papec wrote:
> On Mon, 22 Sep 2003 08:22:03 GMT, Sam <samj2@austarmetro.com.au>
> wrote:
> 
> 
>>Hello .... again :)
>>after I search my book and the online hlep. trying to find it there is a 
>> built-in function to get the length of the longest item in a givin 
>>array. length will not do it and scalar @array will not do it. I can 
>>loop through the array but just wanted to ask if there is a built in 
>>function for it. and how would you know if there is a built-in-function 
>>for a task you want to do? or is it by time-experiance combo?
> 
> 
> You'll have to loop through the array,
> #untested
> my $max = 0;
> $_>$max && $max=$_ for map length, @arr;
> 
> 
> or if you more prefer,
> for (map length, @arr) {
>   $_>$max && $max=$_;
> }
> 


I have 10 arrays in my program and want to do this in all of them. my 
approch would be

#to store the result of the loop in separate sclars for each array

my ($arr1_max, $arr2_max, $arr(n)_max)=0;

#is there a perl variable which holds a list of all the arrays in a 
#program? instead of doing this comming line

for my $arr (\@arr1, \@arr2, \@arr(n)) {
  my $uni_max = '$','the name of the variable inside the $arr in this 
case arr1, how can I do this?','_max';	#builds a string which is the variable to hold 
the result of the inside loop
for (map length, @{$arr}) {
   $uni_max=$_ if $_>$uni_max;
}

print $arr1_max;
print $arr2_max;
print $arr(n)_max;



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

Date: Mon, 22 Sep 2003 19:33:11 GMT
From: Sam <samj2@austarmetro.com.au>
Subject: Re: length of the longest $_ in @_
Message-Id: <3F6F5051.7040903@austarmetro.com.au>

Barry Kimelman wrote:
> [This followup was posted to comp.lang.perl.misc]
> 
> In article <3F6EB301.6080302@austarmetro.com.au>, samj2
> @austarmetro.com.au says...
> 
>>Hello .... again :)
>>after I search my book and the online hlep. trying to find it there is a 
>>  built-in function to get the length of the longest item in a givin 
>>array. length will not do it and scalar @array will not do it. I can 
>>loop through the array but just wanted to ask if there is a built in 
>>function for it. and how would you know if there is a built-in-function 
>>for a task you want to do? or is it by time-experiance combo?
>>
>>thanks
>>
>>
> 
> 
> $maxlen = (sort { $b <=> $a } map { length $_ } @names)[0];

that is very clean, thanks alot



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

Date: 22 Sep 2003 20:20:22 GMT
From: Eric Bohlman <ebohlman@earthlink.net>
Subject: Re: length of the longest $_ in @_
Message-Id: <Xns93FE9D868780ebohlmanomsdevcom@130.133.1.4>

Matija Papec <mpapec@yahoo.com> wrote in 
news:rretmvsvombhmiemf0ijrho35462dtkcbe@4ax.com:

> or if you more prefer,
> for (map length, @arr) {
>   $_>$max && $max=$_;

$max=$_ if $_>$max;

is more Perlish.  Your form strikes me as writing sh in Perl.

> }



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

Date: Mon, 22 Sep 2003 23:21:14 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: length of the longest $_ in @_
Message-Id: <3rpumvgav04v8nrhaeabn1kcipla0ncui2@4ax.com>

X-Ftn-To: Eric Bohlman 

Eric Bohlman <ebohlman@earthlink.net> wrote:
>Matija Papec <mpapec@yahoo.com> wrote in 
>news:rretmvsvombhmiemf0ijrho35462dtkcbe@4ax.com:
>
>> or if you more prefer,
>> for (map length, @arr) {
>>   $_>$max && $max=$_;
>
>$max=$_ if $_>$max;
>
>is more Perlish.  Your form strikes me as writing sh in Perl.

I'm sorry. :) Actually the former doesn't compile so it must be,
$_>$max and $max=$_;
or
$max=$_ if $_>$max;
as you pointed out.



-- 
Matija


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

Date: Mon, 22 Sep 2003 23:21:15 +0200
From: Matija Papec <mpapec@yahoo.com>
Subject: Re: length of the longest $_ in @_
Message-Id: <rbpumvkvk5b03ri595p5sk187e1k5sdgcv@4ax.com>

X-Ftn-To: Sam 

Sam <samj2@austarmetro.com.au> wrote:
>I have 10 arrays in my program and want to do this in all of them. my 
>approch would be
>
>#to store the result of the loop in separate sclars for each array
>
>my ($arr1_max, $arr2_max, $arr(n)_max)=0;
>
>#is there a perl variable which holds a list of all the arrays in a 
>#program? 

I'm afraid there is no such thing, but you can use array of arrays; I see
you're used to references,

#untested
my @arrofarr = (\@arr1, \@arr2, \@arr3);
my @max = (0) x (scalar @arrofarr);

for my $i (0 .. $#arrofarr) {

  for (map length, @{ $arrofarr[$i] }) {
    $max[$i]=$_ if $_>$max[$i];
  }
}
print "Max lengths are: ", join ",", @max;

hope this helps.


-- 
Matija


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

Date: Mon, 22 Sep 2003 23:01:04 +0200
From: Tore Aursand <tore@aursand.no>
Subject: Re: list-parsing problem
Message-Id: <pan.2003.09.18.09.44.33.911309@aursand.no>

On Thu, 18 Sep 2003 02:19:52 -0700, Marcus Claesson wrote:
> I have a list like this:
> 
> 1	a
> 2	b
> 2	c
> 3	a
> 4	d
> 4	d
> 4	e
> 4	f
> 5	g
> 
> and I want to make the first column non-redundant and collect the
> second column values on the same line, like this:
> 
> 1	a
> 2	b,c
> 3	a
> 4	d,e,f
> 5	g

#!/usr/bin/perl
#
use strict;
use warnings;

my %hash = ();
while ( <DATA> ) {
    chomp;
    my @columns = split( /\s+/ );
    $hash{$columns[0]}->{$columns[1]}++;
}

my @sorted = sort {
    $hash{$a} <=> $hash{$b}
} keys %hash;

foreach ( @sorted ) {
    print $_ . "\t" . join(', ', sort keys %{$hash{$_}}) . "\n";
}

__DATA__
1	a
2	b
2	c
3	a
4	d
4	d
4	e
4	f
5	g


-- 
Tore Aursand <tore@aursand.no>

"You know the world is going crazy when the best rapper is white, the best
 golfer is black, France is accusing US of arrogance and Germany doesn't
 want to go to war."


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

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


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