Top Banner
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong
17

LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Sep 26, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

LING/C  SC/PSYC  438/538  

Lecture  4  Sandiway  Fong  

Page 2: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Con=nuing  with  Perl  

•  Homework  3:  first  Perl  homework  – due  Sunday  by  midnight  – one  PDF  file,  by  email  to  me  

•  Learn  Perl  – Books…  – Online  resources  •  hLp://learn.perl.org/  •  we  begin  with  ...  •  hLp://perldoc.perl.org/perlintro.html  

Page 3: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  arrays  and  hashes  

•  Scalars:    –  strings,  numbers  (integers,  floa=ng  point  numbers),  references  

•  Arrays:  –  Idea:  list  of  scalars  – Access  by  index:  0,1,2,…  

•  Hash:  –  Like  an  array  except  access  not  through  a  numeric  index  

– Use  user-­‐specified  keys  

$variable  

@variable  

%variable  

different  namespaces:  $apple  @apple  %apple  are  different  data  structures  and  can  co-­‐exist  simultaneously  

Page 4: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Arrays  

•  Arrays:  –  Idea:  list  of  scalars  – Access  by  index:  0,1,2,…  

•  Notes  on  output  •  print  @a      zeroonetwothreefour  •  print  “@a”    zero  one  two  three  four  

controlled  by  system    variable  $”  default  value:  a  space  

•  nega=ve  indices  count  from  the  end  -­‐2..-­‐1  

•  $#array  (index  of  last  element)  •  @array  (can  have  scalar  interpreta=on)  

Page 5: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Arrays  

Forgot  to  men=on  last  =me,  a  special  array  called  @ARGV  •  Geeng  user  input  into  your  program:  

@ARGV  

C  programming  language:  int argc, char *argv[]Shell  script:  $0 $1 $2 ..  ($#)  

Page 6: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Hashes  

Page 7: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Hashes  •  Notes  on  arrays  and  hashes  

–  arrays  are  indexed  from  0,1,2,3…  –  hashes  are  like  arrays  with  user-­‐defined  indexing      (aka  associa=ve  array  or  hash  table)  

–  ini=aliza=on      (use  list  nota=on  (or  shortcut):  round  brackets  and  commas)  •  @a  =  (“zero”,  “one”,  “two”,  “three”,  “four”);  •  %h  =  (“zero”,  0,  “one”,  1,  “two”,  2,  “three”,    3,  “four”,  4);          (key/value  pairs)  

–  access  to  individual  elements      (square  brackets  vs.  curly  braces)  •  $a[1]      “one”  •  $h{zero}    0  

Shortcut:  

Page 8: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Hashes  

•  Notes  on  arrays  and  hashes  – output  

•  print @a  zeroonetwothreefour  •  print "@a"    zero  one  two  three  four  •  print %h    three3one1zero0two2four4                  (note:  different  order)  •  print "%h"    %h    (literal,  no  interpola=on  done)  

Page 9: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

More  on  Hash  tables  

•  Example:  

•  Output:  

Unique  key  constraint:  

Page 10: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

More  on  Hash  tables  

•  Last  =me:    – Exercise:  try  to  write  the  code  for  looking  up  keys  based  off  a  value    

– Anyone  try  it?  

•  An  opportunity  to  use  a  loop  •  Extras:  

 last    @ARGV  

Page 11: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  arrays  

•  List  opera=ons:  

Page 12: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Arrays  •  Last  =me:  

–  by  default  sort  works  according  to  the  ASCII  chart,  character  by  character  

asciitable.com  

Page 13: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Arrays  Numeric  sort?    •  Idea:  to  pass  an  inline  

comparison  func=on  as  parameter…  

•  Note:  fc  (fold  case)  from  Perl  5.16  onwards  only  

Page 14: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Perl  Hashes  

•  Sor=ng  with  a  hash  

sort  byage  keys  %age  

Let  sort  by  fruit,  by  color,  ascending,  descending…  

Page 15: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Implicit  Coercion  

•  Example:    –  the  following  program  prints  Equal!  –  ==  is  the  numeric  equality  comparison  

operator  

my $one_string = “1”;my $one_number = 1;if ($one_string == $one_number) { print “Equal!\n”} else { print “Different!\n”}

•  Example:  –  the  following  program  prints  

 3  is  my  number  –  .  is  the  string  concatena=on  operator  

my @a = qw(one, two, three);my $string = @a.“ is my number”;print “$string\n”;

Perl  features  implicit  coercion  of  data  types    

Page 16: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Implicit  Coercion  

•  print  “4”  *  4  16  •  (strange  behavior  if  you’re  coming  from  Python)  •  print  “4”  x  4    (“x”  is  the  repeGGon  operator)  4444  •  @a  =  (4)  x  4    (in  list  context)  (4,  4,  4,  4)  

Page 17: LING/C’SC/PSYC’438/538’ - University of Arizonasandiway/ling538-15/lecture4.pdf · Homework’3’’ Check’your’understanding…’ Mostfrequentnames’(2000’US’Census)’

Homework  3    Check  your  understanding…  Most  frequent  names  (2000  US  Census)  •  Smith  2376207  •  Johnson  1857160  •  Williams  1534042  •  Brown  1380145  •  Jones  1362755  •  Miller  1127803  •  Davis  1072335  •  Garcia  858289  •  Rodriguez  804240  •  Wilson  783051    •  Mar=nez  775072  •  Anderson  762394  •  Taylor  720370  •  Thomas  710696  •  Hernandez  706372  •  Moore  698671    •  Mar=n  672711  •  Jackson  666125  •  Thompson  644368  •  White      639515  

1.  Put  them  in  a  hash  2.  Sort  by  name,  sort  by  frequency  3.  ascending  and  descending  4. Write  code  to  count  the  number  of  

names  with  frequency  >  some  number,  e.g.  1,000,000  –  supplied  on  the  command  line  

5.  perl  prnames.perl  name|frequency  ascending|descending  

6.  perl  prnames.perl  count  number    •  i.e.  computes  item  4.  

7.  Print  out  the  results  (names…)