Perl Brown Bag            Data Mining              Shaun Griffith              March 20, 200605/13/12        ATI Confident...
Agenda           •Program Structure            •Slinging Strings05/13/12                        2
First ProgramRead a GDF and grab some data05/13/12                        3
Program StructureShebang    •#!/path/to/perl•Unix shell runs this program on the script•Other choices    •DOS:           #...
Program So Far#!/your/perl/here05/13/12                             5
ScripturesAlso known as “strictures”           use strict;           use warnings;Strict•Declare all variables before use ...
Scriptures…Warnings    •isn’t numeric    •undefined valueWarnings print to STDERRby defaultWarnings usuallymean program er...
Program So Far#!/your/perl/hereuse strict;use warnings;05/13/12                             8
VariablesScalars•Hold a single value    •String    •Number    •Reference (like a pointer)•Start with $    •$name = “Fred”;...
Variables…Arrays•Hold more than one value (scalar)•Order is important•Start with @ or $    •@list = (1,2,3,4,5);    •$list...
Variables…Hashes• Pairs of data: key and value• No order – No duplicate keys• Start with % or @ or $           %cost = ( “...
Reading FilesPerl DWIM (Do What I Mean)• To read files listed on the command line:while (<>) { do_something_here; }    • “...
Program So Far#!/your/perl/hereuse strict;use warnings;while (<>){}05/13/12                             13
MatchingTo match barcodes:    m/barcode=(d+)/i;    • m// is the match operator    • barcode= is literal text to match    •...
Program So Far#!/your/perl/hereuse strict;use warnings;while (<>){    if ( m/barcode=(d+)/i )    { print “$1n”; }}05/13/12...
More StuffPass/Fail is on the same line:     m/(PASS|FAIL)/i;           •Vertical bar is “or”Print this out too:     if ( ...
Program So Far#!/your/perl/hereuse strict;use warnings;while (<>){    if ( m/barcode=(d+)/i )    { print “$1t”;           ...
Printing HeadersYou could do this:    print “BarcodetPFn”; # t is tab…but if spacing is important:    printf “%10st%4sn”, ...
Ta-Da!!!#!/your/perl/hereuse strict;use warnings;# headerprintf “%10st%4sn”, “Barcode”, “ PF ”;while (<>){    if ( m/barco...
Questions?Questions on this material?  • Reading files  • Variables  • Matching  • PrintingQuestions on anything else?  • ...
Next Time           Running Perl           Perl Debugger05/13/12                   21
Upcoming SlideShare
Loading in...5
×

Perl Intro 2 First Program

147

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
147
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Perl Intro 2 First Program

  1. 1. Perl Brown Bag Data Mining Shaun Griffith March 20, 200605/13/12 ATI Confidential 1
  2. 2. Agenda •Program Structure •Slinging Strings05/13/12 2
  3. 3. First ProgramRead a GDF and grab some data05/13/12 3
  4. 4. Program StructureShebang •#!/path/to/perl•Unix shell runs this program on the script•Other choices •DOS: #!/this/doesn’t/do/much •Unix – find Perl in your path eval (exit $?0) && eval exec perl -w -S $0 ${1+"$@"}‘ && eval exec perl -w -S $0 $argv:q if 0; # The above invocation finds Perl in the path, # wherever it may be05/13/12 4
  5. 5. Program So Far#!/your/perl/here05/13/12 5
  6. 6. ScripturesAlso known as “strictures” use strict; use warnings;Strict•Declare all variables before use (“vars”)•No symbolic references (“refs”)•Declare all subroutines before use (“subs”)05/13/12 6
  7. 7. Scriptures…Warnings •isn’t numeric •undefined valueWarnings print to STDERRby defaultWarnings usuallymean program errorsor bad data!05/13/12 7
  8. 8. Program So Far#!/your/perl/hereuse strict;use warnings;05/13/12 8
  9. 9. VariablesScalars•Hold a single value •String •Number •Reference (like a pointer)•Start with $ •$name = “Fred”; •$age = 17; •$current_name = $name;05/13/12 9
  10. 10. Variables…Arrays•Hold more than one value (scalar)•Order is important•Start with @ or $ •@list = (1,2,3,4,5); •$list[3] = “Markam”; •@list[11,28] = (“Red”,”Green”);•Size: $size = @list;•Last index: $last = $#list;05/13/12 10
  11. 11. Variables…Hashes• Pairs of data: key and value• No order – No duplicate keys• Start with % or @ or $ %cost = ( “apples” => 0.45, “bananas” => 0.55 ); @cost{@fruit} = ( 0.45, 0.55 ); $cost{apples} = 0.45;• List of keys: @keys = keys %cost;• Size: $fruit = scalar keys %cost;05/13/12 11
  12. 12. Reading FilesPerl DWIM (Do What I Mean)• To read files listed on the command line:while (<>) { do_something_here; } • “<>” is the “diamond” operator • If empty, reads from STDIN (a file “handle”)… • …which defaults to @ARGV • “<>” automatically opens and closes files05/13/12 12
  13. 13. Program So Far#!/your/perl/hereuse strict;use warnings;while (<>){}05/13/12 13
  14. 14. MatchingTo match barcodes: m/barcode=(d+)/i; • m// is the match operator • barcode= is literal text to match •d+ matches one or more digits (0-9) • () captures matches into $1, $2, etc. •/i ignores caseTo print it out: print “$1n”; # n is end of linePutting them together: if ( m/barcode=(d+)/i ) { print “$1n”; }05/13/12 14
  15. 15. Program So Far#!/your/perl/hereuse strict;use warnings;while (<>){ if ( m/barcode=(d+)/i ) { print “$1n”; }}05/13/12 15
  16. 16. More StuffPass/Fail is on the same line: m/(PASS|FAIL)/i; •Vertical bar is “or”Print this out too: if ( m/(PASS|FAIL)/i ) { print “$1n”; }But let’s print all of that on one line: if ( m/barcode=(d+)/i ) { print “$1t”; if ( m/(pass|fail)/i ) { print "$1”; } print "n"; }05/13/12 16
  17. 17. Program So Far#!/your/perl/hereuse strict;use warnings;while (<>){ if ( m/barcode=(d+)/i ) { print “$1t”; if ( m/(pass|fail)/i ) { print "$1”; } print "n"; }}05/13/12 17
  18. 18. Printing HeadersYou could do this: print “BarcodetPFn”; # t is tab…but if spacing is important: printf “%10st%4sn”, “Barcode”, “ PF ”;This is the same printf as C. • %10s • % starts a field • 10 gives the width • s is for strings • d is for integers • e/f/g are for real (floats)Do the same for the other prints if you want…05/13/12 18
  19. 19. Ta-Da!!!#!/your/perl/hereuse strict;use warnings;# headerprintf “%10st%4sn”, “Barcode”, “ PF ”;while (<>){ if ( m/barcode=(d+)/i ) { printf “%10st”, $1; if ( m/(pass|fail)/i ) { printf “%4s”, $1; } print "n"; }}exit; # redundant, but good for debugger05/13/12 19
  20. 20. Questions?Questions on this material? • Reading files • Variables • Matching • PrintingQuestions on anything else? • Reading from more than 1 file? • Substitutions? • Loops? • Subroutines?05/13/12 20
  21. 21. Next Time Running Perl Perl Debugger05/13/12 21
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×