SlideShare ist ein Scribd-Unternehmen logo
1 von 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; 

Weitere ähnliche Inhalte

Was ist angesagt?

The Joy of Smartmatch
The Joy of SmartmatchThe Joy of Smartmatch
The Joy of Smartmatch
Andrew Shitov
 
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
 
Perl.Hacks.On.Vim
Perl.Hacks.On.VimPerl.Hacks.On.Vim
Perl.Hacks.On.Vim
Lin Yo-An
 

Was ist angesagt? (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
 

Ähnlich wie ABC of Perl programming

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
 
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
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
 

Ähnlich wie 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...
 

Kürzlich hochgeladen

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
ssuserdda66b
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Kürzlich hochgeladen (20)

Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).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;