David vd Geer Inhuur tbv IPlib wrote: > Hi, > > I am currently almost done with my current job. > As I am reviewing my scripts the foreach-loop started to anoy me. > > I am afraid this is slowing down the script. Does anyone know a faster way to > do the following : > > # ------ > open(FH, "< $groupfile"); > @usrs = <FH>;
Always check if open succeeds with an or die "..." statement Slurping the file in is not advisable. It will be better to loop through it > > close FH; > > $htusr = (grep {/htuser: /} @usrs)[0] ; > $phi = (grep {/phil: /} @usrs)[0] ; > $al = (grep {/all: /} @usrs)[0] ; It is better to anchor regexes, this should be written as /^htuser:/ etc. I guess you are trying to find the group name of an entered user (You have not mentioned what $pwuser is, this is a guess) If this is true (perldoc -f getpwnam, perldoc -f getgrgid) This piece of code should do the job for you #!/usr/bin/perl -w use strict; my $grp_name = getgrgid ((getpwnam ($pwuser))[3]); $grp_name will contain the group name > > > @htuser = split(/ /, $htusr); > @phil = split(/ /, $phi); > @all = split(/ /, $al); > > foreach $htuser(@htuser) { > chomp $htuser; > if ( "$pwuser" eq "$htuser" ) { $group = "iclab"; } > } > foreach $phil(@phil) { > chomp $phil; > if ( "$pwuser" eq "$phil" ) { $group = "phil"; } > } > foreach $all(@all) { > chomp $all; > if ( "$pwuser" eq "$all" ) { $group = "all"; } > } > if(!($group)) { $group = "none"; } > # ------ > Groupfile : > > htuser: user1 user2 user3 manyothers > phil: user4 user5 manymore > all: external1 external2 etc > # ------- > > I know I should have used Strict, after I reviewed everything I will try to make > it strict again. You guys convinced me of using strict. It's only a pain to correct > it afterwards :( > > Thanks for your help in advance ! > > Regs David > > -- > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]