[17130] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4542 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 6 18:10:31 2000

Date: Fri, 6 Oct 2000 15:10:14 -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: <970870213-v9-i4542@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 6 Oct 2000     Volume: 9 Number: 4542

Today's topics:
        Probably making a stpid error trying to get a variable  <bbartram@installs.com>
    Re: Probably making a stpid error trying to get a varia nobull@mail.com
    Re: Probably making a stpid error trying to get a varia (John J. Trammell)
    Re: Probably making a stpid error trying to get a varia <bbartram@installs.com>
        reading in file as uneven multi-dimensional array <kelley.a.kent@intel.com>
    Re: reading in file as uneven multi-dimensional array (Logan Shaw)
    Re: reading in file as uneven multi-dimensional array (Logan Shaw)
    Re: reading in file as uneven multi-dimensional array <anmcguire@ce.mediaone.net>
    Re: reading in file as uneven multi-dimensional array <kelley.a.kent@intel.com>
    Re: reading in file as uneven multi-dimensional array (Logan Shaw)
    Re: Reading textfiles from client? (Eric Bohlman)
    Re: regex <lr@hpl.hp.com>
    Re: regex <jeffp@crusoe.net>
    Re: Reverse by paragraphs - NOT! <godzilla@stomp.stomp.tokyo>
    Re: Rounding Integers (Logan Shaw)
    Re: Splitting data <lr@hpl.hp.com>
    Re: Unexpected behavior in 5.005_03 <uri@sysarch.com>
    Re: Unexpected behavior in 5.005_03 mrcribbins@my-deja.com
    Re: Unexpected behavior in 5.005_03 <uri@sysarch.com>
    Re: Unexpected behavior in 5.005_03 mrcribbins@my-deja.com
    Re: Unexpected behavior in 5.005_03 <uri@sysarch.com>
    Re: Variable names enclosed within curly braces <lr@hpl.hp.com>
    Re: Variable names enclosed within curly braces <godzilla@stomp.stomp.tokyo>
    Re: Variable names enclosed within curly braces <bmb@ginger.libs.uga.edu>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Fri, 06 Oct 2000 17:36:36 GMT
From: "Bradley J. Bartram" <bbartram@installs.com>
Subject: Probably making a stpid error trying to get a variable to match against an array
Message-Id: <E4oD5.1153$q4.100341@petpeeve.ziplink.net>

Hello everybody -

I pologize for the stupidity of my question but I figure I'll throw it out
and see what comes back.

I need to generate 3 files each day with some reports in them.
Date-stringAM, Date-string-PM and Date-stringLATE.

Everything works except the AM PM LATE function which is defined as:
    AM - 0 - 8
    PM - 9 - 16
    Late - 17 - 23

I've used an if, elsif, else statement set up as:
    if(($hour) =~ m/\w([9|10|11|12|13|14|15|16]) {
        $post="PM";
} elsif (($hour) =~ m/\w([17|18|19|20|21|22|23]) {
        $post="Late";
} else {
        $post="AM";
}

$hour of course is defined as the hour and $post is tagged to the end of the
filename.

The problem I'm seeing is that the results are unpredictable.  Any
suggestions about where my error lies?

If you've made it this far into my post I appreciate it.  Thanks

Brad




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

Date: 06 Oct 2000 20:18:08 +0100
From: nobull@mail.com
Subject: Re: Probably making a stpid error trying to get a variable to match against an array
Message-Id: <u9bswxdbin.fsf@wcl-l.bham.ac.uk>

"Bradley J. Bartram" <bbartram@installs.com> writes:

> I pologize for the stupidity of my question but I figure I'll throw it out
> and see what comes back.

The [ ] in a regex defines a character class.  You do not want to
define a character class so you should not randomly insert [ ] into
your regular expressions.

If you want to use the m// operator to see if a pattern matches the
_whole_ of a string then you should anchor the pattern both ends with
^ and $ respectively.

The \w in regex means... oh, forget it!  

Let's face it you should just go read the whole of perldoc perlre as
you clearly have no idea about regular expressions and simply
enumerating the misconceptions you've shown us so far is not going to
help you.
 
> I need to generate 3 files each day with some reports in them.
> Date-stringAM, Date-string-PM and Date-stringLATE.
> 
> Everything works except the AM PM LATE function which is defined as:
>     AM - 0 - 8
>     PM - 9 - 16
>     Late - 17 - 23

If you want to detect a numeric range then the arithmetic comparison
operators < and > are better tools than regular expressions.

 I've used an if, elsif, else statement set up as:
>     if(($hour) =~ m/\w([9|10|11|12|13|14|15|16]) {
>         $post="PM";
> } elsif (($hour) =~ m/\w([17|18|19|20|21|22|23]) {
>         $post="Late";
> } else {
>         $post="AM";
> }

That's not even syntactically valid.  If you miss-transcribe your code
when asking for help you make it very hard to help you because we
can't tell if we are actually finding your real mistakes or your
transcription errors.
 
> $hour of course is defined as the hour and $post is tagged to the end of the
> filename.

So that was the thinking behind the code above?  Even allowing for
every conceivable misconception I can't figure out what posessed you
to put those \w in there.  Or were they inroduced by transcription
errors?

$post = $hour < 9 ? 'AM' : $hour < 17 ? 'PM' : 'Late'; 

If you don't like the A?B:C syntax you can also do:

if ($hour < 9 ) {
  $post = 'AM';
} elsif ( $hour < 17 ) { 
  $post = 'PM';
} else {
  $post = 'Late'; 
}

> The problem I'm seeing is that the results are unpredictable.

No the results of your code are perfectly predictable.

> Any suggestions about where my error lies?

You are using a tool you don't understand for a purpose for which it
is not suited. 

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: 6 Oct 2000 19:46:05 GMT
From: trammell@nitz.hep.umn.edu (John J. Trammell)
Subject: Re: Probably making a stpid error trying to get a variable to match against an array
Message-Id: <slrn8trgo4.s91.trammell@nitz.hep.umn.edu>

On Fri, 06 Oct 2000 17:36:36 GMT, Bradley J. Bartram
<bbartram@installs.com> wrote:
>Hello everybody -
>
>I pologize for the stupidity of my question but I figure I'll throw it out
>and see what comes back.
>
>I need to generate 3 files each day with some reports in them.
>Date-stringAM, Date-string-PM and Date-stringLATE.
>
>Everything works except the AM PM LATE function which is defined as:
>    AM - 0 - 8
>    PM - 9 - 16
>    Late - 17 - 23

How about:

   if ($hour < 8.5) {
      $post = "AM";
   } elsif ($hour < 16.5) {
      $post = "PM";
   } else {
      $post = "Late";
   }

-- 
John J. Trammell
johntrammell@yahoo.com


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

Date: Fri, 06 Oct 2000 20:02:44 GMT
From: "Bradley J. Bartram" <bbartram@installs.com>
Subject: Re: Probably making a stpid error trying to get a variable to match against an array
Message-Id: <EdqD5.1157$q4.100920@petpeeve.ziplink.net>

Thank you for your input.

As blunt as it may be, you are quite correct.  Instead of keeping something
simple I was trying to make more of it than was obviously necessary.  As
soon as I saw response I did a big duh and promptly slapped myself in the
forehead.

Part of learning something new is making mistakes, some big, some small.
Before I ask again, I'll make sure I'm not blundering too big.

Thanks

Brad


<nobull@mail.com> wrote in message news:u9bswxdbin.fsf@wcl-l.bham.ac.uk...
> "Bradley J. Bartram" <bbartram@installs.com> writes:
>
> > I pologize for the stupidity of my question but I figure I'll throw it
out
> > and see what comes back.
>
> The [ ] in a regex defines a character class.  You do not want to
> define a character class so you should not randomly insert [ ] into
> your regular expressions.
>
> If you want to use the m// operator to see if a pattern matches the
> _whole_ of a string then you should anchor the pattern both ends with
> ^ and $ respectively.
>
> The \w in regex means... oh, forget it!
>
> Let's face it you should just go read the whole of perldoc perlre as
> you clearly have no idea about regular expressions and simply
> enumerating the misconceptions you've shown us so far is not going to
> help you.
>
> > I need to generate 3 files each day with some reports in them.
> > Date-stringAM, Date-string-PM and Date-stringLATE.
> >
> > Everything works except the AM PM LATE function which is defined as:
> >     AM - 0 - 8
> >     PM - 9 - 16
> >     Late - 17 - 23
>
> If you want to detect a numeric range then the arithmetic comparison
> operators < and > are better tools than regular expressions.
>
>  I've used an if, elsif, else statement set up as:
> >     if(($hour) =~ m/\w([9|10|11|12|13|14|15|16]) {
> >         $post="PM";
> > } elsif (($hour) =~ m/\w([17|18|19|20|21|22|23]) {
> >         $post="Late";
> > } else {
> >         $post="AM";
> > }
>
> That's not even syntactically valid.  If you miss-transcribe your code
> when asking for help you make it very hard to help you because we
> can't tell if we are actually finding your real mistakes or your
> transcription errors.
>
> > $hour of course is defined as the hour and $post is tagged to the end of
the
> > filename.
>
> So that was the thinking behind the code above?  Even allowing for
> every conceivable misconception I can't figure out what posessed you
> to put those \w in there.  Or were they inroduced by transcription
> errors?
>
> $post = $hour < 9 ? 'AM' : $hour < 17 ? 'PM' : 'Late';
>
> If you don't like the A?B:C syntax you can also do:
>
> if ($hour < 9 ) {
>   $post = 'AM';
> } elsif ( $hour < 17 )

>   $post = 'PM';
> } else {
>   $post = 'Late';
> }
>
> > The problem I'm seeing is that the results are unpredictable.
>
> No the results of your code are perfectly predictable.
>
> > Any suggestions about where my error lies?
>
> You are using a tool you don't understand for a purpose for which it
> is not suited.
>
> --
>      \\   ( )
>   .  _\\__[oo
>  .__/  \\ /\@
>  .  l___\\
>   # ll  l\\
>  ###LL  LL\\




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

Date: Fri, 6 Oct 2000 11:34:57 -0700
From: "Kelley Kent" <kelley.a.kent@intel.com>
Subject: reading in file as uneven multi-dimensional array
Message-Id: <8rl5ug$6td@news.or.intel.com>

I have this text file that is not exactly formatted, for example:

       my mother said
       to pick
       the very best one
       and
       you are it

And what I'd like to do is to read in the file as a multidimensional
array, where 1st dimension is the line of the file, and the 2nd is
each individual word of the line, i.e

       $file[0][0] = my
       $file[0][1] = mother
       $file[0][2] = said
       $file[1][0] = to
       $file[1][1] = pick
       $file[2][0] = the
       ...

What I have right now, that works, is

       $row = -1;
      @lines = <FILE>;
       foreach $line (@lines) {
           $row++; $col = 0;
           chop($line);
           @words = split /\s+/, $line;
           foreach $word (@words) {
                $file[$row][$col++] = $word;
           }
       }

But I thought there might be a way to use the array
@words somehow with a push or join on the @file
array and avoid that second foreach loop and not
have to mess with the counters $row and $col.

Just wondering.

Thanx,

Kelley





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

Date: 6 Oct 2000 14:02:18 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: reading in file as uneven multi-dimensional array
Message-Id: <8rl7jq$bcu$1@provolone.cs.utexas.edu>

In article <8rl5ug$6td@news.or.intel.com>,
Kelley Kent <kelley.a.kent@intel.com> wrote:
>I have this text file that is not exactly formatted, for example:
>
>       my mother said
>       to pick
>       the very best one
>       and
>       you are it
>
>And what I'd like to do is to read in the file as a multidimensional
>array, where 1st dimension is the line of the file, and the 2nd is
>each individual word of the line, i.e
>
>       $file[0][0] = my
>       $file[0][1] = mother
>       $file[0][2] = said
>       $file[1][0] = to
>       $file[1][1] = pick
>       $file[2][0] = the
>       ...
>
>What I have right now, that works, is
>
>       $row = -1;
>      @lines = <FILE>;
>       foreach $line (@lines) {
>           $row++; $col = 0;
>           chop($line);
>           @words = split /\s+/, $line;
>           foreach $word (@words) {
>                $file[$row][$col++] = $word;
>           }
>       }

Yes indeed, there is a simpler way:

	while (<>)
		{
		chomp;
		push (@file, [ split ]);
		}

This works because two-dimensional arrays don't exist in Perl; instead,
you just have arrays whose values are references to other arrays.
The syntax

	$file[0][0] = "my";

is really just shorthand for this:

	$file[0]->[0] = "my";

Hope that helps.

  - Logan


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

Date: 6 Oct 2000 14:06:38 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: reading in file as uneven multi-dimensional array
Message-Id: <8rl7ru$be0$1@provolone.cs.utexas.edu>

In article <8rl7jq$bcu$1@provolone.cs.utexas.edu>,
Logan Shaw <logan@cs.utexas.edu> wrote:
>Yes indeed, there is a simpler way:
>
>	while (<>)
>		{
>		chomp;
>		push (@file, [ split ]);
>		}

Oh, and I forgot to mention an even simpler (or at least shorter) way:

	@file = map ([ split ], <>);

Note, however, that this only works because I used split() in a way
that makes it not matter that I didn't chomp the newline.

  - Logan


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

Date: Fri, 6 Oct 2000 15:01:13 -0500
From: "Andrew N. McGuire " <anmcguire@ce.mediaone.net>
Subject: Re: reading in file as uneven multi-dimensional array
Message-Id: <Pine.LNX.4.21.0010061500300.12146-100000@hawk.ce.mediaone.net>

On Fri, 6 Oct 2000, Kelley Kent quoth:

KK> I have this text file that is not exactly formatted, for example:
KK> 
KK>        my mother said
KK>        to pick
KK>        the very best one
KK>        and
KK>        you are it
KK> 
KK> And what I'd like to do is to read in the file as a multidimensional
KK> array, where 1st dimension is the line of the file, and the 2nd is
KK> each individual word of the line, i.e
KK> 
KK>        $file[0][0] = my

[ snip ]

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

my @file;
push @file, [ split ] while <DATA>;
print map @$_ => @file;

__DATA__
my mother said
to pick
the very best one
and
you are it

anm
-- 
perl -wMstrict -e '
$a=[[qw[J u s t]],[qw[A n o t h e r]],[qw[P e r l]],[qw[H a c k e r]]];$.++
;$@=$#$a;$$=[reverse sort map$#$_=>@$a]->[$|];for$](--$...$$){for$}($|..$@)
{$$[$]][$}]=$a->[$}][$]]}}$,=$";$\=$/;print map defined()?$_:$,,@$_ for @$;
'



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

Date: Fri, 6 Oct 2000 13:55:27 -0700
From: "Kelley Kent" <kelley.a.kent@intel.com>
Subject: Re: reading in file as uneven multi-dimensional array
Message-Id: <8rle5v$cp2@news.or.intel.com>

> Oh, and I forgot to mention an even simpler (or at least shorter) way:
>
> @file = map ([ split ], <>);
>

Wow, it's one line AND works. :-)

I think I understand most of it, except the brackets. Am I correct in
assuming that it basically creates an anonymous array from the results
of the split command? Or is something else going on behind the scenes?

Thanks again for the assistance,

--Kelley




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

Date: 6 Oct 2000 16:48:06 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: reading in file as uneven multi-dimensional array
Message-Id: <8rlham$c3d$1@provolone.cs.utexas.edu>

In article <8rle5v$cp2@news.or.intel.com>,
Kelley Kent <kelley.a.kent@intel.com> wrote:
>> Oh, and I forgot to mention an even simpler (or at least shorter) way:
>>
>> @file = map ([ split ], <>);
>
>I think I understand most of it, except the brackets. Am I correct in
>assuming that it basically creates an anonymous array from the results
>of the split command?

Yes.  If an array or list value occurs inside square brackets, an
anonymous array whose value is the list/array is created.

Thus, this

	$x = \@myarray;

makes $x a reference to @myarray, but this

	$x = [ @myarray ];

makes $x a reference to a copy of @myarray.

Hope that helps.

  - Logan


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

Date: 6 Oct 2000 21:39:16 GMT
From: ebohlman@omsdev.com (Eric Bohlman)
Subject: Re: Reading textfiles from client?
Message-Id: <8rlgq4$2kr8$1@news.enteract.com>

Bas Welvering <kpn_bw@hetnet.nl> wrote:
> So, actually, you are saying that Perl can't open a file stored on the
> clients computer, store all lines of the file in an array, and then process
> it?

If "client" here means "HTTP client" (as it seems to from the context),
then the answer is that no process running on an HTTP server, whether
written in Perl or some other language, can access the client machine's
filesystem as a filesystem.  It's certainly possible for a user agent
(such as a browser) on the client machine to let its user select a file or
files on the client machine and transmit their contents to the server
process, which can deal with those contents; this is called
"uploading" whether or not the server process actually stores those
contents in a file on the server's filesystem.  But the server process
simply cannot treat the client's filesystem as if it were mounted; HTTP
simply doesn't provide any means to do that.



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

Date: Fri, 6 Oct 2000 11:58:18 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: regex
Message-Id: <MPG.1447c0a5821d902198ae17@nntp.hpl.hp.com>

In article <Pine.GSO.4.21.0010060749300.14163-100000@crusoe.crusoe.net> 
on Fri, 6 Oct 2000 07:51:23 -0400, Jeff Pinyan <jeffp@crusoe.net> 
says...
> On Oct 6, Somniculosus said:
> 
> >How would you compact the following CODE into a one line regex?
> 
> >$line =~ s|{(.*?)}|<a href="$1">$1</a>|g;
> >($tmp =$1) =~ s/ /+/g;
> >$line =~ s|<a href="(.*?)"|<a href="http://smthg.com/$tmp"|g;
> 
>   $line =~ s!\{([^}]*)\}!
               ^        ^
A minor note:  You gratuitously escaped characters that have no 
metasemantics in this context.  See the observation I just posted in 
another thread about extra syntax implying extra semantics.

>     my ($tmp = $1) =~ tr/ /+/;  # should URL-encode here maybe?

A major note:  This is invalid syntax, as you cannot 'my' an assignment 
expression.  How did this slip by your testing?  :-)

      (my $tmp = $1) =~ tr/ /+/;  # should URL-encode here maybe?

>     qq(<a href="http://smthg.com/$tmp">$1</a>)
>   !ge;

 ...

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


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

Date: Fri, 6 Oct 2000 17:42:19 -0400
From: Jeff Pinyan <jeffp@crusoe.net>
Subject: Re: regex
Message-Id: <Pine.GSO.4.21.0010061734210.14163-100000@crusoe.crusoe.net>

On Oct 6, Larry Rosler said:

>>   $line =~ s!\{([^}]*)\}!
>               ^        ^
>A minor note:  You gratuitously escaped characters that have no 
>metasemantics in this context.  See the observation I just posted in 
>another thread about extra syntax implying extra semantics.

I felt safer putting them there, not because I'm stupid or ignorant, but
because it saves trouble when putting that in a larger regex.  (And I know
you didn't call me stupid or ignorant, but I feel if I said "I feel
safer", I'd get some "there's no reason not to feel safe" comment from
someone.)

>>     my ($tmp = $1) =~ tr/ /+/;  # should URL-encode here maybe?
>      (my $tmp = $1) =~ tr/ /+/;  # should URL-encode here maybe?

My apologies for not pasting what I tested.  I re-wrote the code instead.

-- 
Jeff "japhy" Pinyan     japhy@pobox.com     http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/
CPAN - #1 Perl Resource  (my id:  PINYAN)        http://search.cpan.org/





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

Date: Fri, 06 Oct 2000 11:06:24 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Reverse by paragraphs - NOT!
Message-Id: <39DE14A0.CAC13258@stomp.stomp.tokyo>

Randal L. Schwartz wrote:
 
> > Godzilla! wrote:
 
> Godzilla!> Randall L. Schwartz wrote:
 
> That's not me (check your spelling).
 
> > Ollie continues to make fools of the foolish and himself:
 
> That's not something I said. Be careful how you quote the quoting. :)


These are profound truisms. May the Schwartz be with you young man.


All of this reminds me of two uncles and two cows. Within this
newsgroup, I read these boys constantly engaging in weenie wagging
contests. "Mine's bigger than yours." "Is not." "Is too." "Is not." ...

On our rural Oklahoma farm, like all rural farms scattered
across this mudball we call Earth, kids don't play. We work.
However, we do play. Sometimes we silently sneak off and have
shit bug races, play tag with rattlesnakes and cottonmouths,
place lightning bugs in our mouths and grin, throw dirt clods
at each other and have bean nose sucking contests, which is
a comical tale in itself.

These two teenage uncles, Weldon and Hershel, enter into a 
weenie wagging contest, after consumption of mason jar 
white lightning.

"My cow done be stronger than yer cow." "Ain't so." "Sho' nuff is." ...

My uncles, with some of us kids tagging along, head out to our
pasture patty cow pasture to decide who wins this weenie wagging
contest inspired by brazen drunkeness on a Sunday afternoon. They
tie the tails of these two cows together and begin smacking them
on their flanks. "Haw! Gitty up little dogie!"

A winner is unclear. One cow pulls the other cow's tail out
by the roots and, later in the day, Grandpa takes a stout switch
to both of my uncle's tails, with vigor for a long period of time.
Grandpa then did the same for each of us kids, just for emphasis.

Today, I am sure there are no winners in a weenie wagging contest.


Godzilla!
-- 
No Tale Motel
  http://la.znet.com/~callgirl/android/notell.cgi


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

Date: 6 Oct 2000 13:06:16 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: Rounding Integers
Message-Id: <8rl4ao$apk$1@provolone.cs.utexas.edu>

In article <8rl1rq$ipt$1@nnrp1.deja.com>,  <vivi16@my-deja.com> wrote:
>Can you use sprintf(); to round integers?  I want to round something
>like 4568 to 4600.  I suppose if I knew a number was always going to be
>in the thousands, I could divide by 100, use the CEIL function and then
>multiply by 100.  But is there an easier way or built in command?

You've pretty much summed up the situation.  Do "perldoc -q round" for
more info.

  - Logan


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

Date: Fri, 6 Oct 2000 13:22:41 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Splitting data
Message-Id: <MPG.1447d470823594cc98ae18@nntp.hpl.hp.com>

In article <39DDC9DB.D7C117B3@sinus-medien.de> on Fri, 06 Oct 2000 
14:47:23 +0200, Christian =?iso-8859-1?Q?R=FCggeberg?= <news@sinus-
medien.de> says...
> Hi,
> 
> you also can use this:
> @newlist = split(/|/,$line)

No you can't.  Though several other posters have already corrected this 
very common error implicitly, maybe it's necessary to be explicit.

The regex to split on '|' is /\|/, because '|' is a regex metacharacter.

  perldoc perlre

<SNIP complete copy of previous post, out of logical sequence>

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


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

Date: Fri, 06 Oct 2000 19:24:29 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Unexpected behavior in 5.005_03
Message-Id: <x71yxt4vte.fsf@home.sysarch.com>

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


  m> I'm sure it comes back to something stupid I did, but I thought
  m> I'd go ahead and post here and see what you think. I needed
  m> to do a replacement of a string in about 100 files. I had a
  m> list of filenames and line numbers where the string was found
  m> in the file. I fed that to the following script:

  m> #!/pkgs/local/bin/perl -w

  m> use strict;

  m> $^I = ".bak";

  m> while (<>) {    # filelist.txt passed as argument

  m>     /^(\[\.)([\/\.|A-Z|a-z|_|-|\s\d]+)(\s\-\sline\s)(\d+)(\].*)$/;

yechh! major problems there. you escape way too much, you grab where you
don't need to grab. you have | in char classes which is wrong.
i can't verify this as i have no data but this should do the same:


	my( $file, $line ) = /^\[\.([-\w\s/.]+)\s-\sline\s(\d+)\].*$/ ;


  m>     open(TBR, "/mweb/docs/IT/$2") or warn $!;

  m>     # Opened read-only unless $^I is defined, right?

  m>     while (<TBR>) {

  m>         select TBR;

you can't select TBR as it is opened for reading. you have to open a
file for writing and then just print to it. the $^I stuff only works for
filenames passed in @ARGV. you can fake it out by localizing @ARGV and
assigning the filename to it and looping over <>. i am not sure how well
it will work with your outer loop of <> getting the files and line. i
would explicity read that file and let perl do the read and overwrite
stuff for you.


  m>         if ($. == $4) {
  m>             s/it_corp_web\@/help_desk\@/;

if that replacement is nowhere else in the files except where you have
the line numbers, why not just do a global replace in a one liner?

	perl -p -i.bak -e 's/it_corp_web\@/help_desk\@/' `cat filelist`

just make sure not to have the lines numbers in the file list now.

  m> I ended up with a bunch of 0 length files, and no backup copies.
  m> (Fortunately, I do have backup tapes...) Is this a bug, or am I
  m> an idiot? Or both?

almost assuredly the middle choice.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: Fri, 06 Oct 2000 20:24:47 GMT
From: mrcribbins@my-deja.com
Subject: Re: Unexpected behavior in 5.005_03
Message-Id: <8rlcea$qbe$1@nnrp1.deja.com>

In article <x71yxt4vte.fsf@home.sysarch.com>,
  Uri Guttman <uri@sysarch.com> wrote:
> m>/^(\[\.)([\/\.|A-Z|a-z|_|-|\s\d]+)(\s\-\sline\s)(\d+)(\].*)$/;
>
> yechh! major problems there. you escape way too much, you grab
> where you
> don't need to grab. you have | in char classes which is wrong.

Fair enough. Thanks for the insight. I suppose I ought to read
that regex book I paid 29.95 for. But that wasn't what I was
asking about.

>   m>         select TBR;
>
> you can't select TBR as it is opened for reading. you have to open a

I guess that was my point. Why did it *write* to a file that
was opened read-only?

>   m> (Fortunately, I do have backup tapes...) Is this a bug, or am I
>   m> an idiot? Or both?
>
> almost assuredly the middle choice.

Ouch.

Michael


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


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

Date: Fri, 06 Oct 2000 21:03:21 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Unexpected behavior in 5.005_03
Message-Id: <x7wvfl3co6.fsf@home.sysarch.com>

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

  m> Fair enough. Thanks for the insight. I suppose I ought to read
  m> that regex book I paid 29.95 for. But that wasn't what I was
  m> asking about.

i hope that is MRE. if so, read it. you will learn a lot.

  m> select TBR;
  >> 
  >> you can't select TBR as it is opened for reading. you have to open a

  m> I guess that was my point. Why did it *write* to a file that
  m> was opened read-only?

not sure. possibly something to do with your setting of $^I but as i
said, what you did is not the normal way of using it. -i is meant to be
used with -p or -n loops and not inside code. in fact the docs in
perlvar only mention shutting it off with undef. but you should just do
a global replace one liner like i showed unless you must only substitute
on the line numbers you have.

  >> almost assuredly the middle choice.

  m> Ouch.

i forgot to put a :) here. but that was the only reasonable answer given
the choices.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: Fri, 06 Oct 2000 21:41:40 GMT
From: mrcribbins@my-deja.com
Subject: Re: Unexpected behavior in 5.005_03
Message-Id: <8rlguj$u67$1@nnrp1.deja.com>

In article <x7wvfl3co6.fsf@home.sysarch.com>,
  Uri Guttman <uri@sysarch.com> wrote:
> >>>>> "m" == mrcribbins  <mrcribbins@my-deja.com> writes:
>
>   m> Fair enough. Thanks for the insight. I suppose I ought to read
>   m> that regex book I paid 29.95 for. But that wasn't what I was
>   m> asking about.
>
> i hope that is MRE. if so, read it. you will learn a lot.

Yeah, it is -- and I will definitely be reading it.

> but you should just do a global replace one liner like

I did. For some reason, I thought I was saving Perl some time
by giving it the line number. I do appreciate the correction.

>   >> almost assuredly the middle choice.
>
>   m> Ouch.
>
> i forgot to put a :) here. but that was the only reasonable answer
given
> the choices.

:)

Michael


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


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

Date: Fri, 06 Oct 2000 22:03:01 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: Unexpected behavior in 5.005_03
Message-Id: <x7u2ap39wq.fsf@home.sysarch.com>

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


  >> but you should just do a global replace one liner like

  m> I did. For some reason, I thought I was saving Perl some time
  m> by giving it the line number. I do appreciate the correction.

but you caused yourself more work. remember one of the three tenets of
perl is laziness. but that is on the part of the programmer. so let perl
slave over your text file failing to match on most lines. that is what
it is meant to do.

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


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

Date: Fri, 6 Oct 2000 11:48:27 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Variable names enclosed within curly braces
Message-Id: <MPG.1447be54fc1ff1fd98ae16@nntp.hpl.hp.com>

In article <8rkt9p$f0p$1@nnrp1.deja.com> on Fri, 06 Oct 2000 16:06:24 
GMT, evanharrington@my-deja.com <evanharrington@my-deja.com> says...
> In article <MPG.1446a4f16fc0c3b98ae12@nntp.hpl.hp.com>,
>   Larry Rosler <lr@hpl.hp.com> wrote:
> > In article <8riubs$td3$1@nnrp1.deja.com> on Thu, 05 Oct 2000 22:12:17
> > GMT, evanharrington@my-deja.com <evanharrington@my-deja.com> says...
> >
> > "A foolish consistency is the hobgoblin of little minds, adored by
> > little statesman and philosophers and divines." -- Ralph Waldo Emerson
> >
> > This is truly a foolish consistency, though harmelss in this case.
> > As you note, it will cause Perl programmers to look at you askance.
> 
> Yes, so I have seen.  All I thought I was doing was making it clear in
> every case which variable was meant.  After all, human coders are not
> so good at seeing the few exceptions that cause all the problems.  The
> braces *seemed* to make it crystal-clear.  I was bitten a few times by
> variable name mistakes and thought that curlies were the way to avoid
> the problem in the future.  However, I do understand the point you and
> others have made and I, myself, prefer not to resort to such clutter.

To generalize a bit:  I speculate that extra syntax implies to the 
reader extra semantics.  In this case -- ${x} = ${y}; -- there are no 
extra semantics, just (as you say) clutter, which is disorienting.

I think the same generalization applies to overparenthesizing of 
arithmetic expressions, for example.  But many authorities believe that 
it leads to clarity.

    $x = $y + ($z * $w);

Having been inculcated into the rules of arithmetic precedence since 
elementary algebra, I try in vain to divine the significance of the 
parentheses, and conclude that the programmer is less than adept.  But 
if the operator were, say, '<<'  or '&', I would welcome the use of 
parentheses even where not needed, because the precedence rules aren't 
ingrained.

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


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

Date: Fri, 06 Oct 2000 12:14:47 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Variable names enclosed within curly braces
Message-Id: <39DE24A7.A820358C@stomp.stomp.tokyo>

Larry Rosler wrote:

> evanharrington wrote:
> >  Larry Rosler wrote:
> > >  evanharrington wrote:
> > > > Godzilla! didn't write:


(snippage with Kato Kaylin's hairbrush, a weed whacker)

> I think the same generalization applies to overparenthesizing of
> arithmetic expressions, for example.  But many authorities believe that
> it leads to clarity.
 
>     $x = $y + ($z * $w);
 
> Having been inculcated into the rules of arithmetic precedence since
> elementary algebra, I try in vain to divine the significance of the
> parentheses, and conclude that the programmer is less than adept.  But
> if the operator were, say, '<<'  or '&', I would welcome the use of
> parentheses even where not needed, because the precedence rules aren't
> ingrained.


An assumption is often made of a reader's base knowledge
of precedence in mathematical operations. Many are aware
of these precedence orders, many are not having never
learned or having forgotten since highschool.

  1 + (1 x 1) = 2
  1 + 1 x 1 = 2

All works well until you venture beyond values
of one, or zero if you like playing with infinity.

As an educator, I have found it is safer to assume
no base knowledge of precedence in mathematical
operations and, to be sure those parentheses are 
always present to lend clarity, with high hopes.

1 + (2 x 4) = 9
1 + 2 x 4 = 12


Godzilla!
-- 
Kira, Professional Poker Player
  http://la.znet.com/~callgirl/android/poker.cgi


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

Date: Fri, 6 Oct 2000 15:21:18 -0400
From: Brad Baxter <bmb@ginger.libs.uga.edu>
Subject: Re: Variable names enclosed within curly braces
Message-Id: <Pine.A41.4.21.0010061514040.12350-100000@ginger.libs.uga.edu>

On Fri, 6 Oct 2000 evanharrington@my-deja.com wrote:
> 
> Yes, so I have seen.  All I thought I was doing was making it clear in
> every case which variable was meant.  After all, human coders are not
> so good at seeing the few exceptions that cause all the problems.  The
> braces *seemed* to make it crystal-clear.  I was bitten a few times by
> variable name mistakes and thought that curlies were the way to avoid
> the problem in the future.  However, I do understand the point you and
> others have made and I, myself, prefer not to resort to such clutter.
> 
> Your input is helpful.
> 
> Thank you.
> Evan K. Harrington

Those bites are minimized by use strict; and -w or use warnings; and these
are the correct way to avoid the problem (as some of the previous posts
said).  Your note above doesn't mention them, so I wonder if you caught
the point. If your current scripts do not use strict or -w, you may find
adding them quite painful.  Even so, IMO it's a useful and educational
exercise.

Brad



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

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


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