Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Can't get past 'use strict' :( (http://www.velocityreviews.com/forums/t889679-cant-get-past-use-strict.html)

Karlo Lozovina 12-15-2004 04:37 PM

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

Leendert Bottelberghs 12-15-2004 04:59 PM

Re: Can't get past 'use strict' :(
 
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


Robert Sedlacek 12-15-2004 05:05 PM

Re: Can't get past 'use strict' :(
 
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...


Richard Zilavec 12-15-2004 05:07 PM

Re: Can't get past 'use strict' :(
 

"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






Chris Mattern 12-15-2004 05:32 PM

Re: Can't get past 'use strict' :(
 
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?"

Chris Mattern 12-15-2004 05:34 PM

Re: Can't get past 'use strict' :(
 
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?"

Gunnar Hjalmarsson 12-15-2004 05:48 PM

Re: Can't get past 'use strict' :(
 
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

Karlo Lozovina 12-15-2004 06:20 PM

Re: Can't get past 'use strict' :(
 
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...

Robin 12-16-2004 09:31 AM

Re: Can't get past 'use strict' :(
 

"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



Peter Wyzl 12-16-2004 11:44 AM

Re: Can't get past 'use strict' :(
 
"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




All times are GMT. The time now is 07:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.