sarathi.mh has asked for the wisdom of the Perl Monks concerning the following question:
Hi All,
I am a student, started learning Perl. I have a basic question.
Can we perform interpolation on Lists and Arrays or only on Arrays. I entered the following code :
#!usr/bin/perl -w
use strict
my $list = (2,3,4,5,6)
print "$list \n";
print $list, "\n";
This earns me the following output. "USELESS USE OF A CONSTANT IN VOID CONTEXT AT LINE 5.
Please explain the inner meaning of this error message
Re: Perl Lists and Arrays
by mr_mischief (Monsignor) on Sep 08, 2010 at 19:41 UTC
|
First, please use <c>code goes here</c> tags around your code. It makes sure the code is displayed properly regarding HTML entities, easily distinguished, and easily downloaded for editing.
Second, if that's your actual code then you're missing at least two semicolons. Perl is not a text-line terminated language like some forms of Basic. You need to end your statements with semicolons. (Technically, in Perl, semicolons separate statements but that's a superfluous distinction at the moment.)
Third, the line
my $list = (2,3,4,5,6);
I don't think is doing what you expect it to do. That will give you warnings, because you actually are throwing away a bunch of data there. Perhaps you mean @list instead of $list (which would be the common case for taking the value of an anonymous list and seems to be what you're wanting later in your code and by the name of the variable "list").
There are ways to get data about a list into a scalar, but that's not what you seem to want. Also, you can put a reference to an array (even an anonymous array) into a scalar and get data through the reference, but that's not what the rest of your code is set up to do, either.
| [reply] [d/l] [select] |
Re: Perl Lists and Arrays
by AnomalousMonk (Archbishop) on Sep 08, 2010 at 21:27 UTC
|
As you say you are learning Perl, it may be helpful for you to use diagnostics; in addition to enabling strictures and warnings (which, IMO, should always be done). Another good question to ask in a situation like this is "Why is 6 the only thing printed?". (Some of the diagnostic output in the example below has been elided, and sorry about all the line wrap.)
>perl -le
"use warnings;
use strict;
use diagnostics;
my $list = (2,3,4,5,6);
print qq{$list \n};
"
Useless use of a constant in void context at -e line 1 (#1)
(W void) You did something without a side effect in a context that
+ does
nothing with the return value, such as a statement that doesn't re
+turn a
value from a block, or the left side of a scalar comma operator.
+
...
Another common error is to use ordinary parentheses to construct a
+ list
reference when you should be using square or curly brackets, for
example, if you say
$array = (1,2);
when you should have said
$array = [1,2];
The square brackets explicitly turn a list value into a scalar val
+ue,
while parentheses do not. So when a parenthesized list is evaluat
+ed in
a scalar context, the comma is treated like C's comma operator, wh
+ich
throws away the left argument, which is not what you want. See
perlref for more on this.
...
6
| [reply] [d/l] [select] |
Re: Perl Lists and Arrays
by Corion (Patriarch) on Sep 08, 2010 at 19:42 UTC
|
my $list = (2,3,4,5,6);
... makes little sense to Perl and it tells you so. You can't assign a list to a scalar. | [reply] [d/l] |
Re: Perl Lists and Arrays
by Utilitarian (Vicar) on Sep 08, 2010 at 19:47 UTC
|
It doesn't get that far for me as you dropped the line terminating semi-colons
You are assigning constants to nothing $list is a scalar take a look through perldata for further info
Though I think you might want to use square brackets [ ] or an array
perl -we 'use strict;
my $list = [2,3,4,5,6];
my @list = [2,3,4,5,6];
print "$list \n";
print join ",", @{$list};print "\n";
print @list,"\n"'
___________________
ARRAY(0x8614880)
2,3,4,5,6
ARRAY(0x970e648)
Take a look through perlref for further info on references and accessing their values.
print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
| [reply] [d/l] [select] |
Re: Perl Lists and Arrays
by AndyZaft (Hermit) on Sep 08, 2010 at 19:41 UTC
|
Please use code tags for code, it will make things easier on all of us. Then if you wish to assign a list to an array, use the @ symbol instead of the $. That will actually print what you expect:
2 3 4 5 6
23456
I'd suggest going over the perldocs, especially the perldata section to understand difference between declaring scalars and arrays and how assignment of list would change depending on context. | [reply] [d/l] |
|
|