infinite loop (newbie)

U

usaims

Hello:
I am writing a script to make sure that numbers in a file are all
identical. However the script below is an infinite loop, how do I break
out of it once it hits the last element of the array? I have searched
perldoc but didn't really see anything. Am I on the right track?

#!/usr/bin/perl -w

use strict;
use warnings;

my @array = (<DATA>);
chomp(@array);

for (my $x = $array[0];$x <= $array[8]; $array[0]++)

{
print "They are even.\n";
}


__END__
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
1207553365
 
G

Gunnar Hjalmarsson

usaims said:
I am writing a script to make sure that numbers in a file are all
identical. However the script below is an infinite loop, how do I break
out of it once it hits the last element of the array? I have searched
perldoc but didn't really see anything. Am I on the right track?

Err.. No, not as far as I can see.
for (my $x = $array[0];$x <= $array[8]; $array[0]++)

What did you expect that line to do? And why would you need to use a C
style for loop here?

As regards loops, please study

perldoc perlsyn

This is a possible solution to your problem:

my %hash = map { chomp; $_ => 1 } <DATA>;
print ( keys %hash > 1 ? 'not ' : '', "identical\n" );
 
P

Peter Wyzl

: Hello:
: I am writing a script to make sure that numbers in a file are all
: identical. However the script below is an infinite loop, how do I break
: out of it once it hits the last element of the array? I have searched
: perldoc but didn't really see anything. Am I on the right track?

It sounds like you are looking for 'last'

: #!/usr/bin/perl -w
:
: use strict;
: use warnings;
:
: my @array = (<DATA>);
: chomp(@array);
:
: for (my $x = $array[0];$x <= $array[8]; $array[0]++)

in Perl we would probably write:
for (@array){
# etc
}

The above is difficult to maintain, especially if the array changes size...

BTW, that is not an infinite loop....

: {
: print "They are even.\n";
: }


P
 
F

Fabian Pilkowski

* usaims said:
I am writing a script to make sure that numbers in a file are all
identical. However the script below is an infinite loop, how do I break
out of it once it hits the last element of the array? I have searched
perldoc but didn't really see anything. Am I on the right track?


my @array = (<DATA>);
chomp(@array);
for (my $x = $array[0];$x <= $array[8]; $array[0]++) {
print "They are even.\n";
}

Why do you want to iterate over all numbers between your first and last
line? For example, the more perlish »..«-operator does this job:

for my $x ( $array[0] .. $array[-1] ) {
print "They are even.\n";
}

Nevertheless, IIRC you asked a similar question a week ago. Why do you
don't use a hash since it was proposed there? I'll refresh your mind:

my %hash;
chomp, $hash{$_}++ while <DATA>;

for my $key ( keys %hash ) {
print "Number $key appears $hash{$key} times.\n";
}

In the thread last week David had used keys() to find out how many
different numbers are stored in this hash. What's the matter you don't
using it?

regards,
fabian
 
U

usaims

I'm new to Perl. Learning hashes will be my next objective, sorry for
the posts and thanks in advance.
 
P

Peter Wyzl

: Peter Wyzl wrote:
: > "usaims" wrote:
: >>
: >> for (my $x = $array[0];$x <= $array[8]; $array[0]++)
: >
: > BTW, that is not an infinite loop....
:
: Did you run it?

No, my bad... missed the declaration problem...

Infinite loop by accident (not really a syntax error)

P
 
T

Tad McClellan

Peter Wyzl said:
: Peter Wyzl wrote:
: > "usaims" wrote:
: >>
: >> for (my $x = $array[0];$x <= $array[8]; $array[0]++)
: >
: > BTW, that is not an infinite loop....
:
: Did you run it?

No, my bad... missed the declaration problem...


What declaration problem?

Infinite loop by accident (not really a syntax error)


Of course it is not a syntax error.

You can't get an infinite loop if the program won't even compile...

It is a _semantic_ error.


Syntax is trivial, so trivial that a machine can spot it.

It is the semantic errors that require examination by a sentient being.
 
A

A. Sinan Unur

I'm new to Perl.

Nothing wrong with that.
Learning hashes will be my next objective,

Nice of you to share that with us.
sorry for the posts and thanks in advance.

You can show your appreciation for the help given here by reading and
adhering to the posting guidelines for this group.

Sinan.
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top