[26348] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8523 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Oct 14 14:05:27 2005

Date: Fri, 14 Oct 2005 11:05:04 -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           Fri, 14 Oct 2005     Volume: 10 Number: 8523

Today's topics:
        Hash of array access tricks.  Sorry, long. <keeling@spots.ab.ca>
    Re: Hash of array access tricks.  Sorry, long. xhoster@gmail.com
    Re: Hash of array access tricks.  Sorry, long. <keeling@spots.ab.ca>
    Re: string comparison <rvtol+news@isolution.nl>
    Re: string comparison <jurgenex@hotmail.com>
    Re: string comparison (Anno Siegel)
    Re: string comparison <rvtol+news@isolution.nl>
    Re: string comparison <jurgenex@hotmail.com>
    Re: string comparison <rvtol+news@isolution.nl>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 14 Oct 2005 17:43:07 GMT
From: "s. keeling" <keeling@spots.ab.ca>
Subject: Hash of array access tricks.  Sorry, long.
Message-Id: <slrndkvrhb.bp4.keeling@infidel.spots.ab.ca>

I've built a HoA as so:

my %prompt = (
  FIRST_NAME  => [ qq(NULL), qq(NULL), qq(\tFirst name      -> ), 
                   qq(You MUST supply a first name.\n) ],
  LAST_NAME   => [ qq(NULL), qq(NULL), qq(\tLast name       -> ), 
                   qq(You MUST supply a last name.\n) ],

and so on.  I'd like to write to and access $prompt->{$key}[2] when
prompting a user for the name, and I'd like to stuff the reply in
$prompt{$key}[1], then compare that with $prompt->{$key}[0], which
I'll try to supply via a call to mysql.  :-)

# FIRST_NAME
# 
my $key = qq(FIRST_NAME);
print STDOUT qq($prompt->{$key}[2]);
$prompt->{$key}[1]=<STDIN>;
chomp($prompt->{$key}[1]);
if ( ! $prompt->{$key}[1] ) {
  die $prompt->{$key}[3];
}

What I get is:

Use of uninitialized value in string at ./mupdate.pl line 154 (#1)
    (W uninitialized) An undefined value was used as if it were already
    defined.  It was interpreted as a "" or a 0, but maybe it was a mistake.
    To suppress this warning assign a defined value to your variables.
    
    To help you figure out what was undefined, perl tells you what operation
    you used the undefined value in.  Note, however, that perl optimizes your
    program and the operation displayed in the warning may not necessarily
    appear literally in your program.  For example, "that $foo" is
    usually optimized into "that " . $foo, and the warning will refer to
    the concatenation (.) operator, even though there is no . in your
    program.
    

Use of uninitialized value in die at ./mupdate.pl line 158, <STDIN> line 1 (#1)

Died at ./mupdate.pl line 158, <STDIN> line 1 (#2)
    (F) You passed die() an empty string (the equivalent of die "") or
    you called it with no args and both $@ and $_ were empty.
    
Uncaught exception from user code:
        Died at ./mupdate.pl line 158, <STDIN> line 1.
 at ./mupdate.pl line 158


I think I'm using the same sort of thing, using the same syntax as
Perl Cookbook and Camel say it's to be done.  It sort of works when I
use $prompt{$key}[N] instead.  What am I confusing?


-- 
    Any technology distinguishable from magic is insufficiently advanced.
    (*)    http://www.spots.ab.ca/~keeling           Linux Counter #80292
    - -    Spammers! http://www.spots.ab.ca/~keeling/autospam.html
http://www.ietf.org/rfc/rfc1855.txt democracy human rights Taiwan Independence


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

Date: 14 Oct 2005 17:50:02 GMT
From: xhoster@gmail.com
Subject: Re: Hash of array access tricks.  Sorry, long.
Message-Id: <20051014135002.163$FG@newsreader.com>

"s. keeling" <keeling@spots.ab.ca> wrote:
> I've built a HoA as so:
>
> my %prompt = (
>   FIRST_NAME  => [ qq(NULL), qq(NULL), qq(\tFirst name      -> ),
>                    qq(You MUST supply a first name.\n) ],
>   LAST_NAME   => [ qq(NULL), qq(NULL), qq(\tLast name       -> ),
>                    qq(You MUST supply a last name.\n) ],

%prompt is a lexical hash.

> $prompt->{$key}[1]=<STDIN>;

$prompt is an (undeclared?) scalar which is unrelated to %prompt, other
than that their names look kind of similar.  You are trying to derefence
that scalar as if it held a reference to a hash, which there is no reason
to believe that it does.

> What am I confusing?

The usefulness of "use strict", probably.

Xho

-- 
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service                        $9.95/Month 30GB


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

Date: Fri, 14 Oct 2005 17:51:23 GMT
From: "s. keeling" <keeling@spots.ab.ca>
Subject: Re: Hash of array access tricks.  Sorry, long.
Message-Id: <slrndkvs0r.bp4.keeling@infidel.spots.ab.ca>

s. keeling <keeling@spots.ab.ca>:
>  I've built a HoA as so:
> 
>  my %prompt = (
>    FIRST_NAME  => [ qq(NULL), qq(NULL), qq(\tFirst name      -> ), 
>                     qq(You MUST supply a first name.\n) ],
>    LAST_NAME   => [ qq(NULL), qq(NULL), qq(\tLast name       -> ), 
>                     qq(You MUST supply a last name.\n) ],
> 
>  and so on.  I'd like to write to and access $prompt->{$key}[2] when
>  prompting a user for the name, and I'd like to stuff the reply in
>  $prompt{$key}[1], then compare that with $prompt->{$key}[0], which
>  I'll try to supply via a call to mysql.  :-)

I should also say for some reason it won't work if I don't declare
both $prompt and %prompt, ... or should I just be "my"ing the former
and not the latter?  I'll try that too.


-- 
    Any technology distinguishable from magic is insufficiently advanced.
    (*)    http://www.spots.ab.ca/~keeling           Linux Counter #80292
    - -    Spammers! http://www.spots.ab.ca/~keeling/autospam.html
http://www.ietf.org/rfc/rfc1855.txt democracy human rights Taiwan Independence


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

Date: Fri, 14 Oct 2005 15:21:52 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: string comparison
Message-Id: <dioikb.i4.1@news.isolution.nl>

Dave Weaver schreef:

> If I was the code maintainer, I'd prefer to see:
> 
>     if ( $a eq 'apple' or $a eq 'banana' ) {


This looks too great:

  if ( $a in ('apple', 'banana') ) {

so how about the maintainability of:

  my @fruit = qw(apple banana);

  my $regex = '^('. join('|', @fruit) .')$';

  if ( $a =~ /$regex/ ) {

-- 
Affijn, Ruud

"Gewoon is een tijger."


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

Date: Fri, 14 Oct 2005 14:00:26 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: string comparison
Message-Id: <_RO3f.692$2Y2.425@trnddc05>

Dr.Ruud wrote:
> Dave Weaver schreef:
>
>> If I was the code maintainer, I'd prefer to see:
>>
>>     if ( $a eq 'apple' or $a eq 'banana' ) {
[...]
> so how about the maintainability of:
>
>  my @fruit = qw(apple banana);
>
>  my $regex = '^('. join('|', @fruit) .')$';
>
>  if ( $a =~ /$regex/ ) {

Two problems:
- REs are the wrong tool to do string comparion
- and they will yield wrong results as soon as the search text contains 
RE-special characters

jue




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

Date: 14 Oct 2005 14:40:02 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: string comparison
Message-Id: <diog02$b5k$1@mamenchi.zrz.TU-Berlin.DE>

Dr.Ruud <rvtol+news@isolution.nl> wrote in comp.lang.perl.misc:
> Dave Weaver schreef:
> 
> > If I was the code maintainer, I'd prefer to see:
> > 
> >     if ( $a eq 'apple' or $a eq 'banana' ) {
> 
> 
> This looks too great:
> 
>   if ( $a in ('apple', 'banana') ) {
> 
> so how about the maintainability of:
> 
>   my @fruit = qw(apple banana);
> 
>   my $regex = '^('. join('|', @fruit) .')$';
> 
>   if ( $a =~ /$regex/ ) {

It's fine, though someone might come up with "banana[puertorico]" vs.
"banana[brazil]" and mess up the regex.  I'd also prefer non-capturing
parentheses:

    my $regex = '^(?:' . join( '|', map quotemeta, @fruit) .  ')$';

Anno
-- 
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article.  Click on 
"show options" at the top of the article, then click on the 
"Reply" at the bottom of the article headers.


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

Date: Fri, 14 Oct 2005 16:28:14 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: string comparison
Message-Id: <diomfm.jc.1@news.isolution.nl>

Jürgen Exner:
> Dr.Ruud:

>>  my @fruit = qw(apple banana);
>>
>>  my $regex = '^('. join('|', @fruit) .')$';
>>
>>  if ( $a =~ /$regex/ ) {
>
> Two problems:
> - REs are the wrong tool to do string compari[s]on

What are the big wrongs?


> - and they will yield wrong results as soon as the search text
> contains RE-special characters

In procmail I can use $\var to (almost) unharm this. Of course there
is no need to show a Perl way to do something similar, if the first
problem is that big.

-- 
Affijn, Ruud

"Gewoon is een tijger."



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

Date: Fri, 14 Oct 2005 15:16:12 GMT
From: "Jürgen Exner" <jurgenex@hotmail.com>
Subject: Re: string comparison
Message-Id: <0ZP3f.9$Yk6.2@trnddc01>

Dr.Ruud wrote:
> Jürgen Exner:
>> Two problems:
>> - REs are the wrong tool to do string compari[s]on
>
> What are the big wrongs?

You don't use a gun to swat flies. You use a fly swater.

Perl has a perfectly fine eq operator for comparing strings. There is no 
need to launch the big and expensive RE gun and then jump through hoops to 
"make it work" (e.g. for RE special characters).
Would be interesting to benchmark a simple eq in a loop versus the 
concatenated or'ed REs. My gut feeling is that the loop with eq will be much 
faster

jue 




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

Date: Fri, 14 Oct 2005 18:09:34 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: string comparison
Message-Id: <diosqj.jc.1@news.isolution.nl>

Jürgen Exner schreef:

> Perl has a perfectly fine eq operator for comparing strings. There is
> no need to launch the big and expensive RE gun

OK, but that argument vaporizes if the RE was or will be loaded for
other parts of the program anyway.

> and then jump through
> hoops to "make it work" (e.g. for RE special characters).

No need to jump through hoops:

  if ($a =~ m/\A(apple
                |banana
                |orange
                )
                \z/x) {
  (untested)


> Would be interesting to benchmark a simple eq in a loop versus the
> concatenated or'ed REs. My gut feeling is that the loop with eq will
> be much faster

Compiled simple regexes are also very fast. I'll try benchmark later.

-- 
Affijn, Ruud

"Gewoon is een tijger."



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

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


Administrivia:

#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc.  For subscription or unsubscription requests, send
#the single line:
#
#	subscribe perl-users
#or:
#	unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.  

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

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

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

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


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


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