Can't get past 'use strict' :(

K

Karlo Lozovina

Here I go again: the following code:

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

produces this error:

syntax error at ./backup.pl line 24, near "$dir_list["

when i use "use strict". I read Programming Perl's chapter on strict,
but I can't get this thing working :(.
 
L

Leendert Bottelberghs

Op Wed, 15 Dec 2004 16:37:05 +0000, schreef Karlo Lozovina:
Here I go again: the following code:

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

produces this error:

syntax error at ./backup.pl line 24, near "$dir_list["

I'm not sure where the problem lies, but this can definitely be done more
efficient. Try:

chomp(my @line = <DIRLIST>);


-leendert bottelberghs
 
R

Robert Sedlacek

Karlo said:
while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line; ^^ ^ ^ ^
chomp($dir_list[$i]);
$i++;
}

Besides Leendert's much better hint:
I think you meant to define @dir_list out of this loop and just push new
elements to the array. You don't have to declare every element of that ary.
How about this (untested):

my @dir_list;
while(my $line = <DIRLIST>) {
chomp $line;
push @dir_list, $line;
}
my $i = @dir_list # if needed, but i don't think so..

g,
Robert
 
R

Richard Zilavec

Karlo Lozovina said:
Here I go again: the following code:

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
^^^^^^^^^^^ this is an array.... declare it above

chomp($dir_list[$i]);
$i++;
}

use strict;

my(@dir_list, $i);

while(my $line = <DIRLIST>) {
$dir_list[$i] = $line;
# Removed my();
chomp($dir_list[$i]);
$i++;
}

Richard Zilavec
 
C

Chris Mattern

Karlo said:
Here I go again: the following code:

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

produces this error:

syntax error at ./backup.pl line 24, near "$dir_list["

when i use "use strict". I read Programming Perl's chapter on strict,
but I can't get this thing working :(.
You can't declare an array variable like that. Nor can I see
*why* you'd want to declare an array variable like that. You
can get it to compile like this:

while (my $line = <DIRLIST>) {
my @dir_list;
$dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

This makes @dir_list disappear as soon as your while loop
ends. Is this what you want? If you want @dir_list to
be visible outside the list, then you want this:

my @dir_list;
while (my $line = <DIRLIST>) {
$dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
C

Chris Mattern

Leendert said:
Op Wed, 15 Dec 2004 16:37:05 +0000, schreef Karlo Lozovina:
Here I go again: the following code:

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

produces this error:

syntax error at ./backup.pl line 24, near "$dir_list["

I'm not sure where the problem lies, but this can definitely be done more
efficient. Try:

chomp(my @line = <DIRLIST>);
Well, yes, that's the best way, but let's go one step at a time...

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
G

Gunnar Hjalmarsson

Chris said:
You can get it to compile like this:

while (my $line = <DIRLIST>) {
my @dir_list;
$dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

This makes @dir_list disappear as soon as your while loop
ends.

Not only does it go out of scope, but it also contains only the last
line before doing so, since my() clears the array at each iteration.
 
K

Karlo Lozovina

my @dir_list;
while (my $line = <DIRLIST>) {
$dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

Yup, this is what I needed. Thank you very much for clearing this out.
I had one day to write a rather big Perl program today, but I never
programmed in Perl before. Althoug Perl is quite easy to catch up,
after a days work it is easy to mix things up :).
 
R

Robin

Karlo Lozovina said:
Here I go again: the following code:

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

produces this error:

syntax error at ./backup.pl line 24, near "$dir_list["

when i use "use strict". I read Programming Perl's chapter on strict,
but I can't get this thing working :(.

--
_______ Karlo Lozovina - Mosor
| | |.-----.-----.
| || _ | _ | Na osami blizu mora, dok se sunce zemlji smije
|__|_|__||_____|_____| Balun gledat, za njin letit...

actually, your code looks pretty tight.
-Robin
 
P

Peter Wyzl

: Here I go again: the following code:
:
: while(my $line = <DIRLIST>) {
: my $dir_list[$i] = $line;
: chomp($dir_list[$i]);
: $i++;
: }

It won't compile even without 'use strict'. You can't declare a slice of an
array like that, you need to declare the whole array and then assign to the
slices. However, a better, or more Perlish way to do that would be to take
advantage of some of the majic offered by the <> operator in array context
and read the whole file into the array in one step.

my @dir_list = <DIRLIST>;

You can then chomp() the array without having to chomp every element as you
read it...

chomp @dir_list;

If you find after doing the above that you still need to have $i (if you are
using it for something other than knowing which array element to assign the
line to and chomp, then assigning an array in scalar contect is the number
of elements in the array.

my $i = @dir_list;

Which reduces the above to:->

my @dir_list = <DIRLIST>;
chomp @dir_list;
my $i = @dir_list; # may not be needed..
 
K

Karlo Lozovina

It won't compile even without 'use strict'. You can't declare a
slice of an array like that, you need to declare the whole array
and then assign to the slices.

I know - looking at that code day after, and I seem like an idiot
:).
However, a better, or more Perlish way to do that would be to take
advantage of some of the majic offered by the <> operator in array
context and read the whole file into the array in one step.
my @dir_list = <DIRLIST>;

Wow! Didn't know Perl can do that. It rocks, thanks.
You can then chomp() the array without having to chomp every
element as you read it...
chomp @dir_list;

Didn't now I can do that, either :). Guess I'll have to finish
reading Programming Perl before beginning another Perl project.

Anyway, thanks a lot Peter, you've been most helpfull.
 
T

Tad McClellan

Robin said:
while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}


[ snip quoted .sig, you aren't supposed to do that you know ]

actually, your code looks pretty tight.


Nonsense alert!!


Doing explicit indexing when explicit indexing is not required
by the algorithm is "error prone" rather than "tight".

chomp(@dir_list = <DIRLIST>);

Does the same thing, and seems much closer to what a reasonable
person might call "tight".
 
A

A. Sinan Unur

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
....
syntax error at ./backup.pl line 24, near "$dir_list["
--
_______ Karlo Lozovina - Mosor
| | |.-----.-----.
| || _ | _ | Na osami blizu mora, dok se sunce zemlji smije
|__|_|__||_____|_____| Balun gledat, za njin letit...

actually, your code looks pretty tight.
-Robin

For various values of tight.

How can something that is reported by perl to be a syntax error be
considered "tight".

Oh, by the way, please take a look at the posting guidelines and definitely
do not quote people's signatures.

Sinan.
 
A

A. Sinan Unur

: Here I go again: the following code:
:
: while(my $line = <DIRLIST>) {
: my $dir_list[$i] = $line;
: chomp($dir_list[$i]);
: $i++;
: }

It won't compile even without 'use strict'. You can't declare a slice
of an array like that, you need to declare the whole array and then
assign to the slices.

Minor correction: $dir_list[$i] is not a slice but an array element.

Sinan
 
F

frans abels

Karlo Lozovina said:
Here I go again: the following code:

while(my $line = <DIRLIST>) {
my $dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}

produces this error:

syntax error at ./backup.pl line 24, near "$dir_list["
actually, your code looks pretty tight. -Robin

I don't know what that means,
but it has a syntax error...
my is not allowed with array-element assignments.
If you want to code this way and not the most efficient way
it should be:

my @dir_list;
while(my $line = <DIRLIST>) {
$dir_list[$i] = $line;
chomp($dir_list[$i]);
$i++;
}
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top