[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: gEDA-user: Perl



Karl:
> wpd:
...
> > #!/usr/bin/perl -w
> > my @array = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
> > print length(@array), "\n";
...
> > Maybe things have changed since then, but I spent way too much time
> > being confused by this.

If you really want to understand why this prints 2, look at this:

$ perl -e 'print length(1,2), "\n";'
Too many arguments for length at -e line 1, near "2)"
Execution of -e aborted due to compilation errors.
$

This means the length wants just one argument.

Now, @array is either the array of numbers above, or
@array is the number of elements of the array, i.e. 11.
It depend on the context, if it is "vector" or "scalar"
context.

Since length only wants one argument, @array is treated as a
scalar, which is 11, and the length of the string "11" is 2.

If you want to print out the number of elements in @array,
you have to force it to a scalar context as in:

print scalar(@array), "\n";

 or
 
print @array+0, "\n";

> Badly spent time, check the man page.
>
> If you don't like perl/scheme/pythonn/ruby/c/whatever ---
> don't use it, but stop complaining about it.

Sorry for being a little harsh.

Regards,
/Karl Hammar

-----------------------------------------------------------------------
Aspö Data
Lilla Aspö 148
S-742 94 Östhammar
Sweden
+46 173 140 57




_______________________________________________
geda-user mailing list
geda-user@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-user