The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
perl -write values in a file to @array in perl meghana Shell Programming and Scripting 27 06-07-2009 05:05 PM
Remove an element from an array in PERL chriss_58 Shell Programming and Scripting 3 06-06-2008 05:08 AM
A final question! Compare character with each array element rorey_breaker Shell Programming and Scripting 1 09-28-2007 09:26 AM
array type has incomplete element type jaganadh High Level Programming 1 07-24-2007 03:54 AM
accessing my first element of array afadaghi Shell Programming and Scripting 4 09-29-2005 04:43 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-17-2007
ahtat99 ahtat99 is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 41
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 (permalink)  
Old 01-17-2007
ahtat99 ahtat99 is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 41
Oh btw, I was trying to delete the array elements if we found it matches. Thank you.
  #3 (permalink)  
Old 01-17-2007
nathan nathan is offline VIP Member  
Supporter
  
 

Join Date: Jul 2006
Posts: 156
What is "$array"? Where did you initialize it? Can you post the entire script?
  #4 (permalink)  
Old 01-17-2007
ahtat99 ahtat99 is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 41
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 (permalink)  
Old 01-17-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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 (permalink)  
Old 01-17-2007
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 (permalink)  
Old 01-18-2007
ahtat99 ahtat99 is offline
Registered User
  
 

Join Date: Aug 2006
Posts: 41
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!

Closed Thread

Bookmarks

Tags
awk, awk trim, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0