[16651] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4063 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Fri Aug 18 21:10:33 2000

Date: Fri, 18 Aug 2000 18:10:20 -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: <966647419-v9-i4063@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Fri, 18 Aug 2000     Volume: 9 Number: 4063

Today's topics:
    Re: obfusicate routine to count max string len in hash <mauldin@netstorm.net>
    Re: obfusicate routine to count max string len in hash <uri@sysarch.com>
    Re: obfusicate routine to count max string len in hash <mauldin@netstorm.net>
    Re: obfusicate routine to count max string len in hash <lr@hpl.hp.com>
    Re: obfusicate routine to count max string len in hash (Mark-Jason Dominus)
    Re: obfusicate routine to count max string len in hash (Logan Shaw)
    Re: Perl guestbook hacking (with HTML enabled) (Gwyn Judd)
    Re: perl's -T switch. (Robert Hallgren)
    Re: Problem with hash initialization using list <lr@hpl.hp.com>
    Re: Recursion Help (Mark-Jason Dominus)
    Re: Regex Alternation Question <rick.delaney@home.com>
    Re: Simple regex problem fgont@my-deja.com
    Re: Simple regex problem <lr@hpl.hp.com>
    Re: Simple regex problem <care227@attglobal.net>
    Re: substring-p <mauldin@netstorm.net>
    Re: substring-p <mauldin@netstorm.net>
    Re: Use of uninitialized value, but it's not uninitiali <grichard@uci.edu>
    Re: Use of uninitialized value, but it's not uninitiali <abe@ztreet.demon.nl>
    Re: Use of uninitialized value, but it's not uninitiali <lr@hpl.hp.com>
    Re: Use of uninitialized value, but it's not uninitiali <lr@hpl.hp.com>
    Re: Use of uninitialized value, but it's not uninitiali <lr@hpl.hp.com>
    Re: We Need Your Feedback <gellyfish@gellyfish.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

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

Date: Fri, 18 Aug 2000 22:12:46 GMT
From: Jim Mauldin <mauldin@netstorm.net>
Subject: Re: obfusicate routine to count max string len in hash
Message-Id: <399DB42B.EA888B87@netstorm.net>

Larry Rosler wrote:
> 
> In article <399DA06E.27DD50D7@netstorm.net> on Fri, 18 Aug 2000 20:48:31
> GMT, Jim Mauldin <mauldin@netstorm.net> says...
> > rog wrote:
> > >
> > > Hi, bit silly really but I am trying to come up with the shortest bit
> > > of code to return the length of the longest string in a hash.
> 
> ...
> 
> > print length [sort keys %hash]->[-1];
> 
> Shorter and more efficient (no need to create a temporary array from the
> list):
> 
>   print length +(sort keys %hash)[-1];

Very nice as usual, Larry, but help me out here.  I believed the
+(whatever) construct to be essentially a precedence manager.  In your
example, it's clear that [-1] indicates that we're referring to a list
element.  So what is the extra cost of [list]->[-1]?

> But of course this is an O(N log N) solution; an O(N) solution would
> require more strokes.
> 
>   my $x = 0;
>   $x > length or $x = length for keys %hash;
>   print $x;
> 

Avoids the cost of quicksort.  Nice example of how a low golf score
isn't always the best answer.

Thanks,

-- Jim


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

Date: Fri, 18 Aug 2000 22:46:39 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: obfusicate routine to count max string len in hash
Message-Id: <x7ya1uur03.fsf@home.sysarch.com>

>>>>> "JM" == Jim Mauldin <mauldin@netstorm.net> writes:

  >> print length +(sort keys %hash)[-1];

  JM> Very nice as usual, Larry, but help me out here.  I believed the
  JM> +(whatever) construct to be essentially a precedence manager.  In your
  JM> example, it's clear that [-1] indicates that we're referring to a list
  JM> element.  So what is the extra cost of [list]->[-1]?

allocating the anon array and then dereferencing it and freeing it. his
way only has list on the stack.

  >> my $x = 0;
  >> $x > length or $x = length for keys %hash;
  >> print $x;

  JM> Avoids the cost of quicksort.  Nice example of how a low golf score
  JM> isn't always the best answer.

no one has ever claimed shorter perl golf code is fast code. in fact we
(the PGA and others) point out that execution speed is never judged in
perl golf. determining execution speed of perl code by looking at it is
a black art mastered by no one.

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, 18 Aug 2000 22:51:07 GMT
From: Jim Mauldin <mauldin@netstorm.net>
Subject: Re: obfusicate routine to count max string len in hash
Message-Id: <399DBD2E.AB3F92CE@netstorm.net>

Uri Guttman wrote:
> 
> his way only has list on the stack.
			^^^^^^^^^^^^

Aha.  Uri to the rescue, again.  Thanks.

-- Jim


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

Date: Fri, 18 Aug 2000 15:50:34 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: obfusicate routine to count max string len in hash
Message-Id: <MPG.14075d9118a393ab98ac9b@nntp.hpl.hp.com>

In article <399DB42B.EA888B87@netstorm.net> on Fri, 18 Aug 2000 22:12:46 
GMT, Jim Mauldin <mauldin@netstorm.net> says...
> Larry Rosler wrote:

 ...

> > Shorter and more efficient (no need to create a temporary array from the
> > list):
> > 
> >   print length +(sort keys %hash)[-1];
> 
> Very nice as usual, Larry, but help me out here.

But wrong; see below.

>                                                   I believed the
> +(whatever) construct to be essentially a precedence manager.

In this case, it disambiguates the left paren from being the beginning 
of a parenthesized arg list for print().

>                                                               In your
> example, it's clear that [-1] indicates that we're referring to a list
> element.  So what is the extra cost of [list]->[-1]?

It is the cost of creating an anonymous array and copying the contents 
of the list into the array before indexing into it, rather than simply 
indexing into the list directly.  (And it is one stroke longer.  :-)

> > But of course this is an O(N log N) solution; an O(N) solution would
> > require more strokes.
> > 
> >   my $x = 0;
> >   $x > length or $x = length for keys %hash;
> >   print $x;
> > 
> 
> Avoids the cost of quicksort.  Nice example of how a low golf score
> isn't always the best answer.

The correct answer (as observed later):

      $x=(sort{$b<=>$a}map length,keys%hash)[0];

The faster answer (golfed also):

      my$x=0;$x>length or$x=length for keys%hash;

Not that far away (one stroke).

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


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

Date: Fri, 18 Aug 2000 23:13:15 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: obfusicate routine to count max string len in hash
Message-Id: <399dc30b.1140$2ed@news.op.net>

In article <399DA06E.27DD50D7@netstorm.net>,
Jim Mauldin  <mauldin@netstorm.net> wrote:
>> Hi, bit silly really but I am trying to come up with the shortest bit
>> of code to return the length of the longest string in a hash.
>
>Is this shorter?
>
>print length [sort keys %hash]->[-1];

But that is not what was asked for.  That is the length of the
alphabetically last key.  It is not the length of the longest key.



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

Date: 18 Aug 2000 19:34:08 -0500
From: logan@cs.utexas.edu (Logan Shaw)
Subject: Re: obfusicate routine to count max string len in hash
Message-Id: <8nkkm0$k87$1@provolone.cs.utexas.edu>

In article <399DAAAE.35638860@cc.gatech.edu>,
Stephen Kloder  <stephenk@cc.gatech.edu> wrote:
>rog wrote:
>
>> Hi, bit silly really but I am trying to come up with the shortest bit
>> of code to return the length of the longest string in a hash.
>>
>> I am interested in seeing if it can be done shorter, and as you can see
>> solution 1 is the best ive come up with. Any ideas anyone?
>> cheers
>>
>
>Shortest I can think of that doen't use extra variables is:
>$mlen=(sort{$b<=>$a}map{length}keys%hash)[0];
 $mlen=(sort{$b<=>$a}map{length}keys%hash)[0];

<ARNOLD_DINGFELDER_HORSHAK>
Oh! Oh! Oh!
</ARNOLD_DINGFELDER_HORSHAK>

How about this:

	$mlen=(sort map(s/.//g,keys%hash))[-1];

Or (shorter variable names):

	$l=(sort map(s/.//g,keys%h))[-1];

  - Logan


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

Date: Sat, 19 Aug 2000 00:11:15 GMT
From: tjla@guvfybir.qlaqaf.bet (Gwyn Judd)
Subject: Re: Perl guestbook hacking (with HTML enabled)
Message-Id: <slrn8prk51.dr1.tjla@thislove.dyndns.org>

I was shocked! How could Erik van Roode <newsposter@cthulhu.demon.nl>
say such a terrible thing:

>  Wouldn't it be easier to post a message containing some evil JavaScript
>fragment?

Isn't it bad enough that anyone (including non-malicious users) can
delete all your data? Besides, this is a Perl newsgroup not a javascript
newsgroup.

-- 
Gwyn Judd (print `echo 'tjla@guvfybir.qlaqaf.bet' | rot13`)
I don't wish to appear overly inquisitive, but are you still alive?


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

Date: Fri, 18 Aug 2000 22:06:49 GMT
From: sandhall@swipnet.se (Robert Hallgren)
Subject: Re: perl's -T switch.
Message-Id: <slrn8prd08.uu.sandhall@poetry.lipogram>

On 18 Aug 2000 16:18:24 -0400,
 Jordan Katz <katz@underlevel.net> wrote:

[...]
> Which works fine, but if I add Perl's -T switch, which adds extra
> checking as I understand from perlrun, get_url fails everytime. I
> was hoping someone could clarify why.

Failing in what way? No error message?

Look in 'perldoc perlsec' to find out what the -T switch does and
how to use it in your scripts.


Robert
-- 
Robert Hallgren <sandhall@swipnet.se>

PGP: http://www.lipogram.com/pgpkey.asc
5F1E 95C2 F0D8 25A3 D1BE 0F16 D426 34BD 166A 566C


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

Date: Fri, 18 Aug 2000 14:55:52 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Problem with hash initialization using list
Message-Id: <MPG.140750c17180156698ac98@nntp.hpl.hp.com>

In article <u9hf8iqnvl.fsf@wcl-l.bham.ac.uk> on 18 Aug 2000 22:07:42 
+0100, nobull@mail.com <nobull@mail.com> says...
> Larry Rosler <lr@hpl.hp.com> writes:
> 
> > In article <u9lmxun655.fsf@wcl-l.bham.ac.uk> on 18 Aug 2000 12:38:10 
> > +0100, nobull@mail.com <nobull@mail.com> says...
> > > priitr.randla@eyp.ee (Priit Randla) writes:
> > > 
> > > >  ($key,$info{$key}) = split /=/ while <> ;
> > > >  Obviously this does not work and i do understand why($key not evaluated yet
> > > >  when evaluating $info{$key}).
> > > 
> > > >  Is there some way to initialize hash _elements_ using lists as 
> > > >  %hash = ($key1, $val1, $key2, $val2 ) is doing for _entire_ hash?
> > > 
> > > %hash = %hash, $key, $val;
> > 
> > Unparenthesized, that doesn't do what you think it does.  The '-w' flag 
> > would warn you about this error.
> 
> Yup, I've been using Perl for several years and yet I still forget
> that the presedence of assignment and comma are not the way one would
> expect.

That shows that you didn't learn C first.  In C, the comma operator is 
like the Perl comma operator in scalar context -- evaluate the left 
expression, then evaluate the right expression.  In that context, the 
ultra-low precedence of the comma operator makes eminent sense.

In Perl's list context, the precence of assignment above comma makes 
less sense, as you imply, but it's better the precedences be the same 
independent of context, and history wins.

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


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

Date: Fri, 18 Aug 2000 23:09:52 GMT
From: mjd@plover.com (Mark-Jason Dominus)
Subject: Re: Recursion Help
Message-Id: <399dc23f.1127$2fc@news.op.net>

In article <8nkb1m$57f$1@news.service.uci.edu>, Gabe <grichard@uci.edu> wrote:
>It seems I can't get this recursion to work properly. The program goes down
>two directories deep, but when it find there are no more directory entries
>it doesn't return to the previous level.
>sub Recurse {
>    my $dir = shift;
>    opendir(DIR, $dir) or die "Could not open directory: $!";
>    @messagelist = map { "$dir/$_" } grep (!/^\./, readdir(DIR));


@messagelist is a global variable.  The second call to Recurse
overwrites the old value of @messagelist, and then when the second
call returns, the first call tries to go ahead with the next item in
@messagelist, and discovers that @messagelist does not contain the
data that it should.

To fix this, add 'my':

     my @messagelist = map { "$dir/$_" } grep (!/^\./, readdir(DIR));

This tells Perl that each call to Recurse should use a separate,
unrelated @messagelist.  Then the @messagelist in the second call will
be a different variable from the one in the first call and the two
calls will not interfere.

If you add 

        use strict 'vars';

at the top of your program, Perl will remind you to use 'my'
delcarations on your variables.

Also, for functions like this that walk a directory tree and do
something for each file, you should consider using the File::Find
module that comes with Perl.  You would say something like

        use File::Find;
        find (\&my_function, TOP_LEVEL_DIRECTORY);

and then Perl will call 'my_function' for every file in the top directory
and  and in every directory under it.  Then you make your function
look like this:

        sub my_function {
          
          # Only process plain files whose names end in '.html';
          return unless -f && /\.html$/; 

          $parser->parse_file($i);
          # etc. 
        }
          

This is usually easier than building the recusion machinery yourself.



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

Date: Fri, 18 Aug 2000 22:58:20 GMT
From: Rick Delaney <rick.delaney@home.com>
Subject: Re: Regex Alternation Question
Message-Id: <399DC1D1.6C868225@home.com>


Mark-Jason Dominus wrote:
> 
> In article <399BB4B9.B779C6F0@ppi.de>,
> Jonas Reinsch  <Jonas.Reinsch@ppi.de> wrote:
> >The best solution I could think of is something like:
> >
> >for ($i = 0; $i<@parens; $i++) {
> >    if ($parens[$i]) {$part = $alt[$i];last}
> >}
> >Is there a more direct way to do this?

With all your constraints, yes.  With v5.6, anyway.
 
> That's the only way I know of to do it, and when I've showed it in my
> classes at the Perl conference nobody has ever suggested a better way.

From the depths of perlvar, emerges:

    $part = $alt[$#- - 1];

This is buried in the @- section of perlvar:

   One can use $#- to find the last matched subgroup in the last 
   successful match.  Contrast with $#+, the number of subgroups
   in the regular expression.

-- 
Rick Delaney
rick.delaney@home.com


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

Date: Fri, 18 Aug 2000 23:07:01 GMT
From: fgont@my-deja.com
Subject: Re: Simple regex problem
Message-Id: <8nkfi9$2av$1@nnrp1.deja.com>

In article <m3aeeaz16k.fsf@underlevel.underlevel.net>,
  Jordan Katz <katz@underlevel.net> wrote:

> >      $_=@_[0];
> `@_[0] better written as $_[0]`

Why?
What I tried to write is "I want the 0 element of the @_ array".
Why is it better to write it as $_[0]
(Sorry if it's a stupid question, but I'm rather new to Perl)



> >      if(/\n\n/){
> >           return($PREMATCH, $POSTMATCH);
> >      }


I've found that $PREMATCH an $POSTMATCH functions and it seems that
they don't work... I f I use them, my code doesn't work, but if I use
$` and $' instead, my code works OK... do you know why?


I continued looking at my regex, and I've found that the problem was
not the regexp, but the data I was "analyzing".

I read the file that I was analyzing in a hexadecimal editor, and I
found the 0dh 0ah 0dh 0ah  corresponding to \n\n .

But, eventhough those bytes are there, neither your example (with
split) nor my regexp work.
Do you know what could be the problem?

Kind regards,

Fernando Ariel Gont
e-mail: fgont@NOSPAM.softhome.net



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


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

Date: Fri, 18 Aug 2000 16:45:11 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Simple regex problem
Message-Id: <MPG.14076a67a39029198ac9d@nntp.hpl.hp.com>

In article <8nkfi9$2av$1@nnrp1.deja.com> on Fri, 18 Aug 2000 23:07:01 
GMT, fgont@my-deja.com <fgont@my-deja.com> says...
> In article <m3aeeaz16k.fsf@underlevel.underlevel.net>,
>   Jordan Katz <katz@underlevel.net> wrote:
> 
> > >      $_=@_[0];
> > `@_[0] better written as $_[0]`
> 
> Why?
> What I tried to write is "I want the 0 element of the @_ array".
> Why is it better to write it as $_[0]
> (Sorry if it's a stupid question, but I'm rather new to Perl)

It isn't so stupid a question that it isn't a Frequently Asked Question.  
You should get acquainted with the voluminous Perl FAQ.  Your question 
is in perlfaq4: "What is the difference between $array[1] and 
@array[1]?"

> > >      if(/\n\n/){
> > >           return($PREMATCH, $POSTMATCH);
> > >      }
> 
> 
> I've found that $PREMATCH an $POSTMATCH functions and it seems that
> they don't work... I f I use them, my code doesn't work, but if I use
> $` and $' instead, my code works OK... do you know why?

Yes.  You forgot to include

    use English;

in your program to define those names.  And you forgot to use the '-w' 
flag and

    use strict;

so you would see diagnostics telling you that you were using 
uninitialized variables.

> I continued looking at my regex, and I've found that the problem was
> not the regexp, but the data I was "analyzing".
> 
> I read the file that I was analyzing in a hexadecimal editor, and I
> found the 0dh 0ah 0dh 0ah  corresponding to \n\n .
> 
> But, eventhough those bytes are there, neither your example (with
> split) nor my regexp work.
> Do you know what could be the problem?

The external (in a file) representation of newline on your Windows/DOS 
system is the pair of characters "\15\12".  When read into memory by 
your program, by default they are converted to the single character 
"\n".  So everything should work, if declared properly.

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


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

Date: Fri, 18 Aug 2000 19:51:15 -0400
From: Drew Simonis <care227@attglobal.net>
Subject: Re: Simple regex problem
Message-Id: <399DCBF3.7CB64012@attglobal.net>

fgont@my-deja.com wrote:
> 
> In article <m3aeeaz16k.fsf@underlevel.underlevel.net>,
>   Jordan Katz <katz@underlevel.net> wrote:
> 
> > >      $_=@_[0];
> > `@_[0] better written as $_[0]`
> 
> Why?
> What I tried to write is "I want the 0 element of the @_ array".
> Why is it better to write it as $_[0]
> (Sorry if it's a stupid question, but I'm rather new to Perl)
> 

You want the 0th element of an array, which is not just an array 
element, its a scalar.  Right?  (right)  Thats how I remembered
the differences.  @ is for arrays, $ is for scalars.  Even though 
the bit of data I want comes _from_ an array, it is in effect 
a scalar.  So, $_[0] will get me the 0th scalar in the array.  
(@_[0] will get a slice of the array, which usually isn't what 
is needed)


> > >      if(/\n\n/){
> > >           return($PREMATCH, $POSTMATCH);
> > >      }
> 
> I've found that $PREMATCH an $POSTMATCH functions and it seems that
> they don't work... I f I use them, my code doesn't work, but if I use
> $` and $' instead, my code works OK... do you know why?

Answered in your second post of this particular question as well as in
the Perl documentation. (perlvar to be specific)

 
> I continued looking at my regex, and I've found that the problem was
> not the regexp, but the data I was "analyzing".

The problem is using a regex in the first place.  Using $` and $' aren't
good for performance.  They slow regex matching down considerably.  If
the 
element you have is really just:

$variable = "bunch of text \n\n even more text"

you would be (as I already mentioned) much better served using split(),
which is designed to do just what you want.
 
> I read the file that I was analyzing in a hexadecimal editor, and I
> found the 0dh 0ah 0dh 0ah  corresponding to \n\n .
> 
> But, eventhough those bytes are there, neither your example (with
> split) nor my regexp work.
> Do you know what could be the problem?

A hex editor?  Is this data binary?


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

Date: Sat, 19 Aug 2000 00:48:26 GMT
From: Jim Mauldin <mauldin@netstorm.net>
Subject: Re: substring-p
Message-Id: <399DD8AD.D48C34A7@netstorm.net>

Art Werschulz wrote:
> 
> Hi.
> 
> I would like to determine whether a particular string (say, $word)
> appears as a substring of another string (say, $keywords).  The
> obvious thing to do would be
> 
>   print $word, "\n" if ($keywords =~ $word);
> 
> Unfortunately, this doesn't work if (e.g) $word contains a
> parenthesis.  The main point here is that I would like $word to be
> treated as a garden variety string, not a regular expression.
> 
> Ideas?  Thanks.

See
perldoc perlop
and look for "Binding Operators"

Your only hope is to prepare $word before the match so that it contains
only what you're looking for. If you wanted to remove all
non-alphanumeric characters from $word beforehand, you could do:

$word = tr/A-Za-z0-9//cd;

But please read:

perldoc perlop
looking for "Quote and Quote-like Operators"

before so you know what's going on.  You might also want to be explicit
about the pattern match and use the /i modifier to do a case-insensitive
search:

print $word, "\n" if $keywords =~ /$word/i;

-- Jim


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

Date: Sat, 19 Aug 2000 00:53:46 GMT
From: Jim Mauldin <mauldin@netstorm.net>
Subject: Re: substring-p
Message-Id: <399DD9ED.DCEBBEA3@netstorm.net>

I wrote:

> $word = tr/A-Za-z0-9//cd;
> 

Sheesh ... I better get some sleep.  I meant, of course

$word =~ tr/A-Za-z0-9//cd;

-- Jim


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

Date: Fri, 18 Aug 2000 15:04:56 -0700
From: "Gabe" <grichard@uci.edu>
Subject: Re: Use of uninitialized value, but it's not uninitialized.
Message-Id: <8nkc87$5s4$1@news.service.uci.edu>

So what do I have to do? Give it a value first? Why?

Gabe


Decklin Foster <decklin+usenet@red-bean.com> wrote in message
news:u7in5.1578$CW2.11776@news1.rdc1.ct.home.com...
> Gabe <grichard@uci.edu> writes:
>
> >     my $id; # Here the value is initialized, no?
>
> No. Same goes for $i below.
>
> >         ID: {
> >             for (my $i; $i<10; $i++) {
> >             $id .= int(rand 9);
> >             }
> >         }
>
> That's a very odd way of going about it.
>
>   my $id = sprintf "%010d", rand 10_000_000_000;
>
> But if your system won't generate random numbers that big (you have
> 32-bit ints, for example), you can work around it with
>
>   my $id = sprintf "%05d%05d\n", rand 100_000, rand 100_000;
>
> --
> There is no TRUTH. There is no REALITY. There is no CONSISTENCY. There
> are no ABSOLUTE STATEMENTS. I'm very probably wrong. -- BSD fortune(6)




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

Date: Sat, 19 Aug 2000 00:33:24 +0200
From: Abe Timmerman <abe@ztreet.demon.nl>
Subject: Re: Use of uninitialized value, but it's not uninitialized.
Message-Id: <9vdrpssru3j29plcepi0dbrpr1gd2ul5u0@4ax.com>

On Fri, 18 Aug 2000 14:07:46 -0700, "Gabe" <grichard@uci.edu> wrote:

> I'm getting the following error:
> 
> Use of uninitialized value at parsehtml.pl line 83.

It's actually a warning (as produced by '-w' and family).
 
> My code is:
> sub ID {
>     my $id; # Here the value is initialized, no?

Nope it's 'undef'.  Just assign it the empty string.

	my $id = '';


-- 
Good luck,
Abe


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

Date: Fri, 18 Aug 2000 15:21:03 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Use of uninitialized value, but it's not uninitialized.
Message-Id: <MPG.140756a651863ec898ac99@nntp.hpl.hp.com>

In article <8nk8t0$473$1@news.service.uci.edu> on Fri, 18 Aug 2000 
14:07:46 -0700, Gabe <grichard@uci.edu> says...
> I'm getting the following error:
> 
> Use of uninitialized value at parsehtml.pl line 83.

That is a warning, not an error.  But it shouldn't be happening, and you 
don't want to see it.  So...

> My code is:
> sub ID {
>     my $id; # Here the value is initialized, no?

No.  It is declared but not initialized.  Its value is undefined.  But 
that isn't what is causing the warning.

>     unless (@_) {
>         ID: {
>             for (my $i; $i<10; $i++) {
>             $id .= int(rand 9);
>             }

See below for comments on this loop.

>         }
>     }
>     else {
>         my $table = $_[0];
>         my $idfield = $_[1];
>         my $dbh = $_[2];

          my ($table, $idfield, $dbh) = @_;

>         my $getids = $dbh->prepare(qq{SELECT $idfield FROM $table});
>         $getids->execute;
>         ID: {
>             for (my $i; $i<10; $i++) { #This is line 83

The warning is coming from $i, not $id.  You should write

              for (my $i = 0; $i < 10; $i++) {

But that is C code.  A Perl programmer would write the whole loop as:

              $id .= int rand 9 for 1 .. 10;

>                 $id .= int(rand 9);

This produces no warning because of special dispensation on the .= 
operator.  There is no such dispensation on the < operator in the 
previous line.

>             }
>            while (my @row = $getids->fetchrow_array) {
>                 if (@row eq $id) { redo ID;}

That should be:

                  if ($row[0] eq $id) { redo ID }

You want to use the value returned, not the size of the array, which 
should be 1 for each valid return.

>             }
>         }
>      }
>      return $id;
> }

You are creating a 10-digit integer composed of random digits from 0 to 
8.  I'll bet you want to include 9 in the set of digits.  The fix is 
obvious.

Then you are fetching and searching an entire table to see if that 
number is in the table.  This causes a lot of unnecessary traffic with 
the DBM.  Why not just ask the DBM if the number is there?

After computing the new value of $id:

         my $getids = $dbh->prepare(
             qq{SELECT $idfield FROM $table WHERE $idfield = $id});
         $getids->execute;

Then you will get back one row or nothing, not the entire contents of 
the table.

I haven't tested that, but if it's wrong it's an SQL issue, not a Perl 
issue, anyway.  :-)

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


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

Date: Fri, 18 Aug 2000 15:30:43 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Use of uninitialized value, but it's not uninitialized.
Message-Id: <MPG.140758f1762eae9598ac9a@nntp.hpl.hp.com>

In article <u7in5.1578$CW2.11776@news1.rdc1.ct.home.com> on Fri, 18 Aug 
2000 21:44:58 GMT, Decklin Foster <decklin+usenet@red-bean.com> says...

 ...

>   my $id = sprintf "%010d", rand 10_000_000_000;

On many (most?) machines, that will give you a choice among a set of 
2**15 (32768) numbers, not 10**10 numbers.
 
> But if your system won't generate random numbers that big (you have
> 32-bit ints, for example), you can work around it with
> 
>   my $id = sprintf "%05d%05d\n", rand 100_000, rand 100_000;

Better, but still a restricted set of numbers from each rand().

I think the best approach is what I posted, the same as the OP's 
algorithm with corrections:

    my $id;
    $id .= int rand 10 for 1 .. 10;

Another way:

    my $id = join "" => map int rand 10 => 1 .. 10;

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


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

Date: Fri, 18 Aug 2000 15:53:39 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: Use of uninitialized value, but it's not uninitialized.
Message-Id: <MPG.14075e506ed76f9498ac9c@nntp.hpl.hp.com>

In article <9vdrpssru3j29plcepi0dbrpr1gd2ul5u0@4ax.com> on Sat, 19 Aug 
2000 00:33:24 +0200, Abe Timmerman <abe@ztreet.demon.nl> says...
> On Fri, 18 Aug 2000 14:07:46 -0700, "Gabe" <grichard@uci.edu> wrote:
> 
> > I'm getting the following error:
> > 
> > Use of uninitialized value at parsehtml.pl line 83.
> 
> It's actually a warning (as produced by '-w' and family).
>  
> > My code is:
> > sub ID {
> >     my $id; # Here the value is initialized, no?
> 
> Nope it's 'undef'.  Just assign it the empty string.
> 
> 	my $id = '';

Sorry, that doesn't fix the problem.  See my response.  The .= operator 
doesn't care about an undefined value.

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


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

Date: 18 Aug 2000 23:02:37 +0100
From: Jonathan Stowe <gellyfish@gellyfish.com>
Subject: Re: We Need Your Feedback
Message-Id: <8nkbpt$bl7$1@orpheus.gellyfish.com>

On Fri, 18 Aug 2000 15:50:01 GMT waseema@my-deja.com wrote:
> Hi
> 
> We are currently at the very end of the developemnt of a brand new IT
> site , covering every issue to do with the this vast medium of
> communication and business.

Bollocks, get stuffed spammer.

/J\
-- 
yapc::Europe in assocation with the Institute Of Contemporary Arts
   <http://www.yapc.org/Europe/>   <http://www.ica.org.uk>


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

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


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