splice function problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting splice function problem
# 1  
Old 10-28-2009
splice function problem

Hi All,

I am using splice function in for loop to delete particular element from array with one condition.

Code:
my $cnt=0;
foreach my $elem (@result) 
{
 if (condition){
    splice(@result, $cnt, 1);}
 else{
 $cnt++;}
}

Now when in array, two elements comes sequentially with the condition=true the splice skips second element to delete. It just deletes first one only.

Any clues on this?

Last edited by gentleDean; 10-28-2009 at 06:19 AM.. Reason: Part of some code was missing
gentleDean
# 2  
Old 10-28-2009
Hi.

In perl (and many other scripting languages), printing intermediate values is a useful debugging technique. Setting up a small array and running it through your code will likely illuminate the problem ... cheers, drl
# 3  
Old 10-29-2009
Hi drl,

I am using print to debug my code but my problem here is logical...
In first element where condition is true, will delete (splice) the element and rest are pushed up in array. In immediate next run it will go to next element (as I am using foreach). So it will skip one element in this case....

---------- Post updated 10-29-09 at 01:39 AM ---------- Previous update was 10-28-09 at 10:20 PM ----------

I found one alternative here, that's "redo"... But again, having some loopholes...
Can anyone suggest a better solution here
gentleDean
# 4  
Old 10-29-2009
Hi.

I was suggesting something like this:
Code:
#!/usr/bin/perl

# @(#) p1	Demonstrate splice element tracking.

use warnings;
use strict;
use feature qw(switch say);
use 5.010;

my ($debug);
$debug = 0;
$debug = 1;

my ( $cnt, $element );
my (@a) = qw/ a b c a z /;

print "\n";

print " Array as input :@a:\n\n";

# Original code.

$cnt = 0;
foreach $element (@a) {
  if ( $element eq "a" ) {
    splice( @a, $cnt, 1 );
    print " Inside loop, a is :@a:\n";
  }
  else {
    $cnt++;
  }
}
print "\n Original, cnt = $cnt\n", " array = @a\n";
print "\n -----\n\n";

# Re-written code.

@a   = qw/ a b c a z /;
$cnt = 0;
foreach $element (@a) {
  if ( $element eq "a" ) {
    splice( @a, $cnt, 1 );
    print " Inside loop, a is :@a:\n";
  }
  $cnt++;
}
print "\n Modified, cnt = $cnt\n", " array = @a\n";

exit(0);

producing:
Code:
% ./p1

 Array as input :a b c a z:

 Inside loop, a is :b c a z:
 Inside loop, a is :b a z:

 Original, cnt = 1
 array = b a z

 -----

 Inside loop, a is :b c a z:
 Inside loop, a is :b c z:

 Modified, cnt = 3
 array = b c z

Best wishes ... cheers, drl
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash function problem

I am trying to figure out why I am having a "problem" with some functions in a bash script I am running. The reason for air quoting is that the functions are working, they are just not displaying anything to screen when called from another function. Here's an example: function Create_Input {... (6 Replies)
Discussion started by: dagamier
6 Replies

2. Shell Programming and Scripting

Function problem

hey guys, im trying to learn bourne shell atm and I'm having some issues with functions. so heres my code: #!/bin/bash ##functions memory () { free -m } space () { df -h } ip () { (5 Replies)
Discussion started by: hawkfro12
5 Replies

3. Solaris

sparc, raw splice on end of disk

I noticed we have a small splice on end of disk that is not mounted. What could that be and how do we move it few sectors? Thanks. (7 Replies)
Discussion started by: orange47
7 Replies

4. Shell Programming and Scripting

Problem using function in awk

I created two functions that output two random variables. I want to output them in the output file. But it does not seem to work. # Function rgaussian1(r1, r2) # Gaussian random number generator function rgaussian1(r1, r2) { pi = 3.142 v1 = sqrt( -2 * log(rand()) ) v2... (18 Replies)
Discussion started by: kristinu
18 Replies

5. Shell Programming and Scripting

Calling Function Problem

Hi, I had a scripts which calls two function. One function will call another function, script is working fine but the second function is not calling the first function. Below is the script #!/usr/bin/ksh fun1() { echo $DATETIME >> Test1.ksh return 0 } fun2() { typeset DATETIME=`date... (5 Replies)
Discussion started by: somu_june
5 Replies

6. Shell Programming and Scripting

Problem with function in a script

Hi All, What is the problem with the following script: function mmdd { dd=$2 case $1 in "Jan") mm=01;; "Feb") mm=02;; "Mar") mm=03;; "Apr") mm=04;; "May") mm=05;; "Jun") mm=06;; "Jul") mm=07;; "Aug") mm=08;; "Sep") mm=09;; "Oct") mm=10;; "Nov") mm=11;; "Dec") mm=12;; *)... (3 Replies)
Discussion started by: Hiso
3 Replies

7. Shell Programming and Scripting

Problem with Recursive function

Hi all, I have to move all the files in a tree directory structure to a single directory. Inorder to know which file is from which directory , i'll have to add the name of the directory to the file name. For this i wrote a recursive function which is as follows... (4 Replies)
Discussion started by: malle
4 Replies

8. Shell Programming and Scripting

PERL function problem

I have perl script as follow. ------------------------------------------------------------------------ #! /usr/bin/env perl use strict; sub printLines { print "Inside the function.............\n"; my (@file , $count , $key ) = $_; print $count , $ key ; #... (2 Replies)
Discussion started by: avadhani
2 Replies

9. Programming

Problem with aio_write() function

Hello, How to execute a call back function after aio_write() or aio_read() in Sun Solaris 5.7? I have filled the control block struct aiocb as follows: aio_sigevent.sigev_signo = SIGEV aio_sigevent.sigev_notify = SIGEV_THREAD Then I have filled the call back function in ... (0 Replies)
Discussion started by: hmurali
0 Replies

10. Programming

rexec() function problem

Hi folks, I'm trying to make a reconnection algorithm using rexec(), but I noticed that when rexec() fails returning -1, it is impossible to make it run successfully again until you restart the program or the thread. Example, I have a endless loop for connection retries, if I supply a wrong... (7 Replies)
Discussion started by: lcmoreno
7 Replies
Login or Register to Ask a Question