Why is my script only accessing the first element in array?

CG3

Below is my script. I have attempted many print statements to work out why it is only accessing the first array element. The pattern match works. The array holds a minimum 40 elements. I have checked and it is full. I have printed each line, and each line prints.

my $index = 0;
open(FILE, "$file") or die "\nNot opening $file for reading\n\n";
open(OUT, ">$final") or die "Did not open $final\n";
while (<FILE>) {
    foreach my $barcode (@barcode) {
        my @line = <FILE>;
        foreach $_ (@line) {
            if ($_ =~ /Barcode([0-9]*)\t$barcode[$index]\t$otherarray[$index]/) {
                my $bar = $1;
                $_ =~ s/.*//;

                print OUT ">Barcode$bar"."_"."$barcode[$index]\t$otherarray[$index]";
            }
            print OUT $_;
        }
        $index++;
    }
}

Okay, lets say the input was:

File:
Barcode001    001    abc
Barcode002    002    def
Barcode003    003    ghi

@barcode holds:

001
002
003

@otherarray holds:

abc
def
ghi

The output result for this script is currently printing only:

Barcode001_001 abc

It should be printing:

>Barcode001_001    abc
>Barcode002_002    def
>Barcode003_003    ghi

Where it should be printing a whole load up to ~40 lines.

Any ideas? There must be something wrong with the way I am accessing the array elements? Or incrementing? Hoping this isn't something too silly! Thanks in advance.

It needs the index because I am trying to match arrays in parallel, as they are ordered. Each line needs to match the corresponding indices of the arrays to each line in the file.

CG3

It turns out it was a simple issue as I had suspected being a Monday. I had a colleague go through it with me, and it was the placing of the index:

#my $index = 0; #This means the index is iterated through, 
                #but for each barcode for one line, then it continues 
                #counting up and misses the other values, therefore 
                #repeatedly printing just the first element of the array.
open(FILE, "$file") or die "\nNot opening $file for reading\n\n";
open(OUT, ">$final") or die "Did not open $final\n";
while (<FILE>) {
    $index = 0; #New placement of $index for initialising.
    foreach my $barcode (@barcode) {
        my @line = <FILE>;
        foreach $_ (@line) {
            if ($_ =~ /Barcode([0-9]*)\t$barcode[$index]\t$otherarray[$index]/) {
                my $bar = $1;
                $_ =~ s/.*//;
            print OUT ">Barcode$bar"."_"."$barcode[$index]\t$otherarray[$index]";
            }
        print OUT $_;
        $index++; #Increment here
        }
    #$index++; 
    }
}

Thanks to everyone for their responses, for my original and poorly worded question they would have worked and may be more efficient, but for the purpose of the script and my edited question, it needs to be this way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why is my script only accessing the first element in array?

From Dev

Why is my for loop only grabbing first element?

From Dev

Why is bash only appending first element to array

From Dev

Why is my list function only outputting first value of my array?

From Dev

SQL INSERT why does it add only the first element from array?

From Dev

Why does my function only returns the first object from the array

From Dev

Accessing first open element on a partially filled array

From Dev

Why is my script is only getting the first row of my php table? below is my code

From Dev

Why does my findMax method gives me the result of the first element of an array instead of the maximum element?

From Dev

jQuery recursive script only works on the first element

From Dev

jQuery recursive script only works on the first element

From Dev

My script works only for the first class

From Dev

Accessing first element of stdobject

From Java

Why are double quotes shown only for the first element?

From Dev

Why only the First Element is being added to the div

From Dev

Returning only first element of a sub array with $elemMatch

From Dev

perl, function only printing first element of the array

From Dev

getJSON only getting first element of returned array

From Dev

PHP only getting the first element of a array

From Dev

Why my listview only detects the first checkbox?

From Dev

Why is only my first HTTP request running?

From Dev

Why is my for only working on the first variable?

From Dev

My loop is only checking the first array item

From Dev

My loop is only checking the first array item

From Dev

Why is my JavaScript array showing a two element count when there is supposed to be only one?

From Dev

Why sizes of an array and a pointer to a first element are different?

From Dev

Why is the first element in an array always 0?

From Dev

Why sizes of an array and a pointer to a first element are different?

From Dev

Only one array element is passed to my asynctask

Related Related

  1. 1

    Why is my script only accessing the first element in array?

  2. 2

    Why is my for loop only grabbing first element?

  3. 3

    Why is bash only appending first element to array

  4. 4

    Why is my list function only outputting first value of my array?

  5. 5

    SQL INSERT why does it add only the first element from array?

  6. 6

    Why does my function only returns the first object from the array

  7. 7

    Accessing first open element on a partially filled array

  8. 8

    Why is my script is only getting the first row of my php table? below is my code

  9. 9

    Why does my findMax method gives me the result of the first element of an array instead of the maximum element?

  10. 10

    jQuery recursive script only works on the first element

  11. 11

    jQuery recursive script only works on the first element

  12. 12

    My script works only for the first class

  13. 13

    Accessing first element of stdobject

  14. 14

    Why are double quotes shown only for the first element?

  15. 15

    Why only the First Element is being added to the div

  16. 16

    Returning only first element of a sub array with $elemMatch

  17. 17

    perl, function only printing first element of the array

  18. 18

    getJSON only getting first element of returned array

  19. 19

    PHP only getting the first element of a array

  20. 20

    Why my listview only detects the first checkbox?

  21. 21

    Why is only my first HTTP request running?

  22. 22

    Why is my for only working on the first variable?

  23. 23

    My loop is only checking the first array item

  24. 24

    My loop is only checking the first array item

  25. 25

    Why is my JavaScript array showing a two element count when there is supposed to be only one?

  26. 26

    Why sizes of an array and a pointer to a first element are different?

  27. 27

    Why is the first element in an array always 0?

  28. 28

    Why sizes of an array and a pointer to a first element are different?

  29. 29

    Only one array element is passed to my asynctask

HotTag

Archive