[22892] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 5113 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Jun 12 18:16:02 2003

Date: Thu, 12 Jun 2003 15:15:19 -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           Thu, 12 Jun 2003     Volume: 10 Number: 5113

Today's topics:
    Re: Sorting Algorithm: organize data into a tree <goedicke@goedsole.com>
    Re: Sorting Algorithm: organize data into a tree (Nikolai Chuvakhin)
    Re: Sorting Algorithm: organize data into a tree (Jay Tilton)
        trying to understand regexp backtracking in Perl <layer@*n*o*s*p*a*m*franz.com>
        Using GET with $ENV{'QUERY_STRING'} <woll@yahoo.com>
    Re: Using GET with $ENV{'QUERY_STRING'} <matthew@weierophinney.net>
    Re: Using GET with $ENV{'QUERY_STRING'} <mbudash@sonic.net>
    Re: Using GET with $ENV{'QUERY_STRING'} (Tad McClellan)
    Re: Which value is tha largest(benchmarked) <TruthXayer@yahoo.com>
    Re: Which value is tha largest(benchmarked) <usenet@dwall.fastmail.fm>
    Re: Which value is tha largest(benchmarked) <TruthXayer@yahoo.com>
        Which value is tha largest <hillmw@ram.lmtas.lmco.com>
    Re: Which value is tha largest <ian@WINDOZEdigiserv.net>
    Re: Which value is tha largest (Tad McClellan)
    Re: Which value is tha largest <usenet@dwall.fastmail.fm>
    Re: Which value is tha largest <hillmw@ram.lmtas.lmco.com>
        why does perl do it? (i5513)
    Re: why does perl do it? <ndronen@io.frii.com>
    Re: why does perl do it? <mbudash@sonic.net>
    Re: why does perl do it? (Malcolm Dew-Jones)
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Thu, 12 Jun 2003 17:12:05 GMT
From: William Goedicke <goedicke@goedsole.com>
Subject: Re: Sorting Algorithm: organize data into a tree
Message-Id: <m3znknte3s.fsf@mail.goedsole.com>

Dear Jeffrey - 

"Jeffrey Silverman" <jeffrey@jhu.edu> writes:

> I need to organize a multi-dimensional array into a tree.

use Graph;

     Yours -      Billy

============================================================
     William Goedicke     goedicke@goedsole.com            
                          http://www.goedsole.com:8080      
============================================================

          Lest we forget:

Where there's a whim there's a way.

		- William Goedicke


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

Date: 12 Jun 2003 12:51:33 -0700
From: nc@iname.com (Nikolai Chuvakhin)
Subject: Re: Sorting Algorithm: organize data into a tree
Message-Id: <32d7a63c.0306121151.46fab4d0@posting.google.com>

"Jeffrey Silverman" <jeffrey@jhu.edu> wrote in message 
   news:<pan.2003.06.12.14.09.21.340715@jhu.edu>...
> 
> I am cross-posting to PHP and Perl newsgroups, as the algorithm I am
> seeking should be fairly language-agnostic, and I know both Perl and PHP.
> 
> I need to organize a multi-dimensional array into a tree. The data
> structure is roughly as follows:
> 
> array(
>      "A" => array("parent"=>"B", "self"=>"A", "data"=>"aaaaa") ,
>      "B" => array("parent"=>"default", "self"=>"B", "data"=>"bbbbb") ,
>      "C" => array("parent"=>"B", "self"=>"C", "data"=>"ccccc") ,
>      "D" => array("parent"=>"default", "self"=>"D", "data"=>"ddddd") ,
>      "E" => array("parent"=>"C", "self"=>"E", "data"=>"eeeee") ,
>      "F" => array("parent"=>"C", "self"=>"F", "data"=>"fffff") ,
>      "G" => array("parent"=>"D", "self"=>"G", "data"=>"ggggg") ,
>      )
> 
> Note that it is not already sorted in any way.
> 
> I need an algorithm to organize it into its tree shape, like so:
> 
> array(
>      "default" => array(
>                         "B" => array(
>                                      "A" => array(
>                                                  ),
>                                      "C" => array(
>                                                   "E" => array(),
>                                                   "F" => array(),
>                                                  ),
>                                     ),
>                         "D" => array(
>                                     "G" => array(),
>                                     )
>                        )
>      )

Let me ask a couple of clarifying questions: 

1. What happens to the "data"?  I can't see it anywhere in the 
   structure of the target array?  In the source array, element 
   "B" has "data", in the target array, it doesn't. 

2. What is the significance of the "self" field in the original 
   array?  If it only duplicates a higher-level key, it serves 
   no purpose other than creating confusion.  If it can be 
   different from the higher-level key, it can screw up the 
   conversion.  For example, the array 

   array(
     "A" => array("parent"=>"default", "self"=>"A", "data"=>"aaaaa"),
     "B" => array("parent"=>"default", "self"=>"A", "data"=>"bbbbb"),
   )

   would be converted into 

   array(
     "default" => array("A" => array())
   )

   because the second element would overwrite the first, the 
   reason being their identical values of "self". 

3. How do you safeguard against circular references?  How would 
   you get out of something like this? 

   array(
     "A" => array("parent"=>"B", "self"=>"A", "data"=>"aaaaa"),
     "B" => array("parent"=>"A", "self"=>"B", "data"=>"bbbbb"),
   )

Cheers, 
NC


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

Date: Thu, 12 Jun 2003 21:11:43 GMT
From: tiltonj@erols.com (Jay Tilton)
Subject: Re: Sorting Algorithm: organize data into a tree
Message-Id: <3ee8eb93.85335681@news.erols.com>

"Jeffrey Silverman" <jeffrey@jhu.edu> wrote:

: I am cross-posting to PHP and Perl newsgroups, as the algorithm I am
: seeking should be fairly language-agnostic, and I know both Perl and PHP.
: 
: I need to organize a multi-dimensional array into a tree. The data
: structure is roughly as follows:
: 
: array(
:      "A" => array("parent"=>"B", "self"=>"A", "data"=>"aaaaa") ,
:      "B" => array("parent"=>"default", "self"=>"B", "data"=>"bbbbb") ,
:      "C" => array("parent"=>"B", "self"=>"C", "data"=>"ccccc") ,
:      "D" => array("parent"=>"default", "self"=>"D", "data"=>"ddddd") ,
:      "E" => array("parent"=>"C", "self"=>"E", "data"=>"eeeee") ,
:      "F" => array("parent"=>"C", "self"=>"F", "data"=>"fffff") ,
:      "G" => array("parent"=>"D", "self"=>"G", "data"=>"ggggg") ,
:      )
: 
: Note that it is not already sorted in any way.
: 
: I need an algorithm to organize it into its tree shape, like so:
: 
: array(
:      "default" => array(
:                         "B" => array(
:                                      "A" => array(
:                                                  ),
:                                      "C" => array(
:                                                   "E" => array(),
:                                                   "F" => array(),
:                                                  ),
:                                     ),
:                         "D" => array(
:                                     "G" => array(),
:                                     )
:                        )
:      )

Okie dokie.

    #!perl
    use warnings;
    use strict;
    my %items = (
         A => {parent => "B",       self => "A", data => "aaaaa"} ,
         B => {parent => "default", self => "B", data => "bbbbb"} ,
         C => {parent => "B",       self => "C", data => "ccccc"} ,
         D => {parent => "default", self => "D", data => "ddddd"} ,
         E => {parent => "C",       self => "E", data => "eeeee"} ,
         F => {parent => "C",       self => "F", data => "fffff"} ,
         G => {parent => "D",       self => "G", data => "ggggg"} ,
    );
    # determine heritage
    my %tree;
    while( my($k, $v) = each %items ) {
        my $parent = $v->{parent};
        next unless defined $parent;
        $tree{$k} ||= {};
        $tree{$parent} ||= {};
        $tree{$parent}{$k} = $tree{$k};
    }
    # depth-first search and destroy children from root of %tree
    sub cleanup {
        my($href) = @_;
        for( keys %$href ) {
            cleanup( $href->{$_} );
            delete $tree{$_};
        }
    }
    cleanup( $tree{default} );
    # show results
    use Data::Dumper;
    $Data::Dumper::Sortkeys = 1;
    print Dumper( \%tree );



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

Date: 12 Jun 2003 14:32:46 -0700
From: Kevin Layer <layer@*n*o*s*p*a*m*franz.com>
Subject: trying to understand regexp backtracking in Perl
Message-Id: <mkznknc77l.fsf@*n*o*s*p*a*m*franz.com>

I'm trying to understand better how backtracking works with Perl
regexps.  I looked briefly at the C source code, but I figure I have a
better chance of understanding the output below.

If you execute this Perl script:

  use re 'debug';
  "abbcbc" =~ /a(b|bc)*bc/;

you'll see something like this (in perl 5.6.1, without line numbers):

 1	Matching REx `a(b|bc)*bc' against `abbcbc'
 2	  Setting an EVAL scope, savestack=21
 3	   0 <> <abbcbc>          |  1:  EXACT <a>
 4	   1 <a> <bbcbc>          |  3:  CURLYX[0] {0,32767}
 5	   1 <a> <bbcbc>          | 15:    WHILEM[1/1] 0 out of ..
 6	  Setting an EVAL scope, savestack=26
 7	   1 <a> <bbcbc>          |  5:      OPEN1
 8	   1 <a> <bbcbc>          |  7:      BRANCH
 9	  Setting an EVAL scope, savestack=36
10	   1 <a> <bbcbc>          |  8:        EXACT <b>
11	   2 <ab> <bcbc>          | 13:        CLOSE1
12	   2 <ab> <bcbc>          | 15:        WHILEM[1/1] 1 out of ..
13	  Setting an EVAL scope, savestack=45
14	   2 <ab> <bcbc>          |  5:          OPEN1
15	   2 <ab> <bcbc>          |  7:          BRANCH
16	  Setting an EVAL scope, savestack=55
17	   2 <ab> <bcbc>          |  8:            EXACT <b>
18	   3 <abb> <cbc>          | 13:            CLOSE1
19	   3 <abb> <cbc>          | 15:            WHILEM[1/1] 2 out of ..
20	  Setting an EVAL scope, savestack=64
21	   3 <abb> <cbc>          |  5:              OPEN1
22	   3 <abb> <cbc>          |  7:              BRANCH
23	  Setting an EVAL scope, savestack=74
24	   3 <abb> <cbc>          |  8:                EXACT <b>
25	                                          failed...
26	   3 <abb> <cbc>          | 11:                EXACT <bc>
27	                                          failed...
28	  Clearing an EVAL scope, savestack=64..74
29	     restoring \1 to 2(2)..3
30	                                      failed, try continuation...
31	   3 <abb> <cbc>          | 16:              NOTHING
32	   3 <abb> <cbc>          | 17:              EXACT <bc>
33	                                        failed...
34	                                      failed...
35	   2 <ab> <bcbc>          | 11:            EXACT <bc>
36	   4 <abbc> <bc>          | 13:            CLOSE1
37	   4 <abbc> <bc>          | 15:            WHILEM[1/1] 2 out of ..
38	  Setting an EVAL scope, savestack=64
39	   4 <abbc> <bc>          |  5:              OPEN1
40	   4 <abbc> <bc>          |  7:              BRANCH
41	  Setting an EVAL scope, savestack=74
42	   4 <abbc> <bc>          |  8:                EXACT <b>
43	   5 <abbcb> <c>          | 13:                CLOSE1
44	   5 <abbcb> <c>          | 15:                WHILEM[1/1] 3 out of ..
45	  Setting an EVAL scope, savestack=83
46	   5 <abbcb> <c>          |  5:                  OPEN1
47	   5 <abbcb> <c>          |  7:                  BRANCH
48	  Setting an EVAL scope, savestack=93
49	   5 <abbcb> <c>          |  8:                    EXACT <b>
50	                                              failed...
51	   5 <abbcb> <c>          | 11:                    EXACT <bc>
52	                                              failed...
53	  Clearing an EVAL scope, savestack=83..93
54	     restoring \1 to 4(4)..5
55	                                          failed, try continuation...
56	   5 <abbcb> <c>          | 16:                  NOTHING
57	   5 <abbcb> <c>          | 17:                  EXACT <bc>
58	                                            failed...
59	                                          failed...
60	   4 <abbc> <bc>          | 11:                EXACT <bc>
61	   6 <abbcbc> <>          | 13:                CLOSE1
62	   6 <abbcbc> <>          | 15:                WHILEM[1/1] 3 out of ..
63	  Setting an EVAL scope, savestack=83
64	   6 <abbcbc> <>          |  5:                  OPEN1
65	   6 <abbcbc> <>          |  7:                  BRANCH
66	  Setting an EVAL scope, savestack=93
67	   6 <abbcbc> <>          |  8:                    EXACT <b>
68	                                              failed...
69	   6 <abbcbc> <>          | 11:                    EXACT <bc>
70	                                              failed...
71	  Clearing an EVAL scope, savestack=83..93
72	     restoring \1 to 4(4)..6
73	                                          failed, try continuation...
74	   6 <abbcbc> <>          | 16:                  NOTHING
75	   6 <abbcbc> <>          | 17:                  EXACT <bc>
76	                                            failed...
77	                                          failed...
78	  Clearing an EVAL scope, savestack=64..74
79	     restoring \1 to 2(2)..4
80	                                      failed, try continuation...
81	   4 <abbc> <bc>          | 16:              NOTHING
82	   4 <abbc> <bc>          | 17:              EXACT <bc>
83	   6 <abbcbc> <>          | 19:              END

Some questions:

1   I'm assuming that "Setting an EVAL scope, savestack=..." means
    state is saved for later backtracking.

    Why does Perl save state when entering a group context (lines 2, 7,
    14, ...)?

2  "Clearing an EVAL scope, savestack=64..74" appears to remove
    states the two states 64 and 74 from the stack, and restore the
    match state to that as it was when `savestack=64' was created.

    True?

3   Is there backtracking between lines 33 & 34?  58 and 59?  76 & 77?
    It would seem so, but why not detail it?  There has to be.

    For lines 33 & 34: at line 32, EXACT <bc> is done after
    backtracking (line 30) to the state saved at line 16, and is the
    continuation of the EXACT <b> on line 17.  No where do we see
    "Clearning an EVAL scope, savestack=55", but going from line 32 to
    35 shows the match state going from

	<abb> <cbc>
    to
	<ab> <bcbc>

    This has to mean backtracking occurred, right?

4   Jeffrey Friedl (in Master Regular Expressions, 2nd edition) says
    that saved states are LIFO.  So, why does Perl (and how does it
    know to) pop two states at once (line 28) for one failure (line
    27)?

    My guess:

    Because both alternates failed, we need to go back to a previous
    saved.  However, the most recent saved state is bogus, since it
    corresponds to the state saved just before the two failed matches
    (lines 24-27).  So, the bogus state (74) is tossed and we go back
    to the good state (64), and hence the:

       Clearing an EVAL scope, savestack=64..74

5   "restoring \1 to 2(2)..3" seems to mean:

    restoring the 1st captured group to what's between index `2' and `3'
    in the input string.  I don't know what the `(2)'.  The `..3'
    seems to correspond perfectly to the current index in the string
    after the state is restored.

    What does the `(2)' mean?

6   "failed, try continuation..." seems to mean backtracking to try an
    another alternate, rather "failed..." which is just vanilla
    backtracking.

    True?

Thanks in advance.

Kevin


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

Date: Thu, 12 Jun 2003 16:49:13 +1000
From: "Bob" <woll@yahoo.com>
Subject: Using GET with $ENV{'QUERY_STRING'}
Message-Id: <bcahtl$hdmsk$1@ID-82947.news.dfncis.de>

In the HTML form I have these following paramters passed from this form into
a Perl module.
?item=doo200&action=sell&amount=4&Submit=Submit+Query

which go into QUERY_STRING
the doco I have says to do it like this . . .
@values = split(/&/,$ENV{'QUERY_STRING'});
foreach $input (@values)
{($one, $two) = split(/=/,$input);

Which gives  . . . .
One : Item       Two: doo200
One : Action     Two: sell
One : Amount   Two: 48
One : Submit    Two: Submit+Query


if I want to use the values 'd00200' and 'sell' how do I express it to get
at these values and evaluate these to do whatever processing required ? or
whould I concentrate ini just gettiing rid of the & and  = in a 1 d array ??

I find this confusing because doo200 and  sell then are in $two ?????
if $two eq 'sell'
#   check the databse for item doo200
   if $database_key eq $two    #doo200
   { something}
how is this ever supposed to work ?

TIA
Bob


TIA
Bob.







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

Date: Thu, 12 Jun 2003 18:41:12 GMT
From: Matthew Weier O'Phinney <matthew@weierophinney.net>
Subject: Re: Using GET with $ENV{'QUERY_STRING'}
Message-Id: <slrnbehia8.1a6.matthew@kavalier.weierophinney.net>

* Bob <woll@yahoo.com>:
> In the HTML form I have these following paramters passed from this form into
> a Perl module.
> ?item=doo200&action=sell&amount=4&Submit=Submit+Query
> 
> which go into QUERY_STRING
> the doco I have says to do it like this . . .
> @values = split(/&/,$ENV{'QUERY_STRING'});
> foreach $input (@values)
> {($one, $two) = split(/=/,$input);

Don't do it this way. Use CGI or CGI::Simple to parse the query string;
they have tried and true methods, and will alleviate any headaches
you're experiencing.

-- 
Matthew Weier O'Phinney
matthew@weierophinney.net
http://matthew.weierophinney.net


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

Date: Thu, 12 Jun 2003 18:42:44 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: Using GET with $ENV{'QUERY_STRING'}
Message-Id: <mbudash-285121.11424412062003@typhoon.sonic.net>

In article <bcah9l$gua5q$1@ID-82947.news.dfncis.de>,
 "Bob" <woll@yahoo.com> wrote:

> In the HTML form I have these following paramters passed from this form into
> a Perl module.
> ?item=doo200&action=sell&amount=4&Submit=Submit+Query
> 
> which go into QUERY_STRING
> the doco I have says to do it like this . . .
> @values = split(/&/,$ENV{'QUERY_STRING'});
> foreach $input (@values)
> {($one, $two) = split(/=/,$input);
> 
> Which gives  . . . .
> One : Item       Two: doo200
> One : Action     Two: sell
> One : Amount   Two: 48
> One : Submit    Two: Submit+Query
> 
> 
> if I want to use the values 'd00200' and 'sell' how do I express it to get
> at these values and evaluate these to do whatever processing required ? or
> whould I concentrate ini just gettiing rid of the & and  = in a 1 d array ??
> 
> TIA
> Bob.
> 
> 

use CGI;

http://stein.cshl.org/WWW/software/CGI/

look into the param() method.
-- 
Michael Budash


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

Date: Thu, 12 Jun 2003 16:50:18 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Using GET with $ENV{'QUERY_STRING'}
Message-Id: <slrnbehtcq.3ap.tadmc@magna.augustmail.com>

Bob <woll@yahoo.com> wrote:

> the doco I have says to do it like this . . .
> @values = split(/&/,$ENV{'QUERY_STRING'});


What docs are you referring to?

It would be of service to all of your fellow programmers if you
could warn them off of such poor advice.


   use CGI;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Thu, 12 Jun 2003 13:19:07 -0700
From: TruthXayer <TruthXayer@yahoo.com>
Subject: Re: Which value is tha largest(benchmarked)
Message-Id: <3EE8E03B.671223DC@yahoo.com>

"David K. Wall" wrote:
> =

> Michael Hill <hillmw@ram.lmtas.lmco.com> wrote:
> =

> > Without having to have a nexted if else syntax does anyone know an
> > easy way to determine which value is the largest?
> >
> > $one =3D 3;
> > $two =3D 5;
> > $tre =3D  1;
> > $for =3D 10;
> >
> =

> sub max {
>     my $max =3D shift;
>     $max =3D $_ > $max ? $_ : $max foreach @_;
>     return $max;
> }
> =

> my $maximum =3D max($one, $two, $tre, $for);


Interessting , I benchmarked two cases:
	-> small unsorted arrays
	-> big sorted arrays	(got sorted much quicker with right
ordering)
	-> big reverse sorted  (got awfully slow)

So keep those arrays sorted as much as possible to improve
performance else you are done =

for!


Benchmark: timing 1000000 iterations of MySort, Sort...
    MySort: 50 wallclock secs (49.66 usr +  0.00 sys =3D 49.66
CPU) @ 20136.93/s (n=3D1000000)
      Sort: 71 wallclock secs (71.04 usr +  0.00 sys =3D 71.04
CPU) @ 14076.58/s (n=3D1000000)
     =

    MySort: 50 wallclock secs (49.71 usr +  0.00 sys =3D 49.71
CPU) @ 20116.68/s (n=3D1000000)
      Sort: 72 wallclock secs (71.21 usr +  0.00 sys =3D 71.21
CPU) @ 14042.97/s (n=3D1000000)

Benchmark: timing 1000000 iterations of MySortBig,
SortBig...(in this case i entered an already sorted array in
correct order)

 MySortBig:  9 wallclock secs ( 8.72 usr +  0.00 sys =3D  8.72
CPU) @ 114678.90/s (n=3D1000000)
   SortBig:  6 wallclock secs ( 7.59 usr +  0.00 sys =3D  7.59
CPU) @ 131752.31/s (n=3D1000000)
 =

 MySortBig: 10 wallclock secs ( 9.09 usr +  0.00 sys =3D  9.09
CPU) @ 110011.00/s (n=3D1000000)
   SortBig:  8 wallclock secs ( 7.54 usr +  0.00 sys =3D  7.54
CPU) @ 132625.99/s (n=3D1000000)


Benchmark: timing 1000000 iterations of MySortBig2,
SortBig2...(in this case input array was reverse ordered)

Slow...    1000+wallclock seconds...




-- =

thanks
-Tr=DCtH


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

Date: Thu, 12 Jun 2003 21:34:37 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: Which value is tha largest(benchmarked)
Message-Id: <Xns9398B2CDBE238dkwwashere@216.168.3.30>

TruthXayer <TruthXayer@yahoo.com> wrote:

> "David K. Wall" wrote:
>> sub max {
>>     my $max = shift;
>>     $max = $_ > $max ? $_ : $max foreach @_;
>>     return $max;
>> }
>> 
>> my $maximum = max($one, $two, $tre, $for);
> 
> 
> Interessting , I benchmarked two cases:
>      -> small unsorted arrays
>      -> big sorted arrays     (got sorted much quicker with right
> ordering)
>      -> big reverse sorted  (got awfully slow)
> 
> So keep those arrays sorted as much as possible to improve
> performance else you are done 
> for!

What does sorting have to do with the code I posted?


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

Date: Thu, 12 Jun 2003 15:00:55 -0700
From: TruthXayer <TruthXayer@yahoo.com>
To: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: Which value is tha largest(benchmarked)
Message-Id: <3EE8F817.62C1E734@yahoo.com>

"David K. Wall" wrote:
> =

> TruthXayer <TruthXayer@yahoo.com> wrote:
> =

> > "David K. Wall" wrote:
> >> sub max {
> >>     my $max =3D shift;
> >>     $max =3D $_ > $max ? $_ : $max foreach @_;
> >>     return $max;
> >> }
> >>
> >> my $maximum =3D max($one, $two, $tre, $for);
> >
> >
> > Interessting , I benchmarked two cases:
> >      -> small unsorted arrays
> >      -> big sorted arrays     (got sorted much quicker with right
> > ordering)
> >      -> big reverse sorted  (got awfully slow)
> >
> > So keep those arrays sorted as much as possible to improve
> > performance else you are done
> > for!
> =

> What does sorting have to do with the code I posted?

	Your code is faster than other examples posted here.
	But its speed is also affected depending on whether
	input is sorted or not , check it out with Benchmark.	=


-- =

thanks
-Tr=DCth


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

Date: Thu, 12 Jun 2003 12:59:37 -0500
From: Michael Hill <hillmw@ram.lmtas.lmco.com>
Subject: Which value is tha largest
Message-Id: <3EE8BF89.7437D727@ram.lmtas.lmco.com>

Without having to have a nexted if else syntax does anyone know an easy
way to determine which value is the largest?

$one = 3;
$two = 5;
$tre =  1;
$for = 10;

$largest = (                                 );

Mike



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

Date: Thu, 12 Jun 2003 18:28:09 GMT
From: "Ian.H [dS]" <ian@WINDOZEdigiserv.net>
Subject: Re: Which value is tha largest
Message-Id: <gehhevc3que3inig8il2bhlved964un3cl@4ax.com>
Keywords: Remove WINDOZE to reply

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

Whilst lounging around on Thu, 12 Jun 2003 12:59:37 -0500, Michael
Hill <hillmw@ram.lmtas.lmco.com> amazingly managed to produce the
following with their Etch-A-Sketch:

> Without having to have a nexted if else syntax does anyone know an
> easy way to determine which value is the largest?
> 
> $one = 3;
> $two = 5;
> $tre =  1;
> $for = 10;
> 
> $largest = (                                 );
> 
> Mike


my @largest = (3, 5, 1, 10);
@largest = sort {$b <=> $a} @largest;

my $big_one = $largest[0];


There maybe a better way, but I used this method a week or so ago for
a simple script =)


HTH.



Regards,

  Ian

-----BEGIN xxx SIGNATURE-----
Version: PGP 8.0

iQA/AwUBPujGOGfqtj251CDhEQJLhwCg57JysKEIQa8mdhpGgrjGr9mtWuAAn1ho
CyRlJyh+h6J1tj2Vn9AaX+Bk
=e7kL
-----END PGP SIGNATURE-----

-- 
Ian.H  [Design & Development]
digiServ Network - Web solutions
www.digiserv.net  |  irc.digiserv.net  |  forum.digiserv.net
Programming, Web design, development & hosting.


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

Date: Thu, 12 Jun 2003 13:39:54 -0500
From: tadmc@augustmail.com (Tad McClellan)
Subject: Re: Which value is tha largest
Message-Id: <slrnbehi7q.2vk.tadmc@magna.augustmail.com>

Michael Hill <hillmw@ram.lmtas.lmco.com> wrote:
> Without having to have a nexted if else syntax does anyone know an easy
> way to determine which value is the largest?
> 
> $one = 3;
> $two = 5;
> $tre =  1;
> $for = 10;
> 
> $largest = (                                 );


Inefficient, but you didn't say anything about efficiency :-)


   my($largest) = sort { $b <=> $a } $one, $two, $tre, $for;


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: Thu, 12 Jun 2003 18:58:35 -0000
From: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: Which value is tha largest
Message-Id: <Xns939898593301Adkwwashere@216.168.3.30>

Michael Hill <hillmw@ram.lmtas.lmco.com> wrote:

> Without having to have a nexted if else syntax does anyone know an
> easy way to determine which value is the largest?
> 
> $one = 3;
> $two = 5;
> $tre =  1;
> $for = 10;
> 

sub max {
    my $max = shift;
    $max = $_ > $max ? $_ : $max foreach @_;
    return $max;
}

my $maximum = max($one, $two, $tre, $for);



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

Date: Thu, 12 Jun 2003 16:56:19 -0500
From: Michael Hill <hillmw@ram.lmtas.lmco.com>
To: "David K. Wall" <usenet@dwall.fastmail.fm>
Subject: Re: Which value is tha largest
Message-Id: <3EE8F703.B214ED36@ram.lmtas.lmco.com>

Thanks alot ... I like this one .... works good in my pgm.

Mike

"David K. Wall" wrote:

> Michael Hill <hillmw@ram.lmtas.lmco.com> wrote:
>
> > Without having to have a nexted if else syntax does anyone know an
> > easy way to determine which value is the largest?
> >
> > $one = 3;
> > $two = 5;
> > $tre =  1;
> > $for = 10;
> >
>
> sub max {
>     my $max = shift;
>     $max = $_ > $max ? $_ : $max foreach @_;
>     return $max;
> }
>
> my $maximum = max($one, $two, $tre, $for);



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

Date: 12 Jun 2003 09:31:47 -0700
From: i5513@hotmail.com (i5513)
Subject: why does perl do it?
Message-Id: <a657ec02.0306120831.350d8858@posting.google.com>

Hi, I have a dude:

use strict;
my $i = 0;

my @array = ([1,3], [2,4]);

&try_it (\@array, \$i);
sub try_it
{
  my $list = $_[0];
  my $i = $_[1];

  while ($list->[$$i][0]  <= 5 and $$i <= $#$list)
  {
    $$i++;
  }
}

why is it a infinite loop? why @$list changes, adding empty array each
loop?

I try with:

use strict;

my @array = ([1,3], [2,4]);

&try_it (\@array);
sub try_it
{
  my $list = $_[0];
  my $i = 0;

  while ($list->[$i][0]  <= 5 and $i <= $#$list)
  {
    $i++;
  }
}
and it worked fine.

Too worked fine if loop while condition is changed by order ($$i
<=$#$list and $list->[$$i][0] <= 5)

I think, only when perl get $list->[$$i][0], perl modifies it?
I know perl is "perezoso" (left first)

Thanks, I'm intrigate.

Sorry for my poor english


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

Date: 12 Jun 2003 17:02:03 GMT
From: Nicholas Dronen <ndronen@io.frii.com>
Subject: Re: why does perl do it?
Message-Id: <3ee8b20b$0$206$75868355@news.frii.net>

i5513 <i5513@hotmail.com> wrote:
i> Hi, I have a dude:


You should use warnings as well:

	#!/usr/bin/perl -w

i> use strict;
i> my $i = 0;

i> my @array = ([1,3], [2,4]);

i> &try_it (\@array, \$i);
i> sub try_it
i> {
i>   my $list = $_[0];
i>   my $i = $_[1];

i>   while ($list->[$$i][0]  <= 5 and $$i <= $#$list)
i>   {
i>     $$i++;
i>   }
i> }

i> why is it a infinite loop? why @$list changes, adding empty array each
i> loop?

I believe it's called autovivification.  See perlref.

Test the number of elements in the array before dereferencing it:

	sub try_it {
		my $list = $_[0];
		my $i = $_[1];
		while ($$i <= $#$list and $list->[$$i][0] <= 5) {
			$$i++;
		}
	}

You could also use 'defined' instead of testing the array size, if
you know the array will never be sparse:

	sub try_it {
		my $list = $_[0];
		my $i = $_[1];
		while (defined $list->[$$i] and $list->[$$i][0] <= 5) {
			$$i++;
		}
	}

Regards,

Nicholas

-- 
"Why shouldn't I top-post?"    http://www.aglami.com/tpfaq.html
"Meanings are another story."  http://www.ifas.org/wa/glossolalia.html


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

Date: Thu, 12 Jun 2003 17:05:40 GMT
From: Michael Budash <mbudash@sonic.net>
Subject: Re: why does perl do it?
Message-Id: <mbudash-040D9A.10054012062003@typhoon.sonic.net>

In article <3ee8b20b$0$206$75868355@news.frii.net>,
 Nicholas Dronen <ndronen@io.frii.com> wrote:

> i5513 <i5513@hotmail.com> wrote:
> i> Hi, I have a dude:
> 
> 
> You should use warnings as well:
> 
> 	#!/usr/bin/perl -w
> 
> i> use strict;
> i> my $i = 0;
> 
> i> my @array = ([1,3], [2,4]);
> 
> i> &try_it (\@array, \$i);
> i> sub try_it
> i> {
> i>   my $list = $_[0];
> i>   my $i = $_[1];
> 
> i>   while ($list->[$$i][0]  <= 5 and $$i <= $#$list)
> i>   {
> i>     $$i++;
> i>   }
> i> }
> 
> i> why is it a infinite loop? why @$list changes, adding empty array each
> i> loop?
> 
> I believe it's called autovivification.  See perlref.

or:

http://tlc.perlarchive.com/articles/perl/ug0002.shtml


-- 
Michael Budash


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

Date: 12 Jun 2003 11:38:44 -0800
From: yf110@vtn1.victoria.tc.ca (Malcolm Dew-Jones)
Subject: Re: why does perl do it?
Message-Id: <3ee8c8b4@news.victoria.tc.ca>

i5513 (i5513@hotmail.com) wrote:
: Hi, I have a dude:

: use strict;
: my $i = 0;

: my @array = ([1,3], [2,4]);

: &try_it (\@array, \$i);
: sub try_it
: {
:   my $list = $_[0];
:   my $i = $_[1];

:   while ($list->[$$i][0]  <= 5 and $$i <= $#$list)
:   {
:     $$i++;
:   }
: }

As others have said, autovivification.

Perl cannot compare the value of

	$list->[$$i][0] 

unless it can look it up.  To look it up it needs to first create the
entry for list->[$$i].  That makes the list longer, so $#$list keeps
increasing, so you never get to the end.

That is why you have to check the length first.


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

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


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

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

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

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


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


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