SlideShare a Scribd company logo
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; 
Ad

More Related Content

What's hot (20)

Perl6 in-production
Perl6 in-productionPerl6 in-production
Perl6 in-production
Andrew Shitov
 
The Perl6 Type System
The Perl6 Type SystemThe Perl6 Type System
The Perl6 Type System
abrummett
 
The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
Andrew Shitov
 
Adventures in Optimization
Adventures in OptimizationAdventures in Optimization
Adventures in Optimization
David Golden
 
Working with text, Regular expressions
Working with text, Regular expressionsWorking with text, Regular expressions
Working with text, Regular expressions
Krasimir Berov (Красимир Беров)
 
Introduction to Perl - Day 1
Introduction to Perl - Day 1Introduction to Perl - Day 1
Introduction to Perl - Day 1
Dave Cross
 
Perl Xpath Lightning Talk
Perl Xpath Lightning TalkPerl Xpath Lightning Talk
Perl Xpath Lightning Talk
ddn123456
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
Neatly folding-a-tree
Neatly folding-a-treeNeatly folding-a-tree
Neatly folding-a-tree
Workhorse Computing
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
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
Ricardo Signes
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 
Perl Scripting
Perl ScriptingPerl Scripting
Perl Scripting
Varadharajan Mukundan
 
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)
Michael Schwern
 
Php Basic
Php BasicPhp Basic
Php Basic
Md. Sirajus Salayhin
 
Hashes
HashesHashes
Hashes
Krasimir Berov (Красимир Беров)
 
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
Workhorse Computing
 
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.
Workhorse Computing
 
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)
Pamela Fox
 
Top 10 php classic traps
Top 10 php classic trapsTop 10 php classic traps
Top 10 php classic traps
Damien Seguy
 

Similar to ABC of Perl programming (20)

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Sway Wang
 
Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB Introduction à CoffeeScript pour ParisRB
Introduction à CoffeeScript pour ParisRB
jhchabran
 
Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01Barcelona.pm Curs1211 sess01
Barcelona.pm Curs1211 sess01
Javier Arturo Rodríguez
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
perl-pocket
perl-pocketperl-pocket
perl-pocket
tutorialsruby
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
Hugo 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 mongers
brian d foy
 
wget.pl
wget.plwget.pl
wget.pl
Yasuhiro Onishi
 
Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)Learning Perl 6 (NPW 2007)
Learning Perl 6 (NPW 2007)
brian d foy
 
First steps in PERL
First steps in PERLFirst steps in PERL
First steps in PERL
Brahma Killampalli
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
Henry Osborne
 
Learning Perl 6
Learning Perl 6 Learning Perl 6
Learning Perl 6
brian d foy
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
NBACriteria2SICET
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
brian 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 Functions
David Golden
 
Scripting3
Scripting3Scripting3
Scripting3
Nao 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
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
Sway 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 PHPersistence
Hugo 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 mongers
brian 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 & Arrays
Henry 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 Tie
brian 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 Functions
David Golden
 
Scripting3
Scripting3Scripting3
Scripting3
Nao 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
 
Ad

Recently uploaded (20)

TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Search Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo SlidesSearch Matching Applicants in Odoo 18 - Odoo Slides
Search Matching Applicants in Odoo 18 - Odoo Slides
Celine George
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Ad

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; 
  翻译: