SlideShare una empresa de 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; 

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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
 

Similar 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
 

Similar 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

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Developmentchesterberbo7
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptxJonalynLegaspi2
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseCeline George
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 

Último (20)

Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Using Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea DevelopmentUsing Grammatical Signals Suitable to Patterns of Idea Development
Using Grammatical Signals Suitable to Patterns of Idea Development
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
week 1 cookery 8 fourth - quarter .pptx
week 1 cookery 8  fourth  -  quarter .pptxweek 1 cookery 8  fourth  -  quarter .pptx
week 1 cookery 8 fourth - quarter .pptx
 
How to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 DatabaseHow to Make a Duplicate of Your Odoo 17 Database
How to Make a Duplicate of Your Odoo 17 Database
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 

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;