Day 16: Script Development Suggested Reading: The Story of Mel - PowerPoint PPT Presentation
Computer Sciences 368 Introduction to Perl Day 16: Script Development Suggested Reading: The Story of Mel (slide 5) A good book (not about Perl) 2012 Summer Cartwright 1 Computer Sciences 368 Introduction to Perl Homework Review 2012
Computer Sciences 368 Introduction to Perl Day 16: Script Development Suggested Reading: The Story of Mel (slide 5) A good book (not about Perl) 2012 Summer Cartwright 1
Computer Sciences 368 Introduction to Perl Homework Review 2012 Summer Cartwright 2
Computer Sciences 368 Introduction to Perl Priorities 2012 Summer Cartwright 3
Computer Sciences 368 Introduction to Perl E ffi ciency Correctness Clarity 2012 Summer Cartwright 4
Computer Sciences 368 Introduction to Perl The Story of Mel http://rixstep.com/2/2/20071015,01.shtml 2012 Summer Cartwright 5
Computer Sciences 368 Introduction to Perl E ffi cient (?) but Unclear opendir $dir_fh, $cwd; my @dir_stack = readdir $dir_fh; @dir_stack = grep { !/^\.{1,2}$/ && {$_ = "$cwd/$_"} } @dir_stack; push @stack, reverse @dir_stack; http://stackoverflow.com/questions/8479919/perl-using-loop-or-map-grep opendir $dir_fh, $cwd; foreach my $entry (readdir $dir_fh) { next if $entry =~ /^\.\.?$/; push @stack, "$cwd/$entry"; # or unshift } 2012 Summer Cartwright 6
Computer Sciences 368 Introduction to Perl Ine ffi cient and Unclear my @words; foreach $line (@wordlist) { chomp($line); $line = lc($line); my $found = 0; foreach my $word_ref (@words) { if ($word_ref->[0] eq $line) { $found = 1; $word_ref->[1]++; } } if (not $found) { push(@words, [$line, 0]); } } 2012 Summer Cartwright 7
Computer Sciences 368 Introduction to Perl Good Design is Often E ffi cient and Clear my %words; foreach $line (@wordlist) { chomp($line); $words{lc($line)} += 1; } 2012 Summer Cartwright 8
Computer Sciences 368 Introduction to Perl Script Development 2012 Summer Cartwright 9
Computer Sciences 368 Introduction to Perl Sub-Goal Sub-Goal ??? foreach (sort @tallies) { Sub-Goal Sub-Goal Sub-Goal Sub-Goal Goal next unless $_ > 0; $output .= format($_); } strftime(…) if (exists $hash{$key}) { … } foreach (@list) { … $_ … ; $string =~ s/…/…/ig; } unless (system(…)) { die "… $!\n"; open(my $f, '<', $filename) $count += 1; } or die "...: $!\n"; my @lines = <$f>; sub write_file {} split(/,/, @lines) close($f); 2012 Summer Cartwright 10
Computer Sciences 368 Introduction to Perl (Design, Code, Test)+ Refactor Mercilessly 2012 Summer Cartwright 11
Computer Sciences 368 Introduction to Perl HW #14: Phase I # Read and process forecasts # Read and process observations # Foreach forecast, compare to observations # Print report 2012 Summer Cartwright 12
Computer Sciences 368 Introduction to Perl HW #14: Phase II # Read and process forecasts open(my $forecast_fh, '<', $FORECAST_FILE) or die "open $FORECAST_FILE: $!\n"; while (my $forecast_line = <$forecast_fh>) { chomp($forecast_line); print "$forecast_line\n"; } close $forecast_fh; # Read and process observations # Foreach forecast, compare to observations # Print report 2012 Summer Cartwright 13
Computer Sciences 368 Introduction to Perl HW #14: Phase III sub read_file() { ... } # Read and process forecasts my @f_lines = read_file($FORECAST_FILE) or die "open $FORECAST_FILE: $!\n"; foreach my $forecast_line (@f_lines) { chomp($forecast_line); print "$forecast_line\n"; } # Read and process observations # Foreach forecast, compare to observations # Print report 2012 Summer Cartwright 14
Computer Sciences 368 Introduction to Perl HW #14: Phase IV sub read_file() { ... } # Read and process forecasts my @f_lines = read_file($FORECAST_FILE) or die "open $FORECAST_FILE: $!\n"; my %forecasts; foreach my $forecast_line (@f_lines) { chomp($forecast_line); # print "$forecast_line\n"; my ($date, $time, $high, $low) = split("\t", $forecast_line); $forecasts{$date} = [$high, $low]; } print Dumper(\%forecasts); 2012 Summer Cartwright 15
Computer Sciences 368 Introduction to Perl Questions? 2012 Summer Cartwright 16
Computer Sciences 368 Introduction to Perl Thank You! 2012 Summer Cartwright 17
Recommend
More recommend
Explore More Topics
Stay informed with curated content and fresh updates.