SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Perl Brown Bag


            Data Mining

              Shaun Griffith
              March 20, 2006



05/13/12        ATI Confidential   1
Agenda


           •Program Structure
            •Slinging Strings




05/13/12                        2
First Program
Read a GDF and grab some data




05/13/12                        3
Program Structure
Shebang
    •#!/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 be
05/13/12                                                                   4
Program So Far
#!/your/perl/here




05/13/12                             5
Scriptures
Also 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
Scriptures…
Warnings
    •isn’t numeric
    •undefined value

Warnings print to STDERR
by default

Warnings usually
mean program errors
or bad data!

05/13/12                             7
Program So Far
#!/your/perl/here
use strict;
use warnings;




05/13/12                             8
Variables
Scalars
•Hold a single value
    •String
    •Number
    •Reference (like a pointer)
•Start with $
    •$name = “Fred”;
    •$age       = 17;
    •$current_name = $name;

05/13/12                            9
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
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
Reading Files
Perl 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 files




05/13/12                                              12
Program So Far
#!/your/perl/here
use strict;
use warnings;


while (<>)
{
}




05/13/12                             13
Matching
To 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 case

To print it out:
    print “$1n”; # n is end of line
Putting them together:
    if ( m/barcode=(d+)/i )
    { print “$1n”; }

05/13/12                                      14
Program So Far
#!/your/perl/here
use strict;
use warnings;


while (<>)
{

    if ( m/barcode=(d+)/i )

    { print “$1n”; }
}




05/13/12                             15
More Stuff
Pass/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
Program So Far
#!/your/perl/here
use strict;
use warnings;


while (<>)
{
    if ( m/barcode=(d+)/i )
    { print “$1t”;
           if ( m/(pass|fail)/i )
           { print "$1”; }
           print "n";
    }
}



05/13/12                                  17
Printing Headers
You 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
Ta-Da!!!
#!/your/perl/here

use strict;
use warnings;

# header
printf “%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 debugger




05/13/12                                   19
Questions?
Questions on this material?
  • Reading files
  • Variables
  • Matching
  • Printing

Questions on anything else?
  • Reading from more than 1 file?
  • Substitutions?
  • Loops?
  • Subroutines?




05/13/12                             20
Next Time

           Running Perl
           Perl Debugger




05/13/12                   21

Weitere ähnliche Inhalte

Was ist angesagt?

PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop NotesPamela Fox
 
Dev traning 2016 basics of PHP
Dev traning 2016   basics of PHPDev traning 2016   basics of PHP
Dev traning 2016 basics of PHPSacheen Dhanjie
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...anshkhurana01
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applicationsJoe Jiang
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 
ALFRED - www2013
ALFRED - www2013 ALFRED - www2013
ALFRED - www2013 Disheng Qiu
 

Was ist angesagt? (15)

PHP variables
PHP  variablesPHP  variables
PHP variables
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 
Basic PHP
Basic PHPBasic PHP
Basic PHP
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP Workshop Notes
PHP Workshop NotesPHP Workshop Notes
PHP Workshop Notes
 
Dev traning 2016 basics of PHP
Dev traning 2016   basics of PHPDev traning 2016   basics of PHP
Dev traning 2016 basics of PHP
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
PHP - Introduction to PHP
PHP -  Introduction to PHPPHP -  Introduction to PHP
PHP - Introduction to PHP
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
ALFRED - www2013
ALFRED - www2013 ALFRED - www2013
ALFRED - www2013
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 

Andere mochten auch

Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 SubroutinesShaun Griffith
 
Perl Intro 8 File Handles
Perl Intro 8 File HandlesPerl Intro 8 File Handles
Perl Intro 8 File HandlesShaun Griffith
 
Perl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsPerl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsShaun Griffith
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsShaun Griffith
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Andere mochten auch (8)

Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Perl Intro 7 Subroutines
Perl Intro 7 SubroutinesPerl Intro 7 Subroutines
Perl Intro 7 Subroutines
 
Perl Intro 8 File Handles
Perl Intro 8 File HandlesPerl Intro 8 File Handles
Perl Intro 8 File Handles
 
Perl Intro 6 Ftp
Perl Intro 6 FtpPerl Intro 6 Ftp
Perl Intro 6 Ftp
 
Perl Intro 9 Command Line Arguments
Perl Intro 9 Command Line ArgumentsPerl Intro 9 Command Line Arguments
Perl Intro 9 Command Line Arguments
 
Perl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And SubstitutionsPerl Intro 5 Regex Matches And Substitutions
Perl Intro 5 Regex Matches And Substitutions
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Ähnlich wie Perl Intro 2 First Program

Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Andrea Telatin
 
Lecture 22
Lecture 22Lecture 22
Lecture 22rhshriva
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning PerlDave Cross
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsRoy Zimmer
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1Dave Cross
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learningASIT Education
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl coursepartiernlow
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlDave Cross
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng verQrembiezs Intruder
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionProf. Wim Van Criekinge
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Roy Zimmer
 
Lecture 23
Lecture 23Lecture 23
Lecture 23rhshriva
 

Ähnlich wie Perl Intro 2 First Program (20)

Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
Bioinformatica: Esercizi su Perl, espressioni regolari e altre amenità (BMR G...
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Master perl io_2011
Master perl io_2011Master perl io_2011
Master perl io_2011
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
You Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager NeedsYou Can Do It! Start Using Perl to Handle Your Voyager Needs
You Can Do It! Start Using Perl to Handle Your Voyager Needs
 
Perl Moderno
Perl ModernoPerl Moderno
Perl Moderno
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Learn perl in amc square learning
Learn perl in amc square learningLearn perl in amc square learning
Learn perl in amc square learning
 
Perl courseparti
Perl coursepartiPerl courseparti
Perl courseparti
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Tutorial perl programming basic eng ver
Tutorial perl programming basic eng verTutorial perl programming basic eng ver
Tutorial perl programming basic eng ver
 
Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1Perl Basics for Pentesters Part 1
Perl Basics for Pentesters Part 1
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)Plunging Into Perl While Avoiding the Deep End (mostly)
Plunging Into Perl While Avoiding the Deep End (mostly)
 
Lecture 23
Lecture 23Lecture 23
Lecture 23
 

Perl Intro 2 First Program

  • 1. Perl Brown Bag Data Mining Shaun Griffith March 20, 2006 05/13/12 ATI Confidential 1
  • 2. Agenda •Program Structure •Slinging Strings 05/13/12 2
  • 3. First Program Read a GDF and grab some data 05/13/12 3
  • 4. Program Structure Shebang •#!/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 be 05/13/12 4
  • 6. Scriptures Also 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. Scriptures… Warnings •isn’t numeric •undefined value Warnings print to STDERR by default Warnings usually mean program errors or bad data! 05/13/12 7
  • 8. Program So Far #!/your/perl/here use strict; use warnings; 05/13/12 8
  • 9. Variables Scalars •Hold a single value •String •Number •Reference (like a pointer) •Start with $ •$name = “Fred”; •$age = 17; •$current_name = $name; 05/13/12 9
  • 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. 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. Reading Files Perl 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 files 05/13/12 12
  • 13. Program So Far #!/your/perl/here use strict; use warnings; while (<>) { } 05/13/12 13
  • 14. Matching To 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 case To print it out: print “$1n”; # n is end of line Putting them together: if ( m/barcode=(d+)/i ) { print “$1n”; } 05/13/12 14
  • 15. Program So Far #!/your/perl/here use strict; use warnings; while (<>) { if ( m/barcode=(d+)/i ) { print “$1n”; } } 05/13/12 15
  • 16. More Stuff Pass/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. Program So Far #!/your/perl/here use 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. Printing Headers You 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. Ta-Da!!! #!/your/perl/here use strict; use warnings; # header printf “%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 debugger 05/13/12 19
  • 20. Questions? Questions on this material? • Reading files • Variables • Matching • Printing Questions on anything else? • Reading from more than 1 file? • Substitutions? • Loops? • Subroutines? 05/13/12 20
  • 21. Next Time Running Perl Perl Debugger 05/13/12 21