f



Can't get past 'use strict' :(

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...
0
Karlo
12/15/2004 4:37:05 PM
comp.lang.perl.misc 33233 articles. 1 followers. brian (1246) is leader. Post Follow

15 Replies
547 Views

Similar Articles

[PageSpeed] 28

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

0
Leendert
12/15/2004 4:59:01 PM
Karlo Lozovina wrote:

> 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

-- 
http://www.dunkelheit.at/
thou shallst fear...

0
Robert
12/15/2004 5:05:10 PM
"Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message 
news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
> 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





0
Richard
12/15/2004 5:07:52 PM
Karlo Lozovina wrote:

> 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?"
0
Chris
12/15/2004 5:32:56 PM
Leendert Bottelberghs wrote:

> 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?"
0
Chris
12/15/2004 5:34:16 PM
Chris Mattern wrote:
> 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.

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
0
Gunnar
12/15/2004 5:48:43 PM
Chris Mattern <matternc@comcast.net> wrote in
news:lYGdnQXSRfXX6F3cRVn-hw@comcast.com: 

> 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 :).

-- 
 _______                                        Karlo Lozovina - Mosor
|   |   |.-----.-----.     
|       ||  _  |  _  |  Na osami blizu mora, dok se sunce zemlji smije
|__|_|__||_____|_____|                  Balun gledat, za njin letit...
0
Karlo
12/15/2004 6:20:04 PM
"Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message 
news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
> 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 


0
Robin
12/16/2004 9:31:12 AM
"Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message 
news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
: 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..

-- 
Wyzelli 


0
Peter
12/16/2004 11:44:06 AM
"Peter Wyzl" <wyzelli@yahoo.com> wrote in
news:ayewd.74895$K7.45851@news-server.bigpond.net.au: 

> 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.

-- 
Karlo Lozovina - Mosor
0
Karlo
12/16/2004 12:26:29 PM
Robin <webmaster@infusedlight.net> wrote:
> "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message 
> news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...


>> 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".


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas
0
Tad
12/16/2004 12:52:16 PM
"Robin" <webmaster @ infusedlight.net> wrote in news:WY6dne9XT_dMyFzcRVn-
gg@comcast.com:

> 
> "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message 
> news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...

>> 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.
0
A
12/16/2004 2:24:13 PM
"Peter Wyzl" <wyzelli@yahoo.com> wrote in
news:ayewd.74895$K7.45851@news-server.bigpond.net.au: 

> "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message 
> news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
>: 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
0
A
12/16/2004 2:26:03 PM
On Thu, 16 Dec 2004 10:31:12 +0100, Robin wrote:


> "Karlo Lozovina" <_karlo_@_mosor.net_> wrote in message
> news:Xns95C0B356F954Bkarlomosornet@130.133.1.4...
>> 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++;
}
0
frans
12/16/2004 3:57:41 PM

A. Sinan Unur wrote:

> "Robin" <webmaster @ infusedlight.net> wrote in news:WY6dne9XT_dMyFzcRVn-
> gg@comcast.com:
>>
>>actually, your code looks pretty tight.
> 
> For various values of tight.

The only one I think he means is is meaning #6 as defined in 
Merriam-Webster Online dictionary. :-)

0
Brian
12/16/2004 5:47:18 PM
Reply: