perl r and sql for the very beginners christoph rau, calvin pan and yehudit hasin dec 2011

23
Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Upload: jonah-walters

Post on 22-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Perl R and SQL for the very beginners

Christoph Rau, Calvin Pan and Yehudit HasinDec 2011

Page 2: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

What we do and do not intend to teach

Get you started as painlessly as possible

Make you able to write simple, usable and sustainable code for yourself

Make you comfortable to expand your knowledge on your own

Give you the right address(es) for asking questions

DO DO NOT

Make you change career to a programmer

Replace self – learning process

Make you an expert

Page 3: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Dates

• 12/8 – Introductions + Perl1 - Yehudit• 12/15 – Perl 2 – Yehudit• 12/22-12/29 – NO MEETINGS• 1/5 – Perl 3 – Yehudit• 1/12 – Perl 4 – Yehudit• 1/19 – R 1 - Christoph• 1/26 – R2 - Christoph• 2/2 – R3 – Christoph• 2/9 – SQL1 - Calvin• 2/16 –SQL2 - Calvin

Page 4: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

http://xkcd.com/519/

Page 5: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Today

• Introduction– Why Perl/R/SQL?– What is a programing language?– GPP – good programming practice

• Perl 1– My first program– Data types– Data organization– Simple operators

Page 6: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Why Perl/R/SQL and not Excel? Flexibility

Speed

Reproducibility

✗ Responsibility

✗ More work

✗ Safety

Page 7: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Why Perl & R & SQL?

Perl

R SQL

Manipulation of LARGE data filesText patternsFast

Efficient data storage and retrievalStatistical analysisGraphing capabilitiesAnalysis packages

Page 8: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

What is “programming” and why it is a language?

“Language” – The words, their pronunciation, and the methods of combining them used and understood by a community

A body of words and the systems for their use common to a people who are of the same community or nation, the same geographical area, or the same cultural tradition: the two languages of Belgium; a Bantu language; the French language; the Yiddish language.

The system of linguistic signs or symbols considered in the abstract.

Any set or system of such symbols as used in a more or less uniform fashion by a number of people, who are thus enabled to communicate intelligibly with one another.

Any system of formalized symbols, signs, sounds, gestures, or the like used or conceived as a means of communicating thought, emotion, etc.

Page 9: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Adapt your thinking

• Personal interpretation

• Associative thinking

• Initiative

• Learning from past experience

Computer does EXACTLY and EVERYTHING you tell it to

Page 10: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

What is wrong with that program?

“I would like you to clean my desktop”

Clean (desktop);

Page 11: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

What is wrong with that program?• “Go to desktop folder”

• “For each file examine if it has the words “data”, when it was last opened and size”

• If opened more than a year ago and is smaller than 1Mb and does not contain the word “data” – delete.

Page 12: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Why Perl/R/SQL and not Excel? Flexibility

Speed

Reproducibility

✗ Responsibility

✗ More work

✗ Safety

Page 13: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Good Programming Practice or super fast safety course

Page 14: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

When you cut out the middle man

Page 15: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Good Programming Practice

Have a dedicated folder to optimize your scripts

Have a small file, with similar structure to the data file, to test your script (usually just get sample rows).

Give meaningful names to scripts, variables, files etc…

Document your code

Backup, backup………really BACKUP

Good Bad and ugly

✗ Run scripts in any directory higher than Desktop

✗ Optimize your script on a REAL data file

✗ Use “temp”, “x”, “David1” or similar for naming

✗ Assume that your script is perfect

Page 16: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Beginning Perl Chapter1

Page 17: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Perl – “Beginning Perl” (Simon Cozens, Peter Wainwright)

ResourcesThe main perl websitehttp://www.perl.org/

The book I use as scaffold…/books/beginning-perl/

Very good documentation for all perl functionshttp://perldoc.perl.org/

The best forum about perl for all levelshttp://www.perlmonks.org/

For modules and their documentation:http://www.cpan.org/

Page 18: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

What I hope to cover

• Data types• Data structures – variable, array, hash• Loops and conditional statements – “if”, “for”,

“foreach”, “while”, “unless” etc…..• Open, read write and close files• Functions – modules, subroutines• Regular expressions

Page 19: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Perl scripts are simple text files

#!/usr/bin/perluse strict;use warnings;

print "Hello!!!! :)))";

Get a free editor – the one you will like better Macs: Text Wrangler (simpler) or Komodo…. Windows: Padre, Komodo…..

Page 20: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

#!/usr/bin/perl –w use strict ;use warnings ;

print "Hello!!!! :)))\n” ;

Tells unix where perl resides

End of statement

Quotations mean it is a string

Keyword (a word that is in perl vocabulary, in this case function)

Tells perl to be more verbal about mistakes in code

Page 21: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011

Perl - types of Data

Data can be a number or a string

Data can be organized in scalar – a single value list – can hold multiple values

Perl special signs:$xx= variable – can hold any one value (scalar)@xx= array – can hold multiple values (list).

Page 22: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011
Page 23: Perl R and SQL for the very beginners Christoph Rau, Calvin Pan and Yehudit Hasin Dec 2011