Perl delete an element from array

Probably I am not seeing it or I am not using the "delete" correctly I had the following codes but it does not work for me

#!/bin/perl -w
...
@sysFile1 = (a_b, a_c, a_d);
@sysFile2 = (a_c, a_e, b_f);

foreach $line1 (@sysFile1){
     trim(\$line1);
     (my $tmp1, my $tmp2) = split/_/, $line1;
     foreach $line2 (@sysFile2) {
          trim(\$line2);
          (my $tmp3, my $tmp4) = split/_/,$line2;
          if (($tmp1 eq $ $tmp3) && ($tmp2 eq $tmp4)) {
               print "found\n";
          } else {
               print "not found\n";
          }
          delete $sysFile1[1];
     }
}

and I have the following error message

$ test.pl
delete argument is not a HASH element or slice at test.pl line 50

Can someone show me the light? Or any suggestions?

Oh btw, I was trying to delete the array elements if we found it matches. Thank you.

What is "$array"? Where did you initialize it? Can you post the entire script?

ops, my fault, I was typing this on the fly while converting most of my codes, because of the policy issue,

Actually the $array1 would be $sysFile1. I will make an update to the snipplets, thank you, nathan, for taking your time to review my question.

Try this little script and post the results...

$ cat del.pl
#! /usr/local/bin/perl -w

print "perl version is ", $],  "\n";

@array=(a_b, a_c, a_d);

for ($index=0; $index <= $#array; $index++) {
        if (exists $array[$index])  {
                print "index = ", $index, "  element = ", $array[$index], "\n";
        } else {
                print "index = ", $index, "  element is not there", "\n";
        }
}

delete $array[1];

for ($index=0; $index <= $#array; $index++) {
        if (exists $array[$index])  {
                print "index = ", $index, "  element = ", $array[$index], "\n";
        } else {
                print "index = ", $index, "  element is not there", "\n";
        }

}

$ ./del.pl
perl version is 5.006001
index = 0  element = a_b
index = 1  element = a_c
index = 2  element = a_d
index = 0  element = a_b
index = 1  element is not there
index = 2  element = a_d
$

I don't understand what you are trying to do, actually.

Just bear in mind:

  • To really delete an item from an array, such as (1, 2, 3) -> (1, 3), use splice().
  • delete() simply sets the elements as "undef". Unless the elements happen to appear at the end of the array, they will not be removed, and is usually not what you want. delete() is usually more meaningful for hashes to remove hash items, NOT array items.

You can look at this illustration:

[bernardchan@bernardchan ~]$ perl -MData::Dumper -w -e '@a = (1,2,3); delete $a[0]; print Dumper [@a]'
$VAR1 = [
          undef,
          2,
          3
        ];
[bernardchan@bernardchan ~]$ perl -MData::Dumper -w -e '@a = (1,2,3); splice(@a, 0, 1); print Dumper [@a]'
$VAR1 = [
          2,
          3
        ];

As I'm not sure what you exactly would like to do, you may need to figure out the problem yourself. You may read the manpage for delete() for the complete details.

Thank you all for replying,

Perderabo, your way is very close to what I am trying to accomplish, I will try your logic and let you all know the result. Thank you!

:smiley: