note
vagabonding electron
I am still a newbie, but / therefore - what is wrong with using hash here (with keys as line numbers and values as bases)?
<c>
#!/usr/bin/perl -l
use strict;
use warnings;
print "If an array element is a line number and a base together as a string:";
my %positions;
for my $line (split /\n/, <<'END')
1 ACAC
2 AGAC
3 AGTC
4 ACCA
END
{
next if $line =~ /[GT]/;
my ($number, $bases ) = split / /, $line;
$positions{$number} = $bases;
}
for my $number ( sort keys %positions )
{
print "$number => $positions{$number}";
print "Just print a number $number";
}
print "If there is an array element for a line number and for a base:";
my @array = ( qw (1 ACAC 2 AGAC 3 AGTC 4 ACCA) );
%positions = ();
for (my $i = 0; $i < @array; $i +=2 )
{
my ($number, $bases ) = @array[$i, $i+1];
next if $bases =~ /[GT]/;
$positions{$number} = $bases;
}
for my $number (sort keys %positions)
{
print "$number => $positions{$number}";
print "Just print a number $number";
}
</c>
It prints:
<c>
If an array element is a line number and a base together as a string:
1 => ACAC
Just print a number 1
4 => ACCA
Just print a number 4
If there is an array element for a line number and for a base:
1 => ACAC
Just print a number 1
4 => ACCA
Just print a number 4
</c>
991184
991184