[32062] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 3326 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Mar 20 21:09:27 2011

Date: Sun, 20 Mar 2011 18:09:11 -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           Sun, 20 Mar 2011     Volume: 11 Number: 3326

Today's topics:
    Re: "getting" a website <tadmc@seesig.invalid>
    Re: "getting" a website <tadmc@seesig.invalid>
    Re: "getting" a website <jurgenex@hotmail.com>
    Re: "getting" a website <jurgenex@hotmail.com>
        attns newbs: ignore the parent post <ralph@happydays.com>
        Fetching SQL boolean values with Perl DBI? <dgstallard@gmail.com>
    Re: Fetching SQL boolean values with Perl DBI? <sherm.pendley@gmail.com>
    Re: Fetching SQL boolean values with Perl DBI? <uri@StemSystems.com>
    Re: Is there any point in using a reference to an objec <dgstallard@gmail.com>
    Re: Is there any point in using a reference to an objec <blgl@stacken.kth.se>
    Re: module for MSCRM? <justin.1102@purestblue.com>
    Re: recursive Pythagorian triples <nospam.gravitalsun@hotmail.com.nospam>
    Re: recursive Pythagorian triples <jurgenex@hotmail.com>
    Re: recursive Pythagorian triples <cartercc@gmail.com>
    Re: recursive Pythagorian triples <willem@toad.stack.nl>
    Re: recursive Pythagorian triples <jurgenex@hotmail.com>
    Re: recursive Pythagorian triples <hjp-usenet2@hjp.at>
    Re: recursive Pythagorian triples <jurgenex@hotmail.com>
    Re: recursive Pythagorian triples <xhoster@gmail.com>
    Re: recursive Pythagorian triples <jurgenex@hotmail.com>
        variable/subroutine visibility <ela@yantai.org>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Fri, 18 Mar 2011 07:00:04 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: "getting" a website
Message-Id: <slrnio6i0n.63n.tadmc@tadbox.sbcglobal.net>

Uno <Uno@example.invalid> wrote:
> On 03/10/2011 06:35 AM, Tad McClellan wrote:
                          ^^^^^^^^^^^^^^^^^^^
>
> [re-ordered, for thematic reasons}


I did NOT write that...


>> Uno<Uno@example.invalid>  wrote:
>
> > $_ is a variable.
> >
> > It cannot "return" anything.
> >
> > It does have a value though:
> >
> >      why is the value of $_ more than I wanted.
> >
> >
> > For us to answer that, we would need to know what you wanted!
> >
> > What did you want?
>
> What I want is a reasonable discussion of perl.


You said "more than I wanted".

You clearly wanted less but you didn't say what parts were "more"
than you wanted.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Fri, 18 Mar 2011 07:01:21 -0500
From: Tad McClellan <tadmc@seesig.invalid>
Subject: Re: "getting" a website
Message-Id: <slrnio6i34.63n.tadmc@tadbox.sbcglobal.net>

Uno <Uno@example.invalid> wrote:
> On 03/10/2011 04:24 AM, Jürgen Exner wrote:
>> perldoc perlretut
>
>
> $ perldoc perlretut
> You need to install the perl-doc package to use this program.


Then install that package.

Programming without access to the docs is just silly.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.liamg\100cm.j.dat/"
The above message is a Usenet post.
I don't recall having given anyone permission to use it on a Web site.


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

Date: Fri, 18 Mar 2011 05:54:26 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: "getting" a website
Message-Id: <q4l6o6p85fup2pr6pro5q6gurv1ifv2g6c@4ax.com>

Uno <Uno@example.invalid> wrote:
>On 03/10/2011 04:24 AM, Jürgen Exner wrote:
>> perldoc perlretut
>
>
>$ perldoc perlretut
>You need to install the perl-doc package to use this program.

Well, then why don't you tell the adminstrator of your system that he
didn't finish his job?

jue



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

Date: Fri, 18 Mar 2011 06:09:34 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: "getting" a website
Message-Id: <ajl6o6ps2hlgln6br204bn1eu483gj72kl@4ax.com>

[nested quoted incorrectly marked by Uno]
Uno <Uno@example.invalid> wrote:
>On 03/10/2011 06:35 AM, Tad McClellan wrote:
>> Uno<Uno@example.invalid>  wrote:
>
> > $_ is a variable.
> >
> > It cannot "return" anything.
> >
> > It does have a value though:
> >
> >      why is the value of $_ more than I wanted.
> >
> > For us to answer that, we would need to know what you wanted!
> >
> > What did you want?
>
>What I want is a reasonable discussion of perl.  

Excuse me, but that reply doesn't answer the question. 

You wrote "the value of $_ is more than I wanted". So you were expecting
it to contain some value but it contained more than this value. We
cannot read your mind. Unless you tell us what you were expecting there
is no way for us to find out what this difference is and even less to
tell you why there is this difference.

I don't believe you honestly expected the variable to contain a
reasonable discussion of perl? Because your answer does make it sound
like that.

>My notion of 
>"reasonable" might be another's notion of "highly idiomatic and usually OT."

Maybe, maybe not. The problem here is a very basic one: unless you tell
us what the expected outcome was and what the actual outcome was and how
those two are different we cannot tell you why they are different
because we don't know what that difference is.

jue


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

Date: Fri, 18 Mar 2011 10:01:18 -0400
From: Ralph Malph <ralph@happydays.com>
Subject: attns newbs: ignore the parent post
Message-Id: <ac010$4d8365af$ce534406$15647@news.eurofeeds.com>

Newbs: Ignore the parent post.
It gets auto-posted by mistake.
The guy who set it up to auto-post was thrown out of
his mother's basement to go live at the ymca. As such he
no longer has access to the machine which runs the auto-post script.
We are all hoping that perhaps one day the mother unplugs
the computer during her vacuuming and puts an end to this.


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

Date: Sun, 20 Mar 2011 16:56:13 -0700 (PDT)
From: Dave Stallard <dgstallard@gmail.com>
Subject: Fetching SQL boolean values with Perl DBI?
Message-Id: <f817f0fa-e88b-49ae-9635-2faac09b288f@x18g2000yqe.googlegroups.com>

I'm working with a MySQL database via Perl DBI, and want to fetch the
value of an attribute of SQL boolean type.  I would naturally imagine
this could come back as '0' or '1', but what comes back is instead
something weird that prints as '^@' or '^A' depending upon whether
it's true or false (I forget which is which).    This thing, whatever
it is, appears to be a scalar, not a reference, but I don't know how
to refer to it in Perl.    I'm using fetchrow_hashref, but I don't
think it matters.

Does anybody know what's going on here?   (Apologies if there is a
more appropriate Perl DBI newsgroup which I missed).

thanks,
Dave


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

Date: Sun, 20 Mar 2011 20:13:47 -0400
From: Sherm Pendley <sherm.pendley@gmail.com>
Subject: Re: Fetching SQL boolean values with Perl DBI?
Message-Id: <m2k4ftjpok.fsf@sherm.shermpendley.com>

Dave Stallard <dgstallard@gmail.com> writes:

> I'm working with a MySQL database via Perl DBI, and want to fetch the
> value of an attribute of SQL boolean type.  I would naturally imagine
> this could come back as '0' or '1'

According to the MySQL docs, that's exactly what should come back:

  <http://dev.mysql.com/doc/refman/5.5/en/numeric-type-overview.html>

Summary: Bool is an alias for tinyint, zero is false, nonzero is true.

> but what comes back is instead
> something weird that prints as '^@' or '^A' depending upon whether
> it's true or false (I forget which is which).

Have you tried the same query in a MySQL shell? How are you printing
the value?

> This thing, whatever it is, appears to be a scalar, not a reference

Appears to be a scalar, or is one? If there's any doubt, use Perl's
ref() function to find out for sure.

sherm--

-- 
Sherm Pendley
                                   <http://camelbones.sourceforge.net>
Cocoa Developer


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

Date: Sun, 20 Mar 2011 20:25:36 -0400
From: "Uri Guttman" <uri@StemSystems.com>
Subject: Re: Fetching SQL boolean values with Perl DBI?
Message-Id: <87mxkpjp4v.fsf@quad.sysarch.com>

>>>>> "DS" == Dave Stallard <dgstallard@gmail.com> writes:

  DS> I'm working with a MySQL database via Perl DBI, and want to fetch the
  DS> value of an attribute of SQL boolean type.  I would naturally imagine
  DS> this could come back as '0' or '1', but what comes back is instead
  DS> something weird that prints as '^@' or '^A' depending upon whether
  DS> it's true or false (I forget which is which).    This thing, whatever
  DS> it is, appears to be a scalar, not a reference, but I don't know how
  DS> to refer to it in Perl.    I'm using fetchrow_hashref, but I don't
  DS> think it matters.

you are getting back binary values for 0 and 1 in a char sized int. you
can unpack them into perl integers and use them as such. you could also
convert them other ways which i won't go into here.

uri

-- 
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------


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

Date: Sun, 20 Mar 2011 07:44:04 -0700 (PDT)
From: Dave Stallard <dgstallard@gmail.com>
Subject: Re: Is there any point in using a reference to an object?
Message-Id: <dac7255c-8150-4f44-97b3-cba7461aeb94@t16g2000vbi.googlegroups.com>

On Mar 17, 2:33=A0pm, Bo Lindbergh <b...@stacken.kth.se> wrote:
> In article <slrnio4df5.3hc.ta...@tadbox.sbcglobal.net>,
> =A0Tad McClellan <ta...@seesig.invalid> wrote:
>
> > It is due to MJD:
>
> > =A0 =A0 BECAUSE YOU CAN'T JUST MAKE SHIT UP AND EXPECT THE COMPUTER TO
> > =A0 =A0 MAGICALLY KNOW WHAT YOU MEAN, RETARDO!
>
> The problem with this is that Perl has so much dwimmery in it
> that you often _can_ make shit up successfully.
>
> /Bo Lindbergh

Holy Shit, you used to use Interlisp!!!!

Dave


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

Date: Sun, 20 Mar 2011 19:27:02 +0100
From: Bo Lindbergh <blgl@stacken.kth.se>
Subject: Re: Is there any point in using a reference to an object?
Message-Id: <im5gtj$7oj$1@speranza.aioe.org>

In article <dac7255c-8150-4f44-97b3-cba7461aeb94@t16g2000vbi.googlegroups.com>,
 Dave Stallard <dgstallard@gmail.com> wrote:

> On Mar 17, 2:33 pm, Bo Lindbergh <b...@stacken.kth.se> wrote:
> > In article <slrnio4df5.3hc.ta...@tadbox.sbcglobal.net>,
> >  Tad McClellan <ta...@seesig.invalid> wrote:
> >
> > > It is due to MJD:
> >
> > >     BECAUSE YOU CAN'T JUST MAKE SHIT UP AND EXPECT THE COMPUTER TO
> > >     MAGICALLY KNOW WHAT YOU MEAN, RETARDO!
> >
> > The problem with this is that Perl has so much dwimmery in it
> > that you often _can_ make shit up successfully.
> 
> Holy Shit, you used to use Interlisp!!!!

No, but I have the perl source at hand.

$ grep -i -r dwim perl | wc
      32     277    2824


/Bo Lindbergh


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

Date: Fri, 18 Mar 2011 12:57:40 +0000
From: Justin C <justin.1102@purestblue.com>
Subject: Re: module for MSCRM?
Message-Id: <4f1c58-efl.ln1@zem.masonsmusic.co.uk>

On 2011-03-16, hymie! <hymie@lactose.homelinux.net> wrote:
> Greetings.
>
> My company has recently switched to MS CRM

Abandon ship!! 

Sorry, seriously though, I don't know. Doesn't the CRM drive SQL,
surely there's a database underneath it somewhere? Wouldn't that be 
what you want to talk to?

   Justin.

-- 
Justin C, by the sea.


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

Date: Fri, 18 Mar 2011 15:09:11 +0200
From: "George Mpouras" <nospam.gravitalsun@hotmail.com.nospam>
Subject: Re: recursive Pythagorian triples
Message-Id: <ilvlgj$2li7$1@news.ntua.gr>


"bugbear" <bugbear@trim_papermule.co.uk_trim> wrote in message 
news:ZtednZg1KtV9vx7QnZ2dnUVZ8mOdnZ2d@brightview.co.uk...
> ccc31807 wrote:
>
>>
>> If you think it's 'incoherent babble' you are free to ignore the
>> thread. If you think you can code a recursive Pythagorean triple
>> finder in Perl, you are welcome to try.
>
> Well it would be trivial (and opintless) to express
> the nested loops in a recursive form. Would that satisfy
> your request>
>
>   BugBear

# I have it !

&MySelf;

sub MySelf {
print ++$_[0]."\n";
4==$_[0] ? exit : &MySelf
}




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

Date: Fri, 18 Mar 2011 06:26:46 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: recursive Pythagorian triples
Message-Id: <3hm6o6t5i50htun9drcu9fm9d1nlp09429@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:
>Yes, it's a list comprehension. My question was, can you write a
>Pythagorean triple function in Perl using recursion?

Can you write it using a loop? Then yes, you can write it using
recursion because any loop can trivially be transformed into a recursion
by very basic program transformation.

	WHILE (cond) {
		body}

becomes

	do_loop
	
	FUNC do_loop {
		if ( cond) {
			body
			do_loop
		}
	}

Oh, and before you ask: yes, any other loop form can be transformed into
a while loop.

What do students learn in computer science today?

>I understand recursion as solving a problem by calling either directly
>or indirectly the original function with a smaller set of the original
>data until it returns on some base case. 

Not necessarily a smaller set of data but there must exist some function
from data to natural numbers where the value will decrease with each
recursive call. This is called the termination function. It doesn't have
to reach 0, but it must become smaller. Otherwise you cannot guarantee
that the recursion will terminate.

jue


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

Date: Fri, 18 Mar 2011 06:42:48 -0700 (PDT)
From: ccc31807 <cartercc@gmail.com>
Subject: Re: recursive Pythagorian triples
Message-Id: <232a8d32-3143-4fc3-8146-9e60ee52da2c@r4g2000prm.googlegroups.com>

On Mar 18, 5:16=A0am, bugbear <bugbear@trim_papermule.co.uk_trim> wrote:
> Well it would be trivial (and opintless) to express
> the nested loops in a recursive form. Would that satisfy
> your request>

First of all, I'm not very good at recursion, so this is a learning
exercise for me. My apologies if this offends anyone.

The first thing in writing a recursive function is to identify a base
case. For example, to find the length of a list, you start with the
base case of an empty list, the length of which is zero. Using Erlang
simply for the sake of syntax, you would write it like this:
list_len([]) -> 0;

The second thing is to identify the next smaller case after the
original argument, which is the original list minus one. The entire
function looks like this:
list_len([]) -> 0;
list_len([H|T]) -> 1 + list_len(T).

The fibonacci function is a double recursion in that you recurse down
two paths. This is a trivial implementation, without an accumulator,
and is not efficient, but it shows the double recursion:
fib(0) -> 1;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).

Now, my question is, how do you write a triple recursive IN PERL, for
example, to find Pythagorean triples?

CC.


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

Date: Fri, 18 Mar 2011 14:20:58 +0000 (UTC)
From: Willem <willem@toad.stack.nl>
Subject: Re: recursive Pythagorian triples
Message-Id: <slrnio6qia.e1c.willem@toad.stack.nl>

ccc31807 wrote:
) Now, my question is, how do you write a triple recursive IN PERL, for
) example, to find Pythagorean triples?

As a counter question:
How would you write a *RECURSIVE* pythagorean triples in Erlang ?

Pythagorean triples is simply not a problem that is suitable for recursion.


SaSW, Willem
-- 
Disclaimer: I am in no way responsible for any of the statements
            made in the above text. For all I know I might be
            drugged or something..
            No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT


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

Date: Fri, 18 Mar 2011 19:08:47 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: recursive Pythagorian triples
Message-Id: <mu28o6tsv5ns6il2rsn595qpbmjo9q9kcf@4ax.com>

Willem <willem@toad.stack.nl> wrote:
>ccc31807 wrote:
>) Now, my question is, how do you write a triple recursive IN PERL, for
>) example, to find Pythagorean triples?

I have no idea what a triple recursive is supposed to be ...

>Pythagorean triples is simply not a problem that is suitable for recursion.

Really? This works for me:

use strict; use warnings;
sub is_square{
    my ($x) = @_;
    my $sq = sprintf("%u", sqrt($x));
    if (($sq * $sq) == $x) {
	return $sq;
    } else {
	return 0;
    }
}

sub doit{
    my ($a, $b) = @_;
    exit if $b > 1000; #artifical termination of endless recursion
    if ($a <= $b) {
	my $c = is_square($a*$a + $b*$b);
	print "($a, $b, $c)\n" if $c;
	doit($a+1, $b);
    } else {
	doit(1, $b+1);
    }
}

doit(1,1);

The only tricky part was to find a function that tests if a number is
the square of natural number. I am sure there are better ways but this
works.
And because this whole recursion is a tail recursion you can trivially
rewrite it into using only global variables, thus probably saving quite
some memory:

my ($a, $b, $c);
sub doit{
    exit if $b > 1000; #artifical termination of endless recursion
    if ($a <= $b) {
	$c = is_square($a*$a + $b*$b);
	print "($a, $b, $c)\n" if $c;
	$a++;
	doit();
    } else {
	$b++; $a=1;
	doit();
    }
}

$a = $b = 1;
doit();



jue


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

Date: Sat, 19 Mar 2011 15:21:38 +0100
From: "Peter J. Holzer" <hjp-usenet2@hjp.at>
Subject: Re: recursive Pythagorian triples
Message-Id: <slrnio9evi.9kv.hjp-usenet2@hrunkner.hjp.at>

On 2011-03-18 02:44, ccc31807 <cartercc@gmail.com> wrote:
> On Mar 16, 8:02 pm, Xho Jingleheimerschmidt <xhos...@gmail.com> wrote:
>> The above non-recursive example is what you gave to explain why you
>> wanted a recursive version in Perl.  If we are not to assume that you
>> are just babbling incoherently, then the most reasonable assumption is
>> that you for some reason thought the above was recursive.  Otherwise
>> using it as an example is just bizarre.
>
> You missed the point. I didn't mention the Erlang script to begin with
> because it wasn't relevant to the question. I only mentioned it in the
> context of providing an explanation of the original post.

Unfortunately you didn't explain anything. Maybe you meant to write "I
came upon this non-recursive Erlang program, and I wondered why it
wasn't recursive, since Erlang programmers are normally very fond of
recursion. So I tried to come up with a recursive solution myself and
failed". But you didn't write that. Instead you just posted the script,
which left us to decide between the two alternatives Xho mentioned.

> Perl doesn't have list comprehensions, as far as I know,

I don't know Erlang, but if the explanation at
http://en.wikibooks.org/wiki/Erlang_Programming/List_Comprehensions is
correct, the Perl equivalent is "map". (And someone already posted a
Perl program using map)

> so you are correct in that asking for a Perl script for finding
> Pythagoriean triples would indeed be bizarre. But that wasn't the
> point.

Huh? How is asking for a Perl script for finding Pythagorean triples
bizarre? That's a very simple problem and you provided an adequate
solution in the very first posting in thread. It can be solved in any
turing-complete programming language and probably one of the most
popular exercises in introductory programming courses.

Xho found it bizarre that you provided a non-recursive Erlang program as
the reason for wanting to write a recursive Perl program. There are
programming languages in which the most natural way to implement an
algorithm is recursive. Perl isn't one of them.

> The point was -- Can you find Pythagorean triples in Perl using
> recursion?

The answer is of course: Yes, trivially.

> If you want a generalization, how do you solve a problem
> that calls for triple recursion. PJH provided one solution that I
> didn't like, but the next day I spend almost the entire day trying to
> better it, and I failed. I'm not convinced that it can't be done in a
> simpler way, but I don't think I can do it.

The algorithm is so simple that there aren't many possible variations,
so I doubt you'll find a simpler recursive implementation of the same
algorithm. 

You can of course change the algorithm (as George and Jürgen did) and
that may be faster, but not necessarily simpler.


> And please don't point out that Erlang is derived from Prolog, not
> Lisp. I know that. I also know that Lisp has iterative constructs, but
> Erlang doesn't. The tie to Erlang was that the particular example used
> list comprehensions, and I wondered how you could use Perl to do the
> same kind of job, and recursion was as close as i could come to
> Erlang's list comprehensions.

Ah, that explains it. Well, nobody understood that because nobody thinks
that recursion the closest thing Perl has to list comprehension. Map
seems to be almost equivalent (given the rather large differences
between Perl and Erlang in general), and even a plain loop is closer,
IMHO.

	hp



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

Date: Sat, 19 Mar 2011 09:30:27 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: recursive Pythagorian triples
Message-Id: <6hl9o6hcuj6luilefjs2goa8bh15upajk1@4ax.com>

ccc31807 <cartercc@gmail.com> wrote:

Going back to the root of this mega-thread

>Pythagorian triples are integers that form the sides of a right
>triangle, e.g., 3, 4, 5. I've posted an iterative version below for
>all triples less than 100. 
>
>#! perl
>use strict;
>use warnings;
>print "Pythagorian triples\n\n";
>for(my $i = 1; $i < 101; $i++) {

Usually written and easier to read as 
	foreach my $i (1..100) { 

>{
>  for(my $j = 1; $j < 101; $j++) {
>    for(my $k = 1; $k < 101; $k++) {
>      print "$i - $j - $k\n" if ($i * $i + $j * $j) == ($k * $k);
>    }
>  }
>}
>exit(0);

>I'm having trouble coming up with a
>recursive version. Any ideas?

Trivial program transformation:

print "Pythagorian triples\n\n";
my ($i, $j, $k) = (1,1,1);
sub phyt{
    print "$i - $j - $k\n" if ($i * $i + $j * $j) == ($k * $k);
    if ($k < 101) {
	if ($j <101) {
	    if ($i <101) {
		$i++; phyt();
	    } else {
		$i=1; $j++; phyt();
	    }
	} else {
		$i=1; $j=1; $k++; phyt();
	}
    }
}

phyt();
exit 0

How to get there? First transform each for loop into a while loop. Then
use the well-known program transformation from while loops into tail
recursion. And then a little bit of cleanup. Trivial, really.

jue


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

Date: Fri, 18 Mar 2011 18:29:14 -0700
From: Xho Jingleheimerschmidt <xhoster@gmail.com>
Subject: Re: recursive Pythagorian triples
Message-Id: <4d8534ea$0$20508$ed362ca5@nr5-q3a.newsreader.com>

ccc31807 wrote:
> If you think it's 'incoherent babble' you are free to ignore the
> thread. If you think you can code a recursive Pythagorean triple
> finder in Perl, you are welcome to try.

sub crap {
   return crap($_[0]-1) if ($_[0]>0);
   return non_recursive_code_that_does_the_job();
}

Xho


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

Date: Sat, 19 Mar 2011 17:26:44 -0700
From: Jürgen Exner <jurgenex@hotmail.com>
Subject: Re: recursive Pythagorian triples
Message-Id: <o4iao69u0fsvvvtfdbo3ejf9o2upfn0vtm@4ax.com>

Xho Jingleheimerschmidt <xhoster@gmail.com> wrote:
>ccc31807 wrote:
>> If you think it's 'incoherent babble' you are free to ignore the
>> thread. If you think you can code a recursive Pythagorean triple
>> finder in Perl, you are welcome to try.

In the past 2 days I posted 3 different, exclusively recursive versions.
Not to mention the generic program transformation rule which allows you
to transform any loop into a recursion. This is quite basic CS stuff. 

Does that satisfy your requirements?

jue


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

Date: Mon, 21 Mar 2011 09:53:06 -0700
From: "ela" <ela@yantai.org>
Subject: variable/subroutine visibility
Message-Id: <im67f3$vqe$1@ijustice.itsc.cuhk.edu.hk>

Previously, George has kindly designed and revised codes for "voting" the 
most representative candidate from __DATA__ below. His initial design is to 
separate subroutines query and ReadData, but as a file of million rows is to 
read, I tried to embed subroutine query into ReadData but failed. There are 
two problems (relevant lines marked with *************).

1) While $a[0] is printed as '02', I don't know why it can't be passed into 
subroutine successfully
2) When the subroutine query is called inside subroutine ReadData, error 
message "Can't coerce array into hash" will prompt during execution. In the 
debug process, I hardcode it to "01" but in real case I wanna pass a[0] as 
argument. The strange point is, if I pass a[0], the error message won't 
prompt but still nothing is to print out.

my @col;
my %data;
$firsttime = 1;
$pre_a = "dummy";

ReadData();
  $_ = query($a[0],$thr);                                 # *************
  print "Field=$_->[0],Value=@{$_->[1]}\n";
  $_ = query('02',$thr);
  print "Field=$_->[0],Value=@{$_->[1]}\n";

sub query
{
for(my $i=$#{$data{$_[0]}}; $i>=0; $i--)
{
#foreach my $RANK (keys %{$data{$_[0]}->[$i]})
foreach my $RANK (sort {$b <=> $a} keys %{$data{$_[0]}->[$i]})

{
return [$col[$i], $data{$_[0]}->[$i]->{$RANK}] if $RANK >= $_[1]
}
}

['',[]]
}
sub ReadData
{
while (<DATA>) { chomp;
my @a = split /\s*\|\s*/, $_, -1;

if (-1 == $#col){push @col, @a[1..$#a] ;next}else {
if ($a[0] ne $pre_a) {
    $pre_a = $a[0];
    if ($firsttime != 1) {
        $_ = query("01",$thr);                                 # 
*************
        print "Field=$_->[0],Value=@{$_->[1]}\n";
    }
    $firsttime = 0;
}
}

unless (1+$#col==$#a) {warn "Skip line number $. \"$_\" because it have
".(1+$#a)." fields, while it should have ".(1+$#col)."\n";next}
$data{$a[0]}->[0]++;
for(my $i=1;$i<=$#a;$i++){$data{$a[0]}->[1]->[$i-1]->[0]->{$a[$i]}++}}

foreach my $id (keys %data)
{
foreach my $f ( @{$data{$id}->[1]} )
{
foreach my $v ( keys %{$f->[0]} )
{
push @{ $f->[1]->{int 100*( $f->[0]->{$v}/$data{$id}->[0])} }, $v
}

# remove unnecessary structures
$f = $f->[1]
}

# remove unnecessary structures
$data{$id} = $data{$id}->[1]
}

#use Data::Dumper; print Dumper(\%data);exit;
}

__DATA__
ID|B|C|D|E|F|G|H
01|3|7|9|3|4|2|3
01|3|7|9|3|4|2|2
01|3|7|9|5|8|6|6
01|3|7|9|3|4|2|3
02|4|7|9|3|4|2|1
02|4|7|9|3|4|2|2
02|4|7|9|3|4|2|3
02|4|7|9|3|4|2|3 




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

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:

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

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#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 V11 Issue 3326
***************************************


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