Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Prob with 'while' loop and subroutine calling

by cool (Scribe)
on Aug 18, 2006 at 21:12 UTC ( [id://568265]=note: print w/replies, xml ) Need Help??


in reply to Re: Prob with 'while' loop and subroutine calling
in thread Prob with 'while' loop and subroutine calling

Dear GrandFather,

First of all, thanks for taking so much pain for my problem.

In the code I posted, mistakenly graph()(line 126) left out while pasting.

The code I have pasted is working and giving results. But without 'use warning' I don feel confident. And also global variable of this code are interfering with rest of my code(actually this piece is part of a large work).

For whileloop case, I didn't get, what is prompting it to run over complete array. like it is not written as while (@x) or while ($fh)

So what I think is, this should run only once when it enters in the sub; when for the first time control enters the loop while($incEnergy > $minEnergy)What is prompting it to return? I know its something v silly that I am not getting, but pl guide me.

Can you pl explain me how this part of your code is working!! Just briefly, what is the concept behind or some link.. Also while compiling its showing this line

Global symbol "$x1" requires explicit package name at monk_graph.pl line 150.

if(@$x1) { $$max = $y->[-1] if $y->[-1] > $max; $$min = $y->[-1] if $y->[-1] < $min; } else { $$min = $$max = $y->[-1];}

Replies are listed 'Best First'.
Re^3: Prob with 'while' loop and subroutine calling
by runrig (Abbot) on Aug 18, 2006 at 21:41 UTC
    while ($incEnergy > $minEnergy)
    In that while loop there is:
    $incEnergy = $incEnergy+$scale;
    So presumably, incEnergy will grow until it is larger than minEnergy(update: incEnergy will shrink until its less than minEnergy), and the while loop will terminate eventually if $scale is greaterless than zero. But you have:
    $scale = ($minEnergy-$maxEnergy)/5;
    Which is greater? min or max? Normally I'd think the max would be greater, so scale would be negative, and you may have an infinite loop. If the code is correct, then it is confusing. Nevermind. But at least it answers your question on how the loop terminates.
    Global symbol "$x1" requires explicit package name at monk_graph.pl line 150.
    I assume that is supposed to be "@x", not "@$x1" in the code.
Re^3: Prob with 'while' loop and subroutine calling
by GrandFather (Saint) on Aug 18, 2006 at 22:31 UTC

    The while loop decrements $incEnergy from $maxEnergy downward in steps of $scale (which is negative) until $incEnergy is less than $minEnergy. It looks fine to me, although it may be a good place for a C type for loop:

    for ($incEnergy = $maxEnergy; $incEnergy > $minEnergy; $incEnergy += $ +scale)

    and omit the $incEnergy = $incEnergy+$scale; at the end of the loop. It looks like the while loop is generating points along an energy axis.

    if(@$x1) { should be if(@$x) {. The test checks to see if @$x contains any elements.

    The LoadFile sub takes a number of reference parameters. The first line in the sub:

    my ($filename, $x, $y, $min, $max) = @_;

    declares the variables and sets them to the parameter values. The last four variables are references. So $$max is the scalar variable that $max refers to. $x and $y are references to arrays and $y->[-1] is the last element of the array refered to by $y. So the line:

    $$max = $y->[-1] if $y->[-1] > $$max;

    means assign the last element of @$Y (the array that $Y refers to) to the scalar that $max refers to if the element is greater than the current maximum. (Note there was an error in this code. See update in previous post.)


    DWIM is Perl's answer to Gödel

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://568265]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2024-04-19 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found