SlideShare uma empresa Scribd logo
1 de 27
Perl ABC                        Part I




               David Young
           yangboh@cn.ibm.com
                Jan. 2011
Perl ABC         Data Structure

Data Structure
  Scalar
  List
  Hash
  Reference
  Filehandle
  Function
Perl ABC                           Data Structure

Scalar
  A number
  A string
  A reference
List
  A list is ordered scalar data.
Hash
  Associative arrays
Perl ABC                           Data Structure

List examples
  A list is ordered scalar data.
  #+begin_src perl 
      @a = ("fred","barney","betty","wilma"); # ugh!
      @a = qw(fred barney betty wilma);       # better!
      @a = qw(
          fred
          barney
          betty
          wilma
      );          # same thing                      
  #+end_src
Perl ABC                    Data Structure

Hash examples
  "associative arrays"

  #+begin_src perl          #+begin_src perl
  %words = (                %words = qw(
      fred   => "camel",        fred   camel
      barney => "llama",        barney llama
      betty  => "alpaca",       betty  alpaca
      wilma  => "alpaca",       wilma  alpaca
  );                        );
  #+end_src                 #+end_src
Perl ABC                     Data Structure

Hash
  continue …
  #+begin_src perl 
     @words = qw(
         fred       camel
         barney     llama
         betty      alpaca
         wilma      alpaca
     );
     %words = @words;
  #+end_src
Perl ABC                         Data Structure

Special Things – Nested List
  There is NOT anythig like list of lists

  #+begin_src perl 
     @a = qw(fred barney betty wilma); 
     @b = ("tom", @a, "jerry");  # be careful


     # what you actually get is ­­ 
     @b = qw(tom fred barney betty wilma jerry); 
  #+end_src
Perl ABC                           Data Structure

Special Things – Nested List
  But … there is nested list in the real world
  What you really mean is
  #+begin_src perl 
     @a = qw(fred barney betty wilma); 
     @b = ("tom", [ @a ], "jerry");    
     @b = ("tom", @a, "jerry");
  #+end_src
  #+begin_src perl 
     $c = [ @a ];  $c = @a;
     @b = ("tom", $c, "jerry");     
  #+end_src
Perl ABC                          Data Structure

Special Things – Nested Hash
  There is nested hash in the real world
  #+begin_src perl 
                                      $words_nest{ mash } = {
                                          captain  => "pierce",
  %words_nest = (
                                          major    => "burns",
      fred    => "camel",
      barney  => "llama",                 corporal => "radar",
                                      };
      betty   => "alpaca",
      wilma   => "alpaca",
      jetsons => {
          husband   => "george",
          wife      => "jane",
          "his boy" => "elroy",  # Key quotes needed.
      },
  );

  #+end_src
Perl ABC               Data Access

Data Access
  Access Scalar Data
  Access List Data
  Access Hash Data
Perl ABC               Data Access

Data Access
  Access Scalar Data
  Access List Data
  Access Hash Data
Perl ABC                                  Data Access

Scalar
  $fred   = "pay"; $fredday = "wrong!";
  $barney = "It's $fredday";          
                    # not payday, but "It's wrong!"
  $barney = "It's ${fred}day";        
                    # now, $barney gets "It's payday"
  $barney2 = "It's $fred"."day";      
                    # another way to do it
  $barney3 = "It's " . $fred . "day"; 
                    # and another way
Perl ABC               Data Access

Data Access
  Access Scalar Data
  Access List Data
  Access Hash Data
Perl ABC                              Data Access

List -- access individully
  #+begin_src perl 
     @a = qw(fred barney betty wilma); 
     @b = ("tom", [ @a ], "jerry");    
  #+end_src
  #+begin_src perl 
     $c = @a;                       # $c = 4;
     $c = $b[0];                    # $c = tom


  #+end_src
Perl ABC                              Data Access

List -- slicing access
  #+begin_src perl 
     @a = qw(fred barney betty wilma); 
     @b = ("tom", [ @a ], "jerry");    
  #+end_src
  #+begin_src perl 
     @c = @a;                    # list copy
     ($aa, $ab, $ac @c) = @a;    # @c = qw(wilma);
     @c = @a[1,2,3];
                # @c = qw(barney betty wilma);
     @c = @a[1..3];              # same thing  
     @a[1,2] = @a[2,1];          # switch value
  #+end_src
Perl ABC                   Data Access

List – access as a whole
  foreach
  map
  grep
Perl ABC                               Data Access

List – access as a whole
  foreach
  #+begin_src perl
  @a = (3,5,7,9);
  foreach $one (@a) {
      $one *= 3;
  }


  # @a is now (9,15,21,27)
  Notice how altering $one in fact altered each element of @a. 
    This is a feature, not a bug.
Perl ABC                               Data Access

List – access as a whole
  map
  @a = (3,5,7,9);
  @b = map { $_ * 3 } @a;            # @b is now (9,15,21,27)
  @c = map { $_ > 5 } @a;            # @c is now (,1,1) 



  grep
  @a = (3,5,7,9);
  @c = grep { $_ > 5 } @a;           # @c is now (7,9)
  @c = grep { $_ > 5 ? $_ : ()} @a;  # equivalent as map
Perl ABC                               Data Access

List – access as a whole
  map and equivalent foreach
  @a = (3,5,7,9);
  @b = map { $_ * 3 } @a;     # @b is now (9,15,21,27)


  # equivalent foreach 
  foreach my $a (@a) {
      push @b, $a * 3;        # did not return values
  }
Perl ABC                              Data Access

List – access as a whole
                          sub time3 { 
  map and equivalent foreach
                             my $num = shift; 
  @a = (3,5,7,9);                   return $num * 3
                                 }
  @b = map { $_ * 3 } @a;     
                                 $func = sub { 
                                    my $num = shift; 
                                    return $num * 3
                                 }

  # equivalents                     sub my_map {
  @b = map &time3($_) @a;            my ($func, $data) = @_;
  @b = map &$func($_) @a;            foreach $a (@$data) {
  @b = my_map &time3, @a;             push @b, &$func($a); 
  @b = my_map $func, @a;            }
                                     return @b;
                                    }
Perl ABC                                 Data Access

Hash -- access individully
  #+begin_src perl 
  %words = (
      fred   => "camel",
      barney => "llama",
      betty  => "alpaca",
      wilma  => "alpaca",
  );
  #+end_src

  #+begin_src perl
   
  $c = $words{"fred"};   # $c = camel 
  $d = "barney";
  $e = $words{$d};       # $e = llama

  #+end_src
Perl ABC                          Data Access

Hash -- access as a whole
#+begin_src perl        #+begin_src perl
%words = (                 @key_list = keys(%words);
  fred   => "camel",       @value_list = values(%words);
  barney => "llama",    #+end_src
  betty  => "alpaca",
  wilma  => "alpaca",   #+begin_src perl
);                      foreach $key (keys(%words){
#+end_src                    print $words{$key}, "n";
                        }
                        #+end_src


                        #+begin_src perl
                        foreach $value (values(%words){
                             print $value, "n";
                        }
                        #+end_src
Perl ABC                               Data Access

List – access nested elements
  #+begin_src perl 
      @a = qw(fred barney betty wilma); 
      @b = ("tom", [ @a ], "jerry");    
      @b = ("tom", @a, "jerry");
  #+end_src



  #+begin_src perl
      $a = $b[1];              # $a = [ @a ]  
      $c = $b[1]­>[1];         # $c = barney
      $c = @b[1][1];           # same thing
      $c = @$a­>[1];           # same thing
      $c = ${$a}[1];           # same thing
  #+end_src
Perl ABC                               Data Access

Hash – access nested elements
  %h_nest = (
      fred    => "camel",
      barney  => "llama",
      betty   => "alpaca",
      wilma   => "alpaca",
      jetsons => {
          husband   => "george",
          wife      => "jane",
          "his boy" => "elroy",
      },
  );

  $c = $h_nest{"jetsons"}{"wife"};  # $c = jane
  $j = "jetsons";  $w = "wife"; 
  $c = $h_nest{$j}{$w};             # same thing

  $jet = $h_nest("jetsons"};        # $jet has a hash
  $d = $jet{"husband"};             # $d = george
Perl ABC                                Data Access

Reference
# Create some variables
$a      = "mama mia";
@array  = (10, 20);
%hash   = ("laurel" => "hardy", "nick" =>  "nora");


# Now create references to them
$r_a     = $a;      # $ra now "refers" to (points to) $a
$r_array = @array;
$r_hash  = %hash;
Perl ABC                               Data Access

Access Reference Data
  # Now create references to them
  $r_a     = $a;      # $ra now "refers" to (points to) $a
  $r_array = @array;
  $r_hash  = %hash;
  # Now access the referenced data
  $r_a;         # the address
  $$r_a;        # the $a;       "mama mia";
  $r_array      # the address
  @$r_array     # the array     (10, 20);
  @$r_array[1]  # the element   20; 
Perl ABC                               Data Access

Access Reference Data
  # Now create references to them
  $r_a     = $a;      # $ra now "refers" to (points to) $a
  $r_array = @array;
  $r_hash  = %hash;
  # Now access the referenced data
  $r_a;         # the address
  $$r_a;        # the $a;       "mama mia";
  $r_array      # the address
  @$r_array     # the array     (10, 20);
  @$r_array[1]  # the element   20; 

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
 
The Perl6 Type System
The Perl6 Type SystemThe Perl6 Type System
The Perl6 Type System
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Perl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally InsanePerl 5.10 for People Who Aren't Totally Insane
Perl 5.10 for People Who Aren't Totally Insane
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
 
Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)Simple Ways To Be A Better Programmer (OSCON 2007)
Simple Ways To Be A Better Programmer (OSCON 2007)
 
Php Basic
Php BasicPhp Basic
Php Basic
 
Hashes
HashesHashes
Hashes
 
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
Neatly Hashing a Tree: FP tree-fold in Perl5 & Perl6
 
Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.Perl6 Regexen: Reduce the line noise in your code.
Perl6 Regexen: Reduce the line noise in your code.
 
Writing Apps the Google-y Way (Brisbane)
Writing Apps the Google-y Way (Brisbane)Writing Apps the Google-y Way (Brisbane)
Writing Apps the Google-y Way (Brisbane)
 
Top 10 php classic traps
Top 10 php classic trapsTop 10 php classic traps
Top 10 php classic traps
 

Semelhante a ABC of Perl programming

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to PerlSway Wang
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB jhchabran
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistenceHugo Hamon
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongersbrian d foy
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)brian d foy
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & ArraysHenry Osborne
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6 brian d foy
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tiebrian d foy
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsDavid Golden
 
Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...Viktor Turskyi
 
Topological indices (t is) of the graphs to seek qsar models of proteins com...
Topological indices (t is) of the graphs  to seek qsar models of proteins com...Topological indices (t is) of the graphs  to seek qsar models of proteins com...
Topological indices (t is) of the graphs to seek qsar models of proteins com...Jitendra Kumar Gupta
 

Semelhante a ABC of Perl programming (20)

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
wget.pl
wget.plwget.pl
wget.pl
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
 
First steps in PERL
First steps in PERLFirst steps in PERL
First steps in PERL
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
 
Taking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order FunctionsTaking Perl to Eleven with Higher-Order Functions
Taking Perl to Eleven with Higher-Order Functions
 
Scripting3
Scripting3Scripting3
Scripting3
 
Maybe you do not know that ...
Maybe you do not know that ...Maybe you do not know that ...
Maybe you do not know that ...
 
Topological indices (t is) of the graphs to seek qsar models of proteins com...
Topological indices (t is) of the graphs  to seek qsar models of proteins com...Topological indices (t is) of the graphs  to seek qsar models of proteins com...
Topological indices (t is) of the graphs to seek qsar models of proteins com...
 

Último

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Último (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

ABC of Perl programming

  • 1. Perl ABC Part I David Young yangboh@cn.ibm.com Jan. 2011
  • 2. Perl ABC Data Structure Data Structure Scalar List Hash Reference Filehandle Function
  • 3. Perl ABC Data Structure Scalar A number A string A reference List A list is ordered scalar data. Hash Associative arrays
  • 4. Perl ABC Data Structure List examples A list is ordered scalar data. #+begin_src perl  @a = ("fred","barney","betty","wilma"); # ugh! @a = qw(fred barney betty wilma);       # better! @a = qw(     fred     barney     betty     wilma );          # same thing                       #+end_src
  • 5. Perl ABC Data Structure Hash examples "associative arrays" #+begin_src perl  #+begin_src perl %words = ( %words = qw(     fred   => "camel",     fred   camel     barney => "llama",     barney llama     betty  => "alpaca",     betty  alpaca     wilma  => "alpaca",     wilma  alpaca ); ); #+end_src #+end_src
  • 6. Perl ABC Data Structure Hash continue … #+begin_src perl  @words = qw(     fred       camel     barney     llama     betty      alpaca     wilma      alpaca ); %words = @words; #+end_src
  • 7. Perl ABC Data Structure Special Things – Nested List There is NOT anythig like list of lists #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", @a, "jerry");  # be careful # what you actually get is ­­  @b = qw(tom fred barney betty wilma jerry);  #+end_src
  • 8. Perl ABC Data Structure Special Things – Nested List But … there is nested list in the real world What you really mean is #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     @b = ("tom", @a, "jerry"); #+end_src #+begin_src perl  $c = [ @a ];  $c = @a; @b = ("tom", $c, "jerry");      #+end_src
  • 9. Perl ABC Data Structure Special Things – Nested Hash There is nested hash in the real world #+begin_src perl  $words_nest{ mash } = {     captain  => "pierce", %words_nest = (     major    => "burns",     fred    => "camel",     barney  => "llama",     corporal => "radar", };     betty   => "alpaca",     wilma   => "alpaca",     jetsons => {         husband   => "george",         wife      => "jane",         "his boy" => "elroy",  # Key quotes needed.     }, ); #+end_src
  • 10. Perl ABC Data Access Data Access Access Scalar Data Access List Data Access Hash Data
  • 11. Perl ABC Data Access Data Access Access Scalar Data Access List Data Access Hash Data
  • 12. Perl ABC Data Access Scalar $fred   = "pay"; $fredday = "wrong!"; $barney = "It's $fredday";                             # not payday, but "It's wrong!" $barney = "It's ${fred}day";                           # now, $barney gets "It's payday" $barney2 = "It's $fred"."day";                         # another way to do it $barney3 = "It's " . $fred . "day";                    # and another way
  • 13. Perl ABC Data Access Data Access Access Scalar Data Access List Data Access Hash Data
  • 14. Perl ABC Data Access List -- access individully #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     #+end_src #+begin_src perl  $c = @a;                       # $c = 4; $c = $b[0];                    # $c = tom #+end_src
  • 15. Perl ABC Data Access List -- slicing access #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     #+end_src #+begin_src perl  @c = @a;                    # list copy ($aa, $ab, $ac @c) = @a;    # @c = qw(wilma); @c = @a[1,2,3];            # @c = qw(barney betty wilma); @c = @a[1..3];              # same thing   @a[1,2] = @a[2,1];          # switch value #+end_src
  • 16. Perl ABC Data Access List – access as a whole foreach map grep
  • 17. Perl ABC Data Access List – access as a whole foreach #+begin_src perl @a = (3,5,7,9); foreach $one (@a) {     $one *= 3; } # @a is now (9,15,21,27) Notice how altering $one in fact altered each element of @a.  This is a feature, not a bug.
  • 18. Perl ABC Data Access List – access as a whole map @a = (3,5,7,9); @b = map { $_ * 3 } @a;            # @b is now (9,15,21,27) @c = map { $_ > 5 } @a;            # @c is now (,1,1)  grep @a = (3,5,7,9); @c = grep { $_ > 5 } @a;           # @c is now (7,9) @c = grep { $_ > 5 ? $_ : ()} @a;  # equivalent as map
  • 19. Perl ABC Data Access List – access as a whole map and equivalent foreach @a = (3,5,7,9); @b = map { $_ * 3 } @a;     # @b is now (9,15,21,27) # equivalent foreach  foreach my $a (@a) {     push @b, $a * 3;        # did not return values }
  • 20. Perl ABC Data Access List – access as a whole sub time3 {  map and equivalent foreach    my $num = shift;  @a = (3,5,7,9);    return $num * 3 } @b = map { $_ * 3 } @a;      $func = sub {     my $num = shift;     return $num * 3 } # equivalents  sub my_map { @b = map &time3($_) @a;  my ($func, $data) = @_; @b = map &$func($_) @a;  foreach $a (@$data) { @b = my_map &time3, @a;     push @b, &$func($a);  @b = my_map $func, @a;  }  return @b; }
  • 21. Perl ABC Data Access Hash -- access individully #+begin_src perl  %words = (     fred   => "camel",     barney => "llama",     betty  => "alpaca",     wilma  => "alpaca", ); #+end_src #+begin_src perl   $c = $words{"fred"};   # $c = camel  $d = "barney"; $e = $words{$d};       # $e = llama #+end_src
  • 22. Perl ABC Data Access Hash -- access as a whole #+begin_src perl  #+begin_src perl %words = (    @key_list = keys(%words);   fred   => "camel",    @value_list = values(%words);   barney => "llama", #+end_src   betty  => "alpaca",   wilma  => "alpaca", #+begin_src perl ); foreach $key (keys(%words){ #+end_src      print $words{$key}, "n"; } #+end_src #+begin_src perl foreach $value (values(%words){      print $value, "n"; } #+end_src
  • 23. Perl ABC Data Access List – access nested elements #+begin_src perl  @a = qw(fred barney betty wilma);  @b = ("tom", [ @a ], "jerry");     @b = ("tom", @a, "jerry"); #+end_src #+begin_src perl $a = $b[1];              # $a = [ @a ]   $c = $b[1]­>[1];         # $c = barney $c = @b[1][1];           # same thing $c = @$a­>[1];           # same thing $c = ${$a}[1];           # same thing #+end_src
  • 24. Perl ABC Data Access Hash – access nested elements %h_nest = (     fred    => "camel",     barney  => "llama",     betty   => "alpaca",     wilma   => "alpaca",     jetsons => {         husband   => "george",         wife      => "jane",         "his boy" => "elroy",     }, ); $c = $h_nest{"jetsons"}{"wife"};  # $c = jane $j = "jetsons";  $w = "wife";  $c = $h_nest{$j}{$w};             # same thing $jet = $h_nest("jetsons"};        # $jet has a hash $d = $jet{"husband"};             # $d = george
  • 25. Perl ABC Data Access Reference # Create some variables $a      = "mama mia"; @array  = (10, 20); %hash   = ("laurel" => "hardy", "nick" =>  "nora"); # Now create references to them $r_a     = $a;      # $ra now "refers" to (points to) $a $r_array = @array; $r_hash  = %hash;
  • 26. Perl ABC Data Access Access Reference Data # Now create references to them $r_a     = $a;      # $ra now "refers" to (points to) $a $r_array = @array; $r_hash  = %hash; # Now access the referenced data $r_a;         # the address $$r_a;        # the $a;       "mama mia"; $r_array      # the address @$r_array     # the array     (10, 20); @$r_array[1]  # the element   20; 
  • 27. Perl ABC Data Access Access Reference Data # Now create references to them $r_a     = $a;      # $ra now "refers" to (points to) $a $r_array = @array; $r_hash  = %hash; # Now access the referenced data $r_a;         # the address $$r_a;        # the $a;       "mama mia"; $r_array      # the address @$r_array     # the array     (10, 20); @$r_array[1]  # the element   20;