talk2kvj has asked for the wisdom of the Perl Monks concerning the following question:

Here is a small piece of code that i couldn't understand. and i get an error as indicated in the comment below. can anyone please explain me the reason for this and what that particular line is doing.

thanks in advance.

foreach $item (@results) { @line=split/,/, $item; $seen{$line[1]}="0"; #$seen{$item}=0; #print "LINE 1 $line[1]\n"; } foreach $item (@results) { @line=split/,/, $item; if ($line[0] =~ /1|6|7|8|9|5/) { &integrate; sub integrate { %matching=(); if ($line[0]=~/6|7|8|/) { $overlapc=$line[1]; foreach $itemi (@eresults) { @linei=split/,/, $itemi; if ($linei[1] eq $line[1]) { @matchone=split/ /, $linei[2]; $lengthandn=$line[4].',', $line[5]; ##error here--- Useless us +e of array element in void context--can u please tell me what this li +ne does. $matching{$matchone[0]}="$lengthandn"; $matching{$matchone[1]}="$lengthandn"; #$seenclone{$matchclone[1]}="1"; #print "678 line 1 $line[1] Length $lengthandn matchclone $matchclone +[1] m0 $matchclone[0]\n"; } }

20040527 Edit by castaway: Changed title from 'newbie- help with a piece of code'

  • Comment on Help resolve Useless use of array element in void context warning.
  • Download Code

Replies are listed 'Best First'.
Re: Help resolve Useless use of array element in void context warning.
by Zaxo (Archbishop) on May 26, 2004 at 19:57 UTC

    You have comma where I think you want dot,

    # $lengthandn=$line[4].',', $line[5]; # ^ $lengthandn = $line[4] . ',' . $line[5];

    After Compline,
    Zaxo

      Thank you all for the help. It worked.
      http://www.perlmonks.org/index.pl?node_id=366807
Re: Help resolve Useless use of array element in void context warning.
by BrowserUk (Patriarch) on May 26, 2004 at 19:55 UTC

    It looks like the line is trying to concatenate the 4th and 5th elements of the line together separated by a comma, but the second dot operator has been typod as a comma.

    $lengthandn=$line[4].',',$line[5]; # ^ should be .

    That line would probably be better written as

    $lengthandn = "$line[4],$line[5]";

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
Re: Help resolve Useless use of array element in void context warning.
by pbeckingham (Parson) on May 26, 2004 at 20:05 UTC

    Take that line out from the code, and it looks like:

    $lengthandn = $line[4] . ',', $line[5];
    I suspect that the last comma on the line was intended to be a period, which eliminates the error, and is probably what you wanted, leaving:
    $lengthandn = $line[4] . ',' . $line[5];
    Update: Oops - didn't see the earlier of the duplicated posts with answers already provided.

Re: Help resolve Useless use of array element in void context warning.
by Trag (Monk) on May 26, 2004 at 19:54 UTC
    I'm new enough that I don't know either, (if I had to guess, it's treating a normal variable as an array, i.e  my $foo=1; print $foo[5]; will not work, I think (UPDATE: Having read the other posts, it looks like I'm wrong about that)). For future reference, please place your code between <'code'> <'/code'> in posts like these (minus the quotation marks though). It makes it much easier to read. Here's a cleaned up version of your code:
    foreach $item (@results) { @line=split/,/, $item; $seen{$line[1]}="0"; #$seen{$item}=0; #print "LINE 1 $line[1]\n"; } foreach $item (@results) { @line=split/,/, $item; if ($line[0] =~ /1|6|7|8|9|5/) { &integrate; sub integrate { %matching +=(); if ($line[0]=~/6|7|8|/) { $overlapc=$line1; foreach $itemi (@eresults) { @linei=split/,/, $itemi; if ($linei1 eq $line1) { @matchone=split/ /, $linei[2]; $lengthandn=$l +ine[4].',', $line[5];
    ##error here--- Useless use of array element in void context--can u please tell me what this line does.
    $matching{$matchone[0]}="$lengthandn"; $matching{$matchone[1]}="$lengthandn"; #$seenclone{$matchclone[1]}="1"; #print "678 line 1 $line[1] Length $l +engthandn matchclone $matchclone[1] m0 $matchclone[0]\n"; } }
    It seems to be missing portions though. All the variables and arrays should be declared at the beginning of the script (as far as I know). One other thing, all of the locations where a number was placed in square brackets were converted to links, and the square brackets disappeared. This is because you did not place your code between the
    tags, so allways do that, please. <div class="pmsig"> <div class="pmsig-334202"> <br><br><code> our @item = reverse (114, 101, 107, 99, 97, 104, 32, 108, 114, 101, 80, 32, 114, 101, 104, 116, 111, 110, 97, 32, 116, 115, 117, + 74); local $my = reverse ")meti@\ ,rhc (pam tnirp";eval $my;