#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    18
    Rep Power
    0

    Issue with Splice


    I saw that splice could add an element to an array given an index. I tried it according to the post with the elements to replace set to 0. however it still erases the line.

    Code:
    #Annotation Helper - Version .1a
    #July 2013, Kenneth Fechter
    
    #!/usr/bin/perl
    
    use strict;
    
    #Open Error Log
    open(STDERR, '>>', "error.log") or die("Cannot redirect STDERR: $!\n");
    if((defined $ARGV[0]) && defined $ARGV[1])
    {
    	open(my $IFH, '<', $ARGV[0]) or die "Error opening $ARGV[0]: $!\n";
    	
    	#Add output file to array for editing;
    	open(my $IOFH, '<', $ARGV[1]) or die "Error opening $ARGV[1]: $!\n";
    	my @OutputArray = <$IOFH>;
    	close $IOFH or die "Error";
    	
    	#open Output file for writing
    	open(my $OFH, '>', $ARGV[1]) or die "ERROR\n";
    	
    	
    	my @inarray = <$IFH>;
    	@inarray = splice(@inarray,1);
    	my @Positionarr;
    	my @Interactionarr;
    	while(my $line = <@inarray>)
    	{
    		chomp($line);
    		my ($Position,$Interaction) = split(',', $line);
    		push(@Positionarr, $Position);
    		push(@Interactionarr, $Interaction);
    	}
    	close($IFH) or die "error closing file\n";
    	
    	my $InteractStr;
    	while(my $line2 = <@Interactionarr>)
    	{
    		chomp($line2);
    		$InteractStr = $InteractStr . $line2;
    	}
    	my $SequenceStr;
    	while(my $line3 = <@Positionarr>)
    	{
    		chomp($line3);
    		my @chars = $line3 =~ /./sg;
    		$SequenceStr = $SequenceStr . $chars[0];
    	}
    
    	while(my $outline = <@OutputArray>)
    	{
    		chomp($outline);
    		my $result = index($outline, $SequenceStr);
    		if($result != -1)
    		{
    			my( $index )= grep { $OutputArray[$_] eq $outline } 0..$#OutputArray;
    			my $InteractionString = " " x $result;
    			my $InteractionString = $InteractionString . $InteractStr;
    			splice(@OutputArray, ($index +1), 0, $InteractionString);
    			
    		}
    		
    		
    	}
    	print $OFH join("",@OutputArray);
    	close $OFH or die;
    	
    }
    elsif(defined $ARGV[0])
    {
    	if($ARGV[0] eq "help")
    	{
    		print "LineParser.pl - July 2013\n";
    		print "Standard Usage: perl ScriptName.pl <InputFileName> <AnnotationFile>\n";
    		print "Errors are saved to a log file\n";
    	}
    	else
    	{
    		print "Run script with argument 'help' to get usage";
    	}
    }
    else
    {
    	print "no arguments defined. Run script with argument 'help' to get usage";
    }
    this is the code I have. Everything else works as I want it to, the only issue being the splice command. Its even putting the line in approximately the right place, only overwriting the line it should be under. I bolded and underlined the command in question
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    830
    Rep Power
    496
    Please explain clearly what you you are trying to do, how you do it and what you obtain, if possible with a 3 or 4 line example.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    18
    Rep Power
    0
    Originally Posted by Laurent_R
    Please explain clearly what you you are trying to do, how you do it and what you obtain, if possible with a 3 or 4 line example.
    Code:
    my $result = index($outline, $SequenceStr);
    		if($result != -1)
    		{
    			my( $index )= grep { $OutputArray[$_] eq $outline } 0..$#OutputArray;
    			my $InteractionString = " " x $result;
    			my $InteractionString = $InteractionString . $InteractStr;
    			splice(@OutputArray, ($index +1), 0, $InteractionString);
    			
    		}
    This is the code I have right now. I am trying to attempt to pull the elements in the array up to the index, add a new line, then put the spliced data back in. $result is for creating the offset. $index is for finding out which line I want to retrieve.

    EDIT: I am now inserting correctly, but the index is wrong. always inserting at the beginning. How do I make it so that it finds the array position of the matched substring

    Offset = index of substring
    I need the position in the array of the line with the substring.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,930
    Rep Power
    1225
    Take a look at the first_index() function in the List::MoreUtils module.

IMN logo majestic logo threadwatch logo seochat tools logo