Let’s assume I have an array, @atom. I am pushing three elements, $a
, $b
, and $c
(residue name, chain and residue number, respectively, fetched from a Protein Data Bank (.pdb) file) into that array.
For instance, $b
has values AAAAAAAAA, BBBBBBB, and CCCCCCC. How can I empty the array every time when $b
changes?
The array is as follows,
push(@atom, "$a $b $c");
I'm not sure why you are using an array when you're only storing a single value in it. You think you are storing three values, but you are putting those into a single string before storing them in the array.
To store three values in your array, you might use code like this:
my @atom = ($residue_name, $chain, $residue_number);
(Notice that I have also changed your variable names. $a
, $b
and $c
are terrible names for variables and $a
and $b
are special variables for Perl and should not be used in random code.)
I don't really know what you are doing here, but it seems to me that it might make more sense to store this data in a hash.
my %atom = (
residue_name => $residue_name,
chain => $chain,
residue_number => $residue_number,
);
Of course, that's only a guess as I don't know what you need to do with your data - but an important part of programming is to get your data structures right.
But let's assume for now that you're still using your original array and you want to a) see if the $chain
variable has changed its value and b) empty the array at that point. You would need to write code something like this:
my @atom = ($residue_name, $chain, $residue_number);
# Store the current value of $chain
my $original_chain = $chain;
Then, later on, you need to check the value has changed and take appropriate action.
if ($chain ne $original_chain) {
@atom = ();
}
Of course, this is all just the sketchiest of suggestions. I have no idea how your code is structured.