[13719] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 1129 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Oct 20 11:05:25 1999

Date: Wed, 20 Oct 1999 08:05:10 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <940431910-v9-i1129@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Wed, 20 Oct 1999     Volume: 9 Number: 1129

Today's topics:
    Re: $ is sneaky, point me to a faq or help please (Bart Lateur)
    Re: $ is sneaky, point me to a faq or help please (Eric Bohlman)
    Re: $ is sneaky, point me to a faq or help please (Abigail)
    Re: Abigail Markov (was Re: Ignore the idiots) <bowman@montana.com>
    Re: ActivePerl/NT: crypt() not implemented ??? <bmurch@cfcg.er.usgs.gov>
    Re: array <msalter@bestweb.net>
    Re: Beginner question - chomp (Tad McClellan)
    Re: Can't get Perl working with Apache <kperrier@blkbox.com>
    Re: Card shuffling <NukeEmUp@ThePentagon.com>
    Re: Card shuffling <rhomberg@ife.ee.ethz.ch>
    Re: Card shuffling <bmurch@cfcg.er.usgs.gov>
    Re: Creating files in perl scripts on NT (Kragen Sitaker)
    Re: dec to bin conversion and vice versa <sb@sdm.de>
    Re: Further Musings <dan@tuatha.sidhe.org>
    Re: help with substrings (Helgi Briem)
    Re: help with substrings <jeffp@crusoe.net>
    Re: help with substrings <lr@hpl.hp.com>
        How to get file size? <acacia@online.no>
    Re: How to get file size? <vincent.murphy@cybertrust.gte.com>
        How to use cperl-mode in emacs <p146046183@ntu.edu.sg>
    Re: How to use cperl-mode in emacs (Brett W. McCoy)
    Re: How to use cperl-mode in emacs <vincent.murphy@cybertrust.gte.com>
    Re: Ignore the idiots (Brett W. McCoy)
    Re: linux perl editor? <kperrier@blkbox.com>
    Re: Matching an asterisk (Brett W. McCoy)
    Re: New Perl/TK Tutorial on perl.com <lusol@Pandora.CC.Lehigh.EDU>
    Re: Passing function as parameter. (Eric Bohlman)
    Re: Passing function as parameter. (Brett W. McCoy)
    Re: Passing function as parameter. <gellyfish@gellyfish.com>
    Re: Perl certification? <latsharj@my-deja.com>
    Re: Perl certification? (Brett W. McCoy)
        Please help with format allnightprod@my-deja.com
    Re: Problems adding to and deleting from an array (Brett W. McCoy)
    Re: Program to use cgi with out a server? lil help (Brett W. McCoy)
    Re: REGEXP rewind, backreference? <webresearch@indy-soft.com>
    Re: SGML/HTML parsing tool (Randal L. Schwartz)
    Re: Unix command ´tree´ in Perl? <rhomberg@ife.ee.ethz.ch>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Wed, 20 Oct 1999 12:59:40 GMT
From: bart.lateur@skynet.be (Bart Lateur)
Subject: Re: $ is sneaky, point me to a faq or help please
Message-Id: <380dbc99.130446@news.skynet.be>

Abigail wrote:

>Why the backwacking of the dashes? Dashes aren't special, unless in
>character classes.
>
>        ($mess, @attach) = split /--\Q$boundary/ => $message;

	print quotemeta('--');
-->
	\-\-

-- 
	Bart.


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

Date: 20 Oct 1999 13:48:32 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: $ is sneaky, point me to a faq or help please
Message-Id: <7ukh7g$553$6@nntp3.atl.mindspring.net>

Bart Lateur (bart.lateur@skynet.be) wrote:
: Abigail wrote:
: 
: >Why the backwacking of the dashes? Dashes aren't special, unless in
: >character classes.
: >
: >        ($mess, @attach) = split /--\Q$boundary/ => $message;
: 
: 	print quotemeta('--');
: -->
: 	\-\-

quotemeta doesn't know whether or not its argument is going to be used in 
a character class, so it errs on the safe side.



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

Date: 20 Oct 1999 09:02:24 -0500
From: abigail@delanet.com (Abigail)
Subject: Re: $ is sneaky, point me to a faq or help please
Message-Id: <slrn80riqv.fji.abigail@alexandra.delanet.com>

Bart Lateur (bart.lateur@skynet.be) wrote on MMCCXLI September MCMXCIII
in <URL:news:380dbc99.130446@news.skynet.be>:
__ Abigail wrote:
__ 
__ >Why the backwacking of the dashes? Dashes aren't special, unless in
__ >character classes.
__ >
__ >        ($mess, @attach) = split /--\Q$boundary/ => $message;
__ 
__ 	print quotemeta('--');
__ -->
__ 	\-\-


Your point being?



Abigail
-- 
$_ = "\x3C\x3C\x45\x4F\x54";
print if s/<<EOT/<<EOT/e;
Just another Perl Hacker
EOT


  -----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 73,000 Newsgroups - Including  Dedicated  Binaries Servers ==-----


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

Date: Tue, 19 Oct 1999 21:40:08 -0600
From: bowman <bowman@montana.com>
Subject: Re: Abigail Markov (was Re: Ignore the idiots)
Message-Id: <380D3998.B4687BC2@montana.com>

Jeff Pinyan wrote:
> 
> Abigail is a Markov chain program written in Perl.  Abigail has had posts
> by the following persons sent through itself:

It took Kernighan & Pike almost 20 lines to do the job in the Perl
version of their Markov program. I hope the true Perl Hackers were more
efficient than a couple of broken down old C dinosaurs.


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

Date: Wed, 20 Oct 1999 10:14:00 -0400
From: Brock Murch <bmurch@cfcg.er.usgs.gov>
Subject: Re: ActivePerl/NT: crypt() not implemented ???
Message-Id: <380DCE28.98241A72@cfcg.er.usgs.gov>

harris_m@my-deja.com wrote:

> I don't get from your posting how are you logging as admin. I have
> installed ActiveState Perl and crypt() function works just fine with me.
>
> Are you using use strict ?
>
> May be you can try using ::crypt() instead of just crypt()
> Harris M.
>
> In article <38041F2E.EA36E423@heart-line.de>,
>   Jens Onnen <onnen@heart-line.de> wrote:
> > Hi !
> >
> > I am using ActivePerl on a NT3.51-system.
> > After implementing a perl forum software package, all seemed to work
> > fine. But when I tried to log in as admin, the following message
> > appeared:
> >
> > "The crypt() function is unimplemented due to excessive paranoia. at
> > D:/xxx/yyy/source/src-board-subs-admin line 20."
> >
> > Please help me !
> >
> > Thank you in advance,
> >
> > Jens Onnen.
> >
> >
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.

Sounds like you have a stripped down perl interpreter (like the one that
comes with sambar server) I had the same error too. Solution: To change the
server config file to have path to interpreter to where active state is
installed..... i.e. c:\perl\bin\perl....

Hope this helps,
Brock




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

Date: Wed, 20 Oct 1999 14:23:43 GMT
From: Mike Salter <msalter@bestweb.net>
Subject: Re: array
Message-Id: <Pine.BSF.4.05.9910201021190.3889-100000@monet.bestweb.net>

On 19 Oct 1999, Dan Nguyen wrote:

DN>Mike Salter <msalter@bestweb.net> wrote:
DN>
DN>: @ar = map {$_ if !/wuuh/} @ar;
DN>
DN>That would be correct if you wanted to replace wuuh with "";
DN>So @arr[2] eq "" is true.  If you wanted to completely remove the item
DN>from the list, and not replace it with "" then you would probably want
DN>to use grep.

You are correct - sorry bout that folks!

Actually, my first response to the question was going to be 'the BKSP
key'.  ;-)




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

Date: Tue, 19 Oct 1999 22:26:51 -0400
From: tadmc@metronet.com (Tad McClellan)
Subject: Re: Beginner question - chomp
Message-Id: <b99ju7.lfd.ln@magna.metronet.com>

Boon Hoo (theebh@magix.com.sg) wrote:

: I understand that chomp removes newline from the end of a string. 


   Actually it removes the "line ending sequence" (whatever $/ is
   set to).

   So it removes different things on different platforms.


: I was
: trying it one day to format some HTML files(grab each line, remove
: newline, and write it to a text-file), when I found out that if I had
: earlier "ftp" the files using binary mode, chomp doesn't seem to work -
: i saw many  ^M control characters 


   Those are Carriage Return characters.


: instead in my output file. What is the
: correct way to remove newlines from textfiles transferred through binary
: mode?


   The correct way is to not transfer text files in binary mode  :-)

   Then the ftp program will convert the line endings for you.


   But since you have already hauled your Windoze files over,
   you can write your own dos2unix converter:

      perl -p -i.dos -e 'tr/\r//d'  filename...


--
    Tad McClellan                          SGML Consulting
    tadmc@metronet.com                     Perl programming
    Fort Worth, Texas


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

Date: 20 Oct 1999 09:36:25 -0500
From: Kent Perrier <kperrier@blkbox.com>
Subject: Re: Can't get Perl working with Apache
Message-Id: <495C12984DD3390B.60A42AC3F98CE90B.EBAB6A088A25A2D2@lp.airnews.net>

kev <kporter@clara.co.uk> writes:

> 
> Sorry, in my rush I posted the wrong lines from the log file. Here's the real
> one:
> 
> "...script not found or unable to stat: /home/httpd/cgi-bin/perl1.pl"
> 
> The script does have that path and the permissions are fine, and it begins
> with a #!
> 
> - Kev

You error doesn't support your statement that the cgi is in /home/httpd/cgi-bin

What is the permissions of the file?  755?

Kent
-- 
Groovy! Smashing! Yay capitalism!

					- Austin Powers, Man of Mystery


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

Date: Wed, 20 Oct 1999 15:05:36 +0100
From: David Cantrell <NukeEmUp@ThePentagon.com>
Subject: Re: Card shuffling
Message-Id: <dssNOBJjOMXHh9I0=01oWXSPVJPp@4ax.com>

On 20 Oct 1999 07:33:25 -0500, abigail@delanet.com (Abigail) said:

>Nevertheless, something has to be wrong with your reading skills.
>There is a link called "FAQs" on the top half of http://www.perl.com.
>Granted, it's an image, but it does have a decent ALT attribute.

Unfortunately that link does nothing useful right now ...

[david@tempest david]$ telnet www.perl.com 80
Trying 208.201.239.50...
Connected to www.perl.com.
Escape character is '^]'.
GET /pub/v/faqs HTTP/1.0

HTTP/1.1 404 Not Found
Date: Wed, 20 Oct 1999 14:05:44 GMT
Server: Apache/1.3.6 (Unix)
Connection: close
Content-Type: text/html

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>404 Not Found</TITLE>
</HEAD><BODY>
<H1>Not Found</H1>
The requested URL /pub/v/faqs was not found on this server.<P>
<HR>
<ADDRESS>Apache/1.3.6 Server at salt.songline.com Port 80</ADDRESS>
</BODY></HTML>
Connection closed by foreign host.

[Copying newsgroup posts to me by mail is considered rude]

-- 
David Cantrell, part-time Unix/perl/SQL/java techie
                full-time chef/musician/homebrewer
                http://www.ThePentagon.com/NukeEmUp


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

Date: Wed, 20 Oct 1999 16:26:40 +0200
From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
Subject: Re: Card shuffling
Message-Id: <380DD120.D8D8388B@ife.ee.ethz.ch>

Mikko Saari wrote:
> 
> I need a perl script to handle decks of cards. My first and most obvious
> problem is shuffling, a problem which has bothered me previously. How do I
> arrange an array in random order?

It is not possible with any pseudo random generator.

A standard deck with 36 cards (as used in Switzerland)
has 36! possible arrangements, which is more than
2^138, so the pseudo random generator would have to have at least 140
bits
for 52 card decks, we get 225 bits.

using Perl rand() to shuffle a 52 card deck, you will get only 4000
million different permutations, which is less than one of every
1000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000
possibilities

- Alex


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

Date: Wed, 20 Oct 1999 10:55:30 -0400
From: Brock Murch <bmurch@cfcg.er.usgs.gov>
Subject: Re: Card shuffling
Message-Id: <380DD7E2.30F03221@cfcg.er.usgs.gov>

Alex Rhomberg wrote:

> Mikko Saari wrote:
> >
> > I need a perl script to handle decks of cards. My first and most obvious
> > problem is shuffling, a problem which has bothered me previously. How do I
> > arrange an array in random order?
>
> It is not possible with any pseudo random generator.
>
> A standard deck with 36 cards (as used in Switzerland)
> has 36! possible arrangements, which is more than
> 2^138, so the pseudo random generator would have to have at least 140
> bits
> for 52 card decks, we get 225 bits.
>
> using Perl rand() to shuffle a 52 card deck, you will get only 4000
> million different permutations, which is less than one of every
> 1000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000_000
> possibilities
>
> - Alex

Now I don't claim to be an experienced programmer (less than 1 year) but I did
see something once that did seem to work. Load the cards into an array in order
and don't shuffle it. Use the rand to select one of the 52 array elements and
as you do so remove that element from the array. Might work in theory? When all
the elements are gone the deck is dealt.  Then only have to randomly select one
of 52 to start and decrease from there......

If this is out to lunch, I'm sorry!

Brock




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

Date: Wed, 20 Oct 1999 14:07:23 GMT
From: kragen@dnaco.net (Kragen Sitaker)
Subject: Re: Creating files in perl scripts on NT
Message-Id: <v0kP3.21301$E_1.1187125@typ11.nn.bcandid.com>

In article <Pine.GSO.4.10.9910201432140.25930-100000@droid.fit.qut.edu.au>,
DAVID GODFREY  <n2142945@droid.fit.qut.edu.au> wrote:
>it doesn't create the files that it is meant to write to.  No error
>messages are given.  I am guessing that the problem may be to do with NT
>permissions.  I would appreciate if anyone has any ideas if they can reply
>
>The code I use to open the file is
>
>$outputfile="massmin.log";
>
>open(OUT_FILE,">>$outputfile");                                                

If you want error messages, you need to check to see whether open()
failed, and if it did, print an error message explaining why.  e.g. or
die "couldn't open $outputfile: $!\n".


-- 
<kragen@pobox.com>       Kragen Sitaker     <http://www.pobox.com/~kragen/>
Tue Oct 19 1999
21 days until the Internet stock bubble bursts on Monday, 1999-11-08.
<URL:http://www.pobox.com/~kragen/bubble.html>


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

Date: 20 Oct 1999 14:02:36 GMT
From: Steffen Beyer <sb@sdm.de>
Subject: Re: dec to bin conversion and vice versa
Message-Id: <7uki1s$s15$1@solti3.sdm.de>

In article <380cd1b4.0@bandit>, Thuan Pham <phamt1@gusun.georgetown.edu> wrote:

> I would like to know if anyone out there has a Perl program/subroutine 
> that does the conversion from decimal to binary and vice versa that I can take
> look at?

> Any help would be greatly appreciated.

From my

Suggestion for the perlfaq4 manpage:
------------------------------------

How do I convert from hexadecimal to decimal:

1) $dec = 0xDEADBEEF;
2) $dec = hex("DEADBEEF");
3) $dec = unpack("N", pack("H8", substr("0" x 8 . "DEADBEEF", -8)));
4) use Bit::Vector;
   $vec = Bit::Vector->new_Hex(32, "DEADBEEF");
   $dec = $vec->to_Dec();

How do I convert from decimal to hexadecimal:

1) $hex = sprintf("%X", 3735928559);
2) $hex = unpack("H*", pack("N", 3735928559));
3) use Bit::Vector;
   $vec = Bit::Vector->new_Dec(32, -559038737);
   $hex = $vec->to_Hex();

How do I convert from octal to decimal:

1) $dec = 033653337357; # note the leading '0'
2) $dec = oct("33653337357");
3) use Bit::Vector;
   $vec = Bit::Vector->new(32);
   $vec->Chunk_List_Store(3, split(//, reverse "33653337357"));
   $dec = $vec->to_Dec();

How do I convert from decimal to octal:

1) $oct = sprintf("%o", 3735928559);
2) use Bit::Vector;
   $vec = Bit::Vector->new_Dec(32, -559038737);
   $oct = reverse join('', $vec->Chunk_List_Read(3));

How do I convert from binary to decimal:

1) $dec = unpack("N", pack("B32",
      substr("0" x 32 . "11011110101011011011111011101111", -32)));
2) use Bit::Vector;
   $vec = Bit::Vector->new_Bin(32, "11011110101011011011111011101111");
   $dec = $vec->to_Dec();

How do I convert from decimal to binary:

1) $bin = unpack("B*", pack("N", 3735928559));
2) use Bit::Vector;
   $vec = Bit::Vector->new_Dec(32, -559038737);
   $bin = $vec->to_Bin();

The remaining transformations (e.g. hex -> oct, bin -> hex, etc.)
are left as an exercise to the inclined reader. :-)

(Note: The advantage of the Bit::Vector module is that it works with
numbers of ANY size.)

Hope this helps.

Regards,
-- 
    Steffen Beyer <sb@engelschall.com>
    http://www.engelschall.com/u/sb/whoami/ (Who am I)
    http://www.engelschall.com/u/sb/gallery/ (Fotos Brasil, USA, ...)
    http://www.engelschall.com/u/sb/download/ (Free Perl and C Software)


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

Date: Wed, 20 Oct 1999 15:35:37 GMT
From: Dan Sugalski <dan@tuatha.sidhe.org>
Subject: Re: Further Musings
Message-Id: <djlP3.1723$IZ5.25733@news.rdc1.ct.home.com>

Craig Berry <cberry@cinenet.net> wrote:
> Wyzelli (wyzelli@yahoo.com) wrote:
> : Craig Berry <cberry@cinenet.net> wrote in message
> : news:s0p7q2hcr0166@corp.supernews.com...
> : > Not true.  On Unix-ish systems, the epoch is midnight Jan 1 1970 UTC.
> : > localtime returns years since 1900.  Big difference.
> : 
> : I guess that varies by system, as I recall that on Mac's the epoch is a
> : different date again, so that further confuses things!

> That's why I specfied Unix-ish, and even that may hide some weaseling
> (Every Un*x variant I've ever met uses the 1970 epoch, but I haven't met
> 'em all).  Under VMS it's some date around 1860 IIRC.  And so forth.

November 17, 1858, FWIW. The Smithsonian base date. The native time
format is a quadword of 100ns ticks since base, guaranteed to cause
us some sort of Y30K problem. And Y10K problem, when all the four
digit years roll. (There is, however, a bug report in for it and
VMS engineering is expected to address the problem before then...:)

VMS Perl, however, uses the Unix secs-since-Jan-1970 date format.

				Dan

(For the interested, it is possible to translate a VMS quadword
date to Unix epoch seconds entirely in perl without any loss of
precision through 2100AD IIRC. I think I posted the code a while
back, so a DejaNews search oughta find it)


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

Date: Wed, 20 Oct 1999 13:14:26 GMT
From: helgi@NOSPAMdecode.is (Helgi Briem)
Subject: Re: help with substrings
Message-Id: <380dbf16.503314858@frettir.simnet.is>

On Mon, 18 Oct 1999 16:22:18 -0400, Kevin Monroe
<kpmonroe@unity.ncsu.edu> wrote:

>For some reason this relatively easy problem has been stumping me all
>day.  Any help would be appreciated as I am beginning to lose my mind
>
>I have a comma deliminated text file such as
>
>121,Jimmy,Jones,0,1,21,555,89
>1547,James,Johnson,155,87,6,1,301
>14,Me,Again,12,197,0,22,1
>
Here is a better version:

#!/usr/local/bin/perl -w

open(NEW, "< /path/to/file.txt") or die "Can't open
file.txt: $!";

@data = <NEW>;

foreach $element(@data){
   @line = split(/,/, $element);
	print "$line[1] ";
	print "$line[2]\n";
	}

close NEW;


>What I would like to be able to do here is get the say 1st,3rd and 5th
>entries following the comma.  However $data[0] reveals the first full
>line commas and all.  And since the length of each field varies using
>subst($line,0,1) will not work either
>
You are mixing up your variables.

@data is the whole input file.

@data[0] is the first line,
@data[1] is the second line
and so on.

$element equals @data[i] for each i 
as you run through the foreach loop.

$line is $element split into an array by commas.

$line[0] is the first entry on the line,
$line[1] is the second and so on.


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

Date: Wed, 20 Oct 1999 09:40:50 -0400
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: help with substrings
Message-Id: <Pine.GSO.4.10.9910200929250.21020-100000@crusoe.crusoe.net>

=cut

=begin FOR HUMANS

This message can be sent through pod2text (or any other converter).  It is
readable in this format, though.  You can check

  perldoc perlpod

for more information on this handy documentation markup.

=end FOR HUMANS

=head1 SYNOPSIS

I want to print specific fields from the comma-separated lines of a file.

=head1 DESCRIPTION

The data looks something like:

  121,Jimmy,Jones,0,1,21,555,89
  1547,James,Johnson,155,87,6,1,301
  14,Me,Again,12,197,0,22,1

Helgi suggested a program that works like so:

  > #!/usr/local/bin/perl -w
  > 
  > open(NEW, "< /path/to/file.txt") or die "Can't open file.txt: $!";
  > 
  > @data = <NEW>;
  > 
  > foreach $element(@data){
  >   @line = split(/,/, $element);
  >   print "$line[1] ";
  >   print "$line[2]\n";
  > }
  > 
  > close NEW;

This can be made more efficient.  You needn't slurp the file into an
array, you can operate on the lines of the file one at a time without the
array.  The for-loop can also be made shorter and save the use of the
@line array.

=head1 EXAMPLES

=head2 Better Loop

  open FILE, "file" or die "can't open file: $!";
  print join(" ", (split /,/)[1,2]), "\n" while <FILE>;
  close FILE;

This loop does the same thing the for-loop did, without the @data,
$element, and @line variables.  It uses less memory by storing only one
line of the file in a variable at a time, AND the split statement only
splits the line into 4 segments (due to magic), as opposed to splitting it
on every single comma.  This is due to Perl's internal magic.

=head2 Array Element vs. Array Slice

  > @data[0] is the first line,
  > @data[1] is the second line

Well, @data[0] is an array SLICE of one element.  The preferred (read:
correct) syntax is $data[0].  Using @data[0] while B<-w> is on will alert
you of your typo.

=head1 AUTHOR

-- 

  MIDN 4/C PINYAN, USNR, NROTCURPI
  jeff pinyan      japhy@pobox.com
  perl stuff       japhy+perl@pobox.com
  CPAN ID: PINYAN  http://www.perl.com/CPAN/authors/id/P/PI/PINYAN/



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

Date: Wed, 20 Oct 1999 07:45:59 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: help with substrings
Message-Id: <MPG.12777581917bc32d98a0ce@nntp.hpl.hp.com>

In article <380dbf16.503314858@frettir.simnet.is> on Wed, 20 Oct 1999 
13:14:26 GMT, Helgi Briem <helgi@NOSPAMdecode.is> says...
> On Mon, 18 Oct 1999 16:22:18 -0400, Kevin Monroe
> <kpmonroe@unity.ncsu.edu> wrote:
 ...
> >I have a comma deliminated text file such as
> >
> >121,Jimmy,Jones,0,1,21,555,89
> >1547,James,Johnson,155,87,6,1,301
> >14,Me,Again,12,197,0,22,1
> >
> Here is a better version:
> 
> #!/usr/local/bin/perl -w

It is best to add 'use strict;' to force declaration of all variables.  
This saves innumerable difficult-to-track-down typos and other errors 
caused by implicitly creating variables with global scope.
 
> open(NEW, "< /path/to/file.txt") or die "Can't open
> file.txt: $!";
> 
> @data = <NEW>;
> 
> foreach $element(@data){

Reading an entire file into an array in memory, then looping over it, is 
not required when the file is processed one line at a time.  This is a 
poor practice, which should not be shown in example code such as this.

I posted the following excerpt from perlop yesterday.  Maybe it should 
be posted every day, until people learn to do it right.

If a <FILEHANDLE> is used in a context that is looking for a list, a 
list consisting of all the input lines is returned, one line per list 
element. It's easy to make a LARGE data space this way, so use with 
care. 

>    @line = split(/,/, $element);
> 	print "$line[1] ";
> 	print "$line[2]\n";
> 	}
> 
> close NEW;
> 
> 
> >What I would like to be able to do here is get the say 1st,3rd and 5th
> >entries following the comma.  However $data[0] reveals the first full
> >line commas and all.  And since the length of each field varies using
> >subst($line,0,1) will not work either
> >
> You are mixing up your variables.
> 
> @data is the whole input file.
> 
> @data[0] is the first line,
> @data[1] is the second line
> and so on.

That is not true.  Those are array slices.  The first line is $date[0], 
which is a scalar; etc.  The '-w' flag warns about using an array slice 
when you mean to use a scalar.

> $element equals @data[i] for each i 
> as you run through the foreach loop.

No, $element equals $data[i] as discussed above.

> $line is $element split into an array by commas.

No, @line is $element split into an array by commas.
 
> $line[0] is the first entry on the line,
> $line[1] is the second and so on.

That is correct.

It is nice that you are trying to be helpful, but some precision in 
thought and terminology would be even more helpful.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


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

Date: Wed, 20 Oct 1999 16:17:26 +0100
From: "Acacia" <acacia@online.no>
Subject: How to get file size?
Message-Id: <R7kP3.7340$7G2.39737@news1.online.no>

I need a simple code for retrieving the file size (in bytes and kB,
prefferably put into 2 different variables, i.e. $bytes and $kb) of
a specified file.

For instance, how to get the size of file.zip ...
------------------

$url = http://www.get.my/file.zip

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

Please help.

Acacia




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

Date: Wed, 20 Oct 1999 14:53:25 GMT
From: Vincent Murphy <vincent.murphy@cybertrust.gte.com>
Subject: Re: How to get file size?
Message-Id: <xjgemeqytga.fsf@gamora.ndhm.gtegsc.com>

>>>>> "Acacia" == Acacia  <acacia@online.no> writes:

    Acacia> I need a simple code for retrieving the file size (in bytes and kB,
    Acacia> prefferably put into 2 different variables, i.e. $bytes and $kb) of
    Acacia> a specified file.

perldoc -f stat
    Acacia> For instance, how to get the size of file.zip ...
    Acacia> ------------------

    Acacia> $url = http://www.get.my/file.zip

Example:

perl -e '$size=(stat("/tmp/core"))[7]; print "size of /tmp/core is $size bytes\n"'

This requires that there is a core file in /tmp though, and not everyone is
lucky enough to have one of these. :-)


--Vinny


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

Date: Wed, 20 Oct 1999 21:51:33 +0800
From: Lin Yong <p146046183@ntu.edu.sg>
Subject: How to use cperl-mode in emacs
Message-Id: <380DC8E5.730759A8@ntu.edu.sg>

Hi,

I use emacs to write my perl program in Linux.  I found there is a
cperl-mode.el package for Perl in emacs.  But, I don't know how to use
it and how to setup it in .emacs.  Can anyone help me?  Thanks a lot.

Daniel


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

Date: Wed, 20 Oct 1999 14:21:36 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: How to use cperl-mode in emacs
Message-Id: <slrn80rk81.gep.bmccoy@moebius.foiservices.com>

Also Sprach Lin Yong <p146046183@ntu.edu.sg>:

>I use emacs to write my perl program in Linux.  I found there is a
>cperl-mode.el package for Perl in emacs.  But, I don't know how to use
>it and how to setup it in .emacs.  Can anyone help me?  Thanks a lot.

Emacs usually comes with that package pre-configured and pre-loaded.

-- 
Brett W. McCoy                             bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek)   http://www.foiservices.com
FOI Services, Inc./DIOGENES                301-975-0110
---------------------------------------------------------------------------


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

Date: Wed, 20 Oct 1999 14:44:19 GMT
From: Vincent Murphy <vincent.murphy@cybertrust.gte.com>
Subject: Re: How to use cperl-mode in emacs
Message-Id: <xjgiu42ytvh.fsf@gamora.ndhm.gtegsc.com>

>>>>> "Daniel" == Lin Yong <p146046183@ntu.edu.sg> writes:

    Daniel> Hi,
    Daniel> I use emacs to write my perl program in Linux.  I found there is a
    Daniel> cperl-mode.el package for Perl in emacs.  But, I don't know how to use
    Daniel> it and how to setup it in .emacs.  Can anyone help me?  Thanks a lot.

in your .emacs set up you auto-mode-alist:

;; Set up of individual files.  The extension will tell what mode that I want.
(setq 
 auto-mode-alist '(
        ("\\.[bB][aA][tT]$"                                      . cperl-mode)
        ("\\.\\(prl\\|pl\\|pm\\|cgi\\)\\'"                       . cperl-mode)))

more info do a C-h a auto-mode-alist in any emacs buffer.

BTW,  comp.emacs would have been the newsgroup to post to.

--vjm


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

Date: Wed, 20 Oct 1999 13:48:55 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: Ignore the idiots
Message-Id: <slrn80riao.gep.bmccoy@moebius.foiservices.com>

Also Sprach emlyn_a@my-deja.com <emlyn_a@my-deja.com>:

>You must remember that although some of the questions we have could be
>answered with a book, we're not always sure which is the BEST way to do
>something, and the only way to find that out is by asking people who
>use the language all the time. I've found that the online references
>and books on the subject are over-rated: they cram too much information
>into a single explanation (often including other stuff in an example
>that is unneccessary), so we never actually find out what the CORE of
>the function/expression/whatever is.

That's what 'perldoc' is for.  I understand the sentiment when you are
really stuck on something, but it seems the same questions (ones that are
answered in one of several FAQs that are distributed with Perl) keep
getting asked over and over.  I've only been in this newsgroup for a week
or so, and it's as if the summer reruns are showing.  I'm by no means an
expert (I've only been doing Perl for less than a year), but whenever I
open up slrn, I've got the Camel Book in my lap and terminal window open
ready to have 'perldoc' typed into it.

-- 
Brett W. McCoy                             bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek)   http://www.foiservices.com
FOI Services, Inc./DIOGENES                301-975-0110
---------------------------------------------------------------------------


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

Date: 20 Oct 1999 09:39:14 -0500
From: Kent Perrier <kperrier@blkbox.com>
Subject: Re: linux perl editor?
Message-Id: <9F14DBE86892E0AC.8A4EE14EE7DF4D48.68E20403C6D13178@lp.airnews.net>

bmccoy@foiservices.com (Brett W. McCoy) writes:

> Also Sprach Marc H. Robards <mhrobards@nospam.tasc.com>:
> 
> >Anyone have any recommendations for a perl editor under Linux?  Or is emacs
> >or vim the way to go?  I've just started using Linux, so any suggesstions
> >would be appreciated.
> 
> emacs or vi are the way to go.

I like echo.

/home/kent$ echo "#!/usr/bin/perl -w" > newscript
/home/kent$ echo "use strict;" >> newscript
 ...

:)
Kent
-- 
Groovy! Smashing! Yay capitalism!

					- Austin Powers, Man of Mystery


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

Date: Wed, 20 Oct 1999 13:39:02 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: Matching an asterisk
Message-Id: <slrn80rho7.gep.bmccoy@moebius.foiservices.com>

Also Sprach Abigail <abigail@delanet.com>:

>-- text =~ /.*\*$/;
>
>That's going to fail on "foo\nbar*" (false negative).
>
>It's also going to fail on "foo*\n" (false positive).

*smack* Forgot about newlines.  Dang.

-- 
Brett W. McCoy                             bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek)   http://www.foiservices.com
FOI Services, Inc./DIOGENES                301-975-0110
---------------------------------------------------------------------------


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

Date: 20 Oct 1999 14:07:56 GMT
From: "Stephen O. Lidie" <lusol@Pandora.CC.Lehigh.EDU>
Subject: Re: New Perl/TK Tutorial on perl.com
Message-Id: <7ukibs$1jis@fidoii.cc.Lehigh.EDU>

Ilya Zakharevich <ilya@math.ohio-state.edu> wrote:
> [A complimentary Cc of this posting was sent to 
> <netwit2@my-deja.com>],
> who wrote in article <7ujhss$ggj$1@nnrp1.deja.com>:
>> Check out the new Perl/TK tutorial at   http://www.perl.com

> All I see is

>                                                   Essential Perl/TK Programming

>    [first.gif] [prev.gif] [1]Next [2]Last [space.gif] [3]Index [4]Home
>    [5][USEMAP:img001.gif]

>                                Slide 1 of 31

> Does not look very educative...

Yup.  Mannually link to the *next* page, then the slide show starts.  My only
real complaint is that a lot of time was spent on menus, but using the old
Tk4 style of simulating a menubar by packing Menubttons in a Frame, rather
than the Tk8 style ....


> Ilya
__
Stephen.O.Lidie@Lehigh.EDU
Lehigh University Computing Center, USA


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

Date: 20 Oct 1999 13:56:48 GMT
From: ebohlman@netcom.com (Eric Bohlman)
Subject: Re: Passing function as parameter.
Message-Id: <7ukhn0$553$7@nntp3.atl.mindspring.net>

xxaxx@my-deja.com wrote:
: I am attempting to write a generic directory scan subroutine which will
: run a specified function for each file found.

Sounds like File::Find.

: 
: The recursive directory scan works fine. And I can do stuff to each of
: the files found. But at the moment I have to write a different set of
: subroutines for each type of job I want to accomplish.
: 
: In c I would just pass in the point to the function, then after a few
: minutes (or hours) of syntax chasing the routine would use the parameter
: passed function on each occurance of the found files.
: 
: How would this be done in perl?

By passing a reference to a sub; look at how File::Find does it.



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

Date: Wed, 20 Oct 1999 14:03:09 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: Passing function as parameter.
Message-Id: <slrn80rj5e.gep.bmccoy@moebius.foiservices.com>

Also Sprach xxaxx@my-deja.com <xxaxx@my-deja.com>:

>I am attempting to write a generic directory scan subroutine which will
>run a specified function for each file found.
>
>The recursive directory scan works fine. And I can do stuff to each of
>the files found. But at the moment I have to write a different set of
>subroutines for each type of job I want to accomplish.
>
>In c I would just pass in the point to the function, then after a few
>minutes (or hours) of syntax chasing the routine would use the parameter
>passed function on each occurance of the found files.
>
>How would this be done in perl?

Perl doesn't use pointers, of course, but you can use function references.
You can define your function then pass the function name as a reference or
create a reference variable, or you can create a reference to an anonymous
subroutine.  I like the latter method myself. Examples:

Ex 1:
sub func { print "Hello, World!" };
$funcref = \&func;

Ex 2:
$funcref = sub { print "Hello, World!" };

Your function reference could then be called as &$funcref or even
&{$funcref}.

Take a look at the perlref documentation.  It has a lot of examples.

-- 
Brett W. McCoy                             bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek)   http://www.foiservices.com
FOI Services, Inc./DIOGENES                301-975-0110
---------------------------------------------------------------------------


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

Date: 20 Oct 1999 15:52:19 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: Passing function as parameter.
Message-Id: <380dd723_2@newsread3.dircon.co.uk>

xxaxx@my-deja.com wrote:
> I am attempting to write a generic directory scan subroutine which will
> run a specified function for each file found.
> 

Any reason you arent using File::Find that works just like this and
will carry on going long after yours has run out of memory ;p-}

> The recursive directory scan works fine. And I can do stuff to each of
> the files found. But at the moment I have to write a different set of
> subroutines for each type of job I want to accomplish.
> 
> In c I would just pass in the point to the function, then after a few
> minutes (or hours) of syntax chasing the routine would use the parameter
> passed function on each occurance of the found files.
> 
> How would this be done in perl?
> 

In an abstract sense just like you would in C - except the term is
reference not pointer.

/J\
-- 
"I'm not Carol Vorderman - you wouldn't see me getting drunk in a kebab
shop" - Lily Savage


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

Date: Wed, 20 Oct 1999 13:29:21 GMT
From: Dick Latshaw <latsharj@my-deja.com>
Subject: Re: Perl certification?
Message-Id: <7ukg31$4u7$1@nnrp1.deja.com>

In article <NU7P3.184$kk1.2991@nsw.nnrp.telstra.net>,
  mgjv@comdyn.com.au (Martien Verbruggen) wrote:
> On Tue, 19 Oct 1999 19:13:46 GMT,
>     bing-du@tamu.edu <bing-du@tamu.edu> wrote:
> > Is there any certification exam for Perl?
>
> Answer the following questions:

<snip Comprehensive Perl Examination and Really Nice Certificate>

Uh... You forgot to ask for his credit card number.
--
Regards,
Dick


Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Wed, 20 Oct 1999 14:04:14 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: Perl certification?
Message-Id: <slrn80rj7f.gep.bmccoy@moebius.foiservices.com>

Also Sprach Matthew Amster-Burton <mamster@mamster.net>:

>Hey, I'm a Certified Perl Monkey!  Wait, maybe I said that a little too
>enthusiastically....

I'm a Perl Mongol myself.  Surely there were Mongols who rode camels...

-- 
Brett W. McCoy                             bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek)   http://www.foiservices.com
FOI Services, Inc./DIOGENES                301-975-0110
---------------------------------------------------------------------------


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

Date: Wed, 20 Oct 1999 14:04:27 GMT
From: allnightprod@my-deja.com
Subject: Please help with format
Message-Id: <7uki51$6j5$1@nnrp1.deja.com>

I am trying to format the the sample

#!c:/perl/bin/perl

$fname = "john";
$lnight = "smith";

format TEST2 =
Name: @<<<<<<<<<< Last: @<<<<<<<<<<
$fname, $lname
 .

write(TEST2);
exit;

Why does it not display my data???

PLEASE EMAIL ME smithj@1hostplus.com

thanks



Sent via Deja.com http://www.deja.com/
Before you buy.


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

Date: Wed, 20 Oct 1999 14:08:08 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: Problems adding to and deleting from an array
Message-Id: <slrn80rjep.gep.bmccoy@moebius.foiservices.com>

Also Sprach M B <mb1010@hotmail.com>:

>I am unable to add to an array and delete from an array. I'm
>adding at the end of the array and deleting from the beginning
>of the array (first-in-first-out).

Why aren't you using pop, push, shift and unshift?  You say you've never
heard of it?  perldoc will show you the way!

-- 
Brett W. McCoy                             bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek)   http://www.foiservices.com
FOI Services, Inc./DIOGENES                301-975-0110
---------------------------------------------------------------------------


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

Date: Wed, 20 Oct 1999 14:11:55 GMT
From: bmccoy@foiservices.com (Brett W. McCoy)
Subject: Re: Program to use cgi with out a server? lil help
Message-Id: <slrn80rjls.gep.bmccoy@moebius.foiservices.com>

Also Sprach deltren@my-deja.com <deltren@my-deja.com>:

>IS there a program i could use to make my forms work when both the html
>and cgi are on my computer which isnt a server?

Not without a properly configured web server, nope.  You can get Personal
Web Server for Windows to test stuff out.  If you're using Linux, you can
use Apache.

>If not any advise to test out simple cgi programs?

You can do it on the command line and pass the parameters in that way:

% perlprog "arg1=val1&arg2=val2"

-- 
Brett W. McCoy                             bmccoy@foiservices.com
Computer Operations Manager (Alpha Geek)   http://www.foiservices.com
FOI Services, Inc./DIOGENES                301-975-0110
---------------------------------------------------------------------------


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

Date: Wed, 20 Oct 1999 15:02:16 GMT
From: "Web Research" <webresearch@indy-soft.com>
Subject: Re: REGEXP rewind, backreference?
Message-Id: <YPkP3.7558$PV2.141063@news.rdc1.tn.home.com>

$pagelines =~
s/(\;.*?)(SELECT)(.*?)(\'\s*||\s+)WHERE(.*?sIsolation.*?\;)/$1\' \+
DM1\.TriDef1\.sNoLock \+ \'WHERE$2/sg;

The snippet above (in conjunction with file slurp) looks at Delphi Source
code and makes replacements to erroneous SQL statements contained in it. It
seems to be skipping lines or running amok across multiple lines ignoring
the ; ; start and end characters until it can resolve the regexp. I want it
to 'stick' to distinct lines of code.

Delphi lines of code end in ; just as perl does and can span multiple lines.

The problem seems to be that there are occurances where .*? near the
semicolon seems to take a ; into account with it and not stick to one
complete line of code. Either that, or the first semicolon makes the regexp
skip a line on its next pass. I'm betting on the latter.

I'm not sure if I need to add an operator with the .*? to say any character
except for ; followed by ;, or rewind on the next pass. I'm thinking rewind
and have found some info on 'backreferencing' but am unsure how this
applies.

Rusman




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

Date: 20 Oct 1999 07:04:57 -0700
From: merlyn@stonehenge.com (Randal L. Schwartz)
Subject: Re: SGML/HTML parsing tool
Message-Id: <m1k8oinn5i.fsf@halfdome.holdit.com>

>>>>> "kiosk62279" == kiosk62279  <kiosk62279@my-deja.com> writes:

kiosk62279> 1.  Has anyone else ever found themselves in the situation
kiosk62279> I describe?  (i.e., needing to parse a lot of HTML files
kiosk62279> and extra data from different table cells)

yes.

kiosk62279> 2.  If so, what tools did you use?  What would you recommend?  What
kiosk62279> tool did you find that was easiest to adapt to different purposes
kiosk62279> (e.g., sometimes I only want the data from TD's that are on odd-
kiosk62279> numbered rows, etc.)

The two hardest things about parsing HTML are:

1) deducing the missing close tags (for which you must understand the DTD)
2) handling poor markup (there's a lot of junk on the net that incorrectly
   MIMEs itself as "text/html")

For #1, I'm currently building a tool using Parse::RecDecent that
takes a DTD to generate a recursive descent parser that so far seems
to correctly "close out" the missing end tags and validate the right
attributes.  (The first trivial application will be an HTML
"pretty-printer" for a column I'm writing for WT.)  It has horrible
error recovery though, so I'm thinking about how to modify it to
handle #2.

I can now see why SGML/HTML is a dead-end, and XML/XHTML will rock.
Those optional close-tags are *hard*, and XML has none such.

print "Just another Perl hacker and web-whacker,"

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


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

Date: Wed, 20 Oct 1999 16:33:04 +0200
From: Alex Rhomberg <rhomberg@ife.ee.ethz.ch>
Subject: Re: Unix command ´tree´ in Perl?
Message-Id: <380DD2A0.3D324613@ife.ee.ethz.ch>

Abigail wrote:

> $$ Not exactly. On my Linux boxes, it is a bit different:
> $$
> $$ > which tree
> $$ /usr/bin/tree
> 
> Yeah, and it's even much easier to simulate:
> 
>    $ /usr/bin/tree
>    Segmentation fault
>    $ perl -wle 'print "Segmentation fault"'
>    Segmentation fault
>    $

forrest:~% limit coredumpsize
coredumpsize    unlimited

This way, it gets quite a bit more complicated to simulate

- Alex


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

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
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: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

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 V9 Issue 1129
**************************************


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