Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Can't get past 'use strict' :(

Reply
Thread Tools

Can't get past 'use strict' :(

 
 
Karlo Lozovina
Guest
Posts: n/a
 
      12-15-2004
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...
 
Reply With Quote
 
 
 
 
Leendert Bottelberghs
Guest
Posts: n/a
 
      12-15-2004
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

 
Reply With Quote
 
 
 
 
Robert Sedlacek
Guest
Posts: n/a
 
      12-15-2004
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...

 
Reply With Quote
 
Richard Zilavec
Guest
Posts: n/a
 
      12-15-2004

"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





 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      12-15-2004
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?"
 
Reply With Quote
 
Chris Mattern
Guest
Posts: n/a
 
      12-15-2004
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?"
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      12-15-2004
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
 
Reply With Quote
 
Karlo Lozovina
Guest
Posts: n/a
 
      12-15-2004
Chris Mattern <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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...
 
Reply With Quote
 
Robin
Guest
Posts: n/a
 
      12-16-2004

"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


 
Reply With Quote
 
Peter Wyzl
Guest
Posts: n/a
 
      12-16-2004
"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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot get past Object Reference Error danielle.m.manning@gmail.com ASP .Net 1 03-28-2006 11:43 PM
Cannot get past Object Reference Error danielle.m.manning@gmail.com ASP .Net 2 03-28-2006 11:17 PM
I get declaration error - When I past it on the next line and comments out the old, the syntax works... why?!? knutivar ASP .Net 2 12-09-2004 05:45 PM
How do you get rid of past newsgroups Monica Computer Support 7 04-24-2004 07:50 PM
Suggestion for HTML Editor to get past the crappy IDE one Lloyd Sheen ASP .Net 13 01-25-2004 10:42 PM



Advertisments