Walt Mankowski on 26 Nov 2003 19:39:38 -0500


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Why can't an array element be used as the index var in foreach?


On Wed, Nov 26, 2003 at 07:01:19PM -0500, Stephen Gran wrote:
> steve:~$ perl -wle 'for ($c[0] = 0; $c[0] < 5; $c[0]++;){print $c[0];}'
> syntax error at -e line 1, near "++;"
> Execution of -e aborted due to compilation errors.
> steve:~$ perl -wle "for ($c[0] = 0; $c[0] < 5; $c[0]++;){print $c[0];}"
> Can't modify single ref constructor in scalar assignment at -e line 1, near "0;"
> syntax error at -e line 1, near "++;"
> Execution of -e aborted due to compilation errors.
> 
> I tried it with both quoting styles in case the shell was somehow
> interfering.

You have two problems there:

1. You should be using single quotes instead of double
2. The third part of the for loop shouldn't have a semicolon after it.

This works fine:

  perl -wle 'for ($c[0] = 0; $c[0] < 5; $c[0]++){print $c[0];}'

Walt

Attachment: pgpmEryr5zKjD.pgp
Description: PGP signature