Day 16: Script Development Suggested Reading: The Story of Mel - - PowerPoint PPT Presentation

day 16 script development
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Day 16: Script Development

Suggested Reading: The Story of Mel (slide 5) A good book (not about Perl)

1

slide-2
SLIDE 2

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Homework Review

2

slide-3
SLIDE 3

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Priorities

3

slide-4
SLIDE 4

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

4

Correctness Clarity Efficiency

slide-5
SLIDE 5

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

The Story of Mel

http://rixstep.com/2/2/20071015,01.shtml

5

slide-6
SLIDE 6

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Efficient (?) but Unclear

6

  • pendir $dir_fh, $cwd;

my @dir_stack = readdir $dir_fh; @dir_stack = grep { !/^\.{1,2}$/ && {$_ = "$cwd/$_"} } @dir_stack; push @stack, reverse @dir_stack;

  • pendir $dir_fh, $cwd;

foreach my $entry (readdir $dir_fh) { next if $entry =~ /^\.\.?$/; push @stack, "$cwd/$entry"; # or unshift }

http://stackoverflow.com/questions/8479919/perl-using-loop-or-map-grep

slide-7
SLIDE 7

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Inefficient and Unclear

7

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]); } }

slide-8
SLIDE 8

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Good Design is Often Efficient and Clear

8

my %words; foreach $line (@wordlist) { chomp($line); $words{lc($line)} += 1; }

slide-9
SLIDE 9

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Script Development

9

slide-10
SLIDE 10

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

10

Goal

split(/,/, @lines)

  • pen(my $f, '<', $filename)
  • r die "...: $!\n";

my @lines = <$f>; close($f); $count += 1; foreach (@list) { … $_ … ; } if (exists $hash{$key}) { … } sub write_file {} $string =~ s/…/…/ig; unless (system(…)) { die "… $!\n"; } strftime(…)

???

Sub-Goal Sub-Goal Sub-Goal Sub-Goal Sub-Goal Sub-Goal

foreach (sort @tallies) { next unless $_ > 0; $output .= format($_); }

slide-11
SLIDE 11

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

(Design, Code, Test)+

11

Refactor Mercilessly

slide-12
SLIDE 12

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

HW #14: Phase I

12

# Read and process forecasts # Read and process observations # Foreach forecast, compare to observations # Print report

slide-13
SLIDE 13

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

HW #14: Phase II

13

# Read and process forecasts

  • pen(my $forecast_fh, '<', $FORECAST_FILE)
  • r 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

slide-14
SLIDE 14

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

HW #14: Phase III

14

sub read_file() { ... } # Read and process forecasts my @f_lines = read_file($FORECAST_FILE)

  • r 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

slide-15
SLIDE 15

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

HW #14: Phase IV

15

sub read_file() { ... } # Read and process forecasts my @f_lines = read_file($FORECAST_FILE)

  • r 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);

slide-16
SLIDE 16

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Questions?

16

slide-17
SLIDE 17

Cartwright 2012 Summer

Computer Sciences 368 Introduction to Perl

Thank You!

17