perl - Handling array and creating list of non required elements -
#array @myfiles = ("public", "a0", "b0", "ks");
now, want a0, b0 , dont want other elements public , ks. so, have below code:
my @myfiles; foreach $names ( @myfiles ) { next if ( $names =~ m/public/); next if ( $names =~ m/ks/ ) ; push (@myfiles, "$names"); }
now, next if statements helps skip elements dont want in new array "@myfiles"
but, instead of next if statements, if want create list of not required elements public, ks , call in foreach loop takes care , gathers required elements a0, b0 how can done? mean :
something creating hash %bad_dir = ( public = 1, ks = 1 ); , calling in foreach loop below:
%bad_dir = ( public = 1, ks = 1 ); foreach $names ( @myfiles ) { next if ( exists ( $bad_dirs{$names} )); #but code not work ( reason creating hash having many such files don't need , want use next if statements. want shorter way. ) }
how can that.
thanks,
perldoc -f grep useful filtering lists:
use warnings; use strict; @myfiles = ("public", "a0", "b0", "ks"); %bads = map { $_ => 1 } qw(public ks); @myfiles = grep { not exists $bads{$_} } @myfiles;
Comments
Post a Comment