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

.



Relevant Pages

  • Re: random array elements and speed
    ... JJ> than splice for large arrays but slower for small arrays. ... and swap/pop is several perl ops. ... a loop with a list of sizes and use my code changes (you want to compare ...
    (comp.lang.perl.misc)
  • Optimizing File handling operations
    ... I Frequently use perl to process 2 files line by line. ... Most of the times I compare two files line by line and check if one line is ... different arrays. ... creating large sized arrays may consume lot of memory. ...
    (perl.beginners)
  • Re: Optimizing File handling operations
    ... I Frequently use perl to process 2 files line by line. ... Most of the times I compare two files line by line and check if one line is ... different arrays. ... You can check for $line1 or $line2 containing anything after the loop has terminated. ...
    (perl.beginners)
  • Re: Compare two arrays
    ... Jerry Preston wrote: ... > compare two arrays and delete the same values in one array. ... > What is wrong and is there a better, PERL way to do this? ...
    (perl.beginners)
  • Re: Array comparison
    ... And how do you compare two classes? ... does not explain why it can't compare two like-typed arrays. ... Free Pascal supports operator overloading. ... more inclined to say the reason is lack of customer demand for the feature. ...
    (alt.comp.lang.borland-delphi)