Perl delete an element from array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl delete an element from array
# 1  
Old 01-17-2007
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

Code:
#!/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
Code:
$ 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?

Last edited by ahtat99; 01-17-2007 at 04:34 PM..
# 2  
Old 01-17-2007
Oh btw, I was trying to delete the array elements if we found it matches. Thank you.
# 3  
Old 01-17-2007
What is "$array"? Where did you initialize it? Can you post the entire script?
# 4  
Old 01-17-2007
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.
# 5  
Old 01-17-2007
Try this little script and post the results...
Code:
$ 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
$

# 6  
Old 01-17-2007
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:

Code:
[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.
# 7  
Old 01-18-2007
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!

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Array Element

This question is for someone that's more familiar with Array Element. I need to know if the maximum array element that can be assigned is 1024 and if its so, Is there a workaround solution when the counter exceeded 1024? param_array="$param_nam" counter=$counter+1 #to avoid space... (3 Replies)
Discussion started by: cumeh1624
3 Replies

2. Shell Programming and Scripting

Perl: How to check whether my array contains element x

Hi All, I am new to perl I am stuck in simple problem I need your help I want to define a subroutine. sub check_if_entity_exists(@array_to_be_checked,$entityName) I have array as http-listener-1 http-listener-2 http-listener-3 http-listener-4 If i send http-listener-3 my... (1 Reply)
Discussion started by: javaholics
1 Replies

3. Programming

How to delete a duplicate element from below array.

Hello forum , Please solve the below queery. A sorted array which has repated elements. A = {1,2,3,3,4,5,5,5,6,9,9} i want to delete the duplicate elements and to genrate a new array. i need the array sholud be like this A = {1,2,3,4,5,6,9}. Please write the piece of code... (4 Replies)
Discussion started by: workforsiva
4 Replies

4. Shell Programming and Scripting

Delete lines in an array Using perl

im having an array @check which contains text ..i want to open the array and i have to delete lines starting from a word called "check1" till "check2" for eg:- check1 Use descriptive titles when posting. For example, do not post questions with subjects like "Help Me!", "Urgent!!" or "Doubt".... (0 Replies)
Discussion started by: rajkrishna89
0 Replies

5. Shell Programming and Scripting

previous element in the array perl

Hi, How to get previous/next element in the array perl Example @queue = (1, 2 ,3 , 4); I want to get value of 1 and 2, or, 2 and 3, or 3 and 4...etc and compare to value which one is greater to do that I need to get previous and next element of array ? (1 Reply)
Discussion started by: guidely
1 Replies

6. Shell Programming and Scripting

Perl: One action if an element doesn't exist in array

Hello, I want to run one (not multiple) action if an element doesn't exist in array. for example: @array = (1..10); foreach $el (@array) { if ($el != 11) { print "number not found\n"; } } the output of this simple script: number not found (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

7. Shell Programming and Scripting

remove an element from array

I need to remove an element from the below array variable TABLENAME. #!/bin/ksh set -A TABLENAME "mirf roxar keke mirs" echo "the array is ${TABLENAME}" If i need to remove say keke and have the final TABLENAME as below, how this could be achieved. Pls throw some light. echo "Modified... (3 Replies)
Discussion started by: michaelrozar17
3 Replies

8. Shell Programming and Scripting

Shift array element

I want to delete and 0th element of array in shell scrpit and also shift all others to one level up. (2 Replies)
Discussion started by: darshakraut
2 Replies

9. Shell Programming and Scripting

Remove an element from an array in PERL

Hello guys, I have the following question. Consider the following code in PERL for($xeAnumber=0; $xeAnumber<@xeAnumber; $xeAnumber++) { if(@xeAnumber==@final_file) { @final_file=@xeTimeStamp; }... (3 Replies)
Discussion started by: chriss_58
3 Replies

10. Shell Programming and Scripting

accessing my first element of array

Hello everyonel, I have an array set like so num=4 read name arr=name I go through while loop to assign different values to different array element from 1 to 4. when I try to access the FIRST element of the array I get the last one first. Like if I say ${arr} it will show the last element... (4 Replies)
Discussion started by: afadaghi
4 Replies
Login or Register to Ask a Question