Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: filtering an array

by vagabonding electron (Hermit)
on Sep 02, 2012 at 14:55 UTC ( #991281=note: print w/ replies, xml ) Need Help??


in reply to filtering an array

I am still a newbie, but / therefore - what is wrong with using hash here (with keys as line numbers and values as bases)?

#!/usr/bin/perl -l use strict; use warnings; print "If an array element is a line number and a base together as a s +tring:"; 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"; }
It prints:
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


Comment on Re: filtering an array
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://991281]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (16)
As of 2015-07-31 15:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (279 votes), past polls