Re: Useless use of array element in void context



On Jun 10, 6:20 am, Marek <m...@xxxxxxxxxxxxxxxxxxxxxxx> wrote:
as a beginner, I am not sure, why I get this message "Useless use
of array element in void context". To illustrate I made a
concentrated excerpt of my larger perl project.

Excellent! Well done! Seriously, that's one of the best things I've
read in this newsgroup in a long time.

#! /usr/bin/perl/

use warnings;
use strict;

More excellencence!!

if (
(
$old_line[2], $old_line[3], $old_line[4],
$old_line[5], $old_line[6]
) == (
$out_line[2], $out_line[3], $out_line[4],
$out_line[5], $out_line[6]
)
)
{

This is the problem. This block does not do what you think it does.
The == operator in Perl does not compare lists. It only compares
scalars. Therefore, both of the "lists" are being evaluated in scalar
context[1], and so it's only comparing the last element of each list.
That is, the above block is identical to:
if ($old_line[6] == $out_line[6])
None of the other elements are being looked at by the == operator.
That's why Perl is telling you all those array elements are being
uselessly used in a void context.

If you want to compare two arrays for equality of their elements, you
have a few possibilities. You could modify the example in the FAQ to
suit your needs. (From a command line, run `perldoc -q equal`), or you
could use either of the modules Array::Compare or List::Compare from
the CPAN (head to http://search.cpan.org and search for those
modules).

Note there are also less robust solutions involving comparing the two
arrays as strings - for example:
if ("@a1" eq "@a2")
or
use Data::Dumper;
if (Dumper(\@a1) eq Dumper(\@a2) )
But these are generally dangerous because they could return false
positives. For example, the first one would report that these two
arrays are indeed equal:
my @a1 = (1, 2, 3);
my @a2 = ("1 2", 3);
The Dumper one is more unlikely to give false positives, but still
possible.

My overall suggestion would be to modify the example given in the FAQ
to suit your needs.

Good luck!
Paul Lalli

.