Paul Kraus wrote: > so foreach dumps the entire file into memory before iterating over the > lines. Good to know. Thanks. > > I don't think your solution will work. > Since the count being taken is going to be sparadic. > > somelements only = 1 at the BOF but by EOF it equals 4. Since you tested > per line your results will be bad because at test time for that line > somelement only = 1 when actually it was equal to 4. Wow that confused > me :) >
you still don't have to read the file twice. By reading it twice, your program is probably 100 times slower than it needs to be especially when you use the split,foreach again one each and every line of the same file. you already store the count in one hash so that's good. what you should do is to store $temp[0] as well. once you finish with the first read, use the hash for your second count: #!/usr/bin/perl -w use strict; my %dup; my %vend; open(PEL,'whatever') || die $!; while(<PEL>){ chomp; my @temp=map{s/ //g;$_} split(/,/); $dup{$temp[1]}->[0]++; $dup{$temp[1]}->[1]=$temp[0]; } close(PEL); while(my($key,$ref) = each %dup){ if($ref->[0] == 1){ $vend{$key} = $ref->[1]; }else{ delete $vend{$key}; } } __END__ not only reading the file twice is slow. there is a potential problem that the file won't be the same (got modified) the second time you open it. It might not be there (get deleted) as well. Don't let that kind of thing get into your program. david >> while (<PEL>) { >> chomp; >> s/ +//g; >> my ($value, $field) = split /,/; >> $dup{$field}++; >> if ($dup{field} == 1) { $vend{$field} = $value } >> else { delete $vend{$field} } >> } >> -----Original Message----- >> From: Jeff 'japhy' Pinyan [mailto:[EMAIL PROTECTED]] >> Sent: Thursday, January 02, 2003 10:30 AM >> To: Paul Kraus >> Cc: 'Perl' >> Subject: RE: Restarting at top of file >> >> >> On Jan 2, Paul Kraus said: >> >> >As you can see I am building a hash on the first pass. Then on the >> >second pass I am building a second hash but I am checking the first >> >hash to see if it had a count greater then two. I don't see >> any other >> >way to do this except two passes through the file. Correct >> me if I am >> >wrong. >> >> Watch me. :) >> >> >I am using a foreach loop because I just picked it. :) it situations >> >like this I never really saw a difference between while and foreach. >> >Why would I want to use a while loop instead? >> >> foreach (<FOO>) reads ALL the lines of <FOO> at once, and >> makes a big list. while (<FOO>) only reads one line at a time. >> >> >foreach (<PEL>){ >> > chomp; >> > @temp=split /,/,$_; >> > $_=~s/ //g foreach (@temp); >> > $dup{$temp[1]}++; >> >} >> > >> >#add item->vendor part numbers to hash if don't exist in >> dup hash seek >> >PEL, 0, 0; foreach (<PEL>){ >> > chomp; >> > @temp=split /,/,$_; >> > $_=~s/ //g foreach (@temp); >> > $vend{$temp[1]}=$temp[0] unless ($dup{$temp[1]} > 1); >> >} >> >> while (<PEL>) { >> chomp; >> s/ +//g; >> my ($value, $field) = split /,/; >> $dup{$field}++; >> if ($dup{field} == 1) { $vend{$field} = $value } >> else { delete $vend{$field} } >> } >> >> That looks to me like it will work. >> >> -- >> Jeff "japhy" Pinyan [EMAIL PROTECTED] >> http://www.pobox.com/~japhy/ >> RPI Acacia brother #734 >> http://www.perlmonks.org/ http://www.cpan.org/ >> <stu> what does y/// stand for? <tenderpuss> why, >> yansliterate of course. [ I'm looking for programming work. >> If you like my work, let me know. ] >> -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]