how to do something

L

Lex

Hi,

I've got two subroutines, one is saying 'do the other x times'. The other is
checking records from a database and prints the x latest records. However,
when a record matches something, it should be skipped. It all works,
however, what happens is that the results are x - 1. So in the example
underneath I normally get 3 results, except when $vergelijker = jep, than
the I only get 2 results, although I'd prefer 3. I tried this but it doesn't
work:

subroutine 1:

my $i = 2;

for (0 .. $i) {
&html_recordheadline (&array_to_hash($_, @results));
}


subroutine 2: (sub html_recordheadline )

if ($vergelijker ne "jep") {
print qq|
blablabla
|;
}

else { $i++; }

Any help is appreciated,
Lex
 
J

James Willmore

I've got two subroutines, one is saying 'do the other x times'. The
other is checking records from a database and prints the x latest
records. However, when a record matches something, it should be
skipped. It all works, however, what happens is that the results are
x - 1. So in the example underneath I normally get 3 results, except
when $vergelijker = jep, than the I only get 2 results, although I'd
prefer 3. I tried this but it doesn't work:

subroutine 1:

my $i = 2;

Where is ^^^ this line being declared? Inside subroutine 1 or
globally? If it's inside subroutine 1, then that's where I _think_
you're issue is - the variable's scope (or where it's life exists).
If $i is supposed to be accessed in _both_ subroutines, then declare
it _outside_ of the subroutines (aka globally).

Or did I miss what you were after?

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
Hand, n.: A singular instrument worn at the end of a human arm
<and commonly thrust into somebody's pocket. -- Ambrose Bierce,
"The Devil's Dictionary"
 
L

Lex

James Willmore said:
Where is ^^^ this line being declared? Inside subroutine 1 or
globally? If it's inside subroutine 1, then that's where I _think_
you're issue is - the variable's scope (or where it's life exists).
If $i is supposed to be accessed in _both_ subroutines, then declare
it _outside_ of the subroutines (aka globally).

Or did I miss what you were after?
No you didn't, and I tried your solution but alas, it wouldn't work. It's
probably my head that is thinking wrong and I should do it in a different
way.

for 0 to $i
do subroutine 2

subroutine 2:
check something and return contents. If you see something weird don't return
contents and raise $i with 1 (so the 'no content' is replaced by the next
record that should return contents)

Is this wrong? I mean, is there a better way to ge this done?

Thanks,

Lex
 
B

Ben Morrow

Lex said:
No you didn't, and I tried your solution but alas, it wouldn't work. It's
probably my head that is thinking wrong and I should do it in a different
way.

for 0 to $i
do subroutine 2

subroutine 2:
check something and return contents. If you see something weird don't return
contents and raise $i with 1 (so the 'no content' is replaced by the next
record that should return contents)

Is this wrong? I mean, is there a better way to ge this done?

# untested

for(my $j = 0; $j < $i; $j++) {
sub2 or $i++;
}

sub sub2 {
if(weird) {
return undef;
}
return $content;
}

__END__

That is not very Perl-y. If you explain more explicitly what you are
actually trying to do, maybe it can be improved.

Ben
 
J

James Willmore

No you didn't, and I tried your solution but alas, it wouldn't work.
It's probably my head that is thinking wrong and I should do it in a
different way.

for 0 to $i
do subroutine 2

subroutine 2:
check something and return contents. If you see something weird
don't return contents and raise $i with 1 (so the 'no content' is
replaced by the next record that should return contents)

Is this wrong? I mean, is there a better way to ge this done?

Hummm ... you want to increment $i ... from subroutine 2 ... when a
certain condition is met in subroutine 2, right? So, $i is _always_
going to change and not remain fixed, dependent upon what subroutine 2
does?

If this is correct, you have the makings of an infinite loop. So long
as subroutine 2 has the ability to change how many times subroutine 1
is executed, you have the potential of _never_ leaving subroutine 1.

Is there a way that you can combine the 2 subroutines? Nothing says
that you _have_ to have subroutines in your code.

HTH

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
You know it's going to be a bad day when you want to put on the
<clothes you wore home from the party and there aren't any.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top