Foreach example - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Foreach example

Description:

Example: Êrcolor = ('buick', 'yellow'); $mycolor = $carcolor{ 'buick' ... open(FILEHANDLE, 'filename'); 'filename' 'filename' ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 26
Provided by: csN4
Learn more at: https://cs.nyu.edu
Category:
Tags: buick | example | foreach | open

less

Transcript and Presenter's Notes

Title: Foreach example


1
Foreach example
  • Example
  • _at_numbers ( 3, 5, 7, 9)
  • foreach one (_at_numbers )
  • one3
  • _at_numbers is now (9,15,21,27)

2
PERL HASHES
  • A hash is a collection of scalar data with
    individual elements selected by some index.
  • Index values are arbitrary scalars called keys
    They are used to retrieve values from the array.
  • Elements of a hash have no special order
  • A hash is denoted by "" sign
  • Elements of a hash are referenced by
    hashnamekey

3
HASH Examples
  • Example carcolor ("buick", "yellow")
  • mycolor carcolor "buick"
  • mycolor is now "yellow"
  • copy original copy from original to
    copy

4
HASH FUNCTIONS
  • keys functions
  • keys(hashname) yields a list of the current
    keys in the hash hashname.
  • Example keys(hashname) keys hashname
  • once for each key of fred
  • foreach key (keys (fred))
  • print "at key we have fredkey \n"
  • show key and value

5
HASH FUNCTIONS (CONT')
  • values function
  • values(hashname) yields a list of the current
    valuesof hashname in the same order as keys
    returned by keys(hashname)
  • lastname ("barney", "flinstone", "gerry",
    "smith")
  • _at_lastname values(lastname) grab the
    values

6
HASH FUNCTIONS (each)
  • each function
  • each(hashname) returns a key-value pair as a
    two element list.
  • Used to iterate over an entire hash (examining
    every element of ).
  • Example
  • while ((first, last)) each(lastname))
  • print "the last name of first is last\n"

7
Hash function delete
  • removes hash elements, takes a hash reference as
    argument
  • delete lastname"barney" lastname has only
    one key-value pair now.

8
CONTROL STRUCTURES
  • Perl supports "if", "for" while" similar than
    those in C.
  • "foreach" constructs is from the C shell
  • foreach example
  • If the list we are iterating over is made of real
    variables Rather than some functions returning a
    list value,
  • Then the variable being used for iteration is in
    fact An alias for each variable in the list
    instead of being A merely copy of the values

9
BASIC I/O
  • Input from STDIN
  • Perl uses the variable _ to contain the line
    read from STDIN.
  • a ltSTDINgt reads the next line
  • _at_a ltSTDINgt reads all lines until control D
  • typically
  • while (defined (line ltSTDINgt)
  • process line here
  • when no more lines read. ltSTDINgt returns undef.

10
using the diamond operator ltgt
  • ltgt operates like ltSTDINgt, but gets data from
    file or files
  • specified on the command line that invoked the
    PERL program.
  • ltgt looks at the _at_ARGV array
  • !/usr/bin/perl
  • while (ltgt)
  • print _

11
Output to STDOUT
  • print for normal output
  • printf for formatted output

12
REGULAR EXPRESSIONS
  • PERL supports the same regular expressions as in
    SED.
  • match operator
  • It takes a regular expression operator on the
    right side and changes the target of the operator
    to some value.
  • The target of the operator can be any
    expression that yields some scalar string value.
  • Example if (ltSTDINgt /yY/)
  • print" what can I do for you? "

13
Regular expressions split function
  • split function takes a regular expression and a
    string, and looks for all occurrences of the
    regular expression withinthat string.
  • Parts of the string that don't match the regular
    expression are returned in sequence as a list of
    values

14
Example of split function
  • line "merlyn118120Randal/home/merlyn/usr/
    bin/perl"
  • _at_fields split(//, line) split line,
    using as delimiter
  • now _at_fields is ("merlyn,"", "118", "120",
    "Randal",
  • "/home/merlyn", "/usr/bin/perl")

15
join function
  • takes a list of values and glues them together
    with a glue string between each list element.
  • Example outline join("", _at_fields)

16
PERL FUNCTIONS
  • Defining a user function
  • Sub subname
  • Statement_1
  • Statement_2
  • Statement_3
  • return value is the value of the return statement
    or of the last expression evaluated in the
    subroutine.

17
Function Arguments
  • Subroutine invocation is followed by a list
    within parenthesis
  • Causing the list to be automatically assigned to
    a special variable named _at__ for the duration of
    the subroutine.
  • _0 is the 1st element of the _at__ array
  • _at__ variable is private to the subroutine.

18
Function private variables
  • using the my operator
  • sub add
  • my (sum) make sum a local variable
  • foreach _ ( _at__)
  • sum _ add each element
  • return sum last expression evaluated

19
Functions Semi-private variables
  • semi-private variables using local
  • local variables are visible to functions called
    from within the block in which those variables
    are declared.

20
FILEHANDLES
  • Recommendation use all uppercase letters in your
    filehandles .
  • 3 files handles, STDIN, STDOUT, STDERR for
    standard in, out and error.
  • open(FILEHANDLE, "filename")
  • gt"filename"
  • gtgt "filename
  • die equivalent to "open that file or die."
  • open(FILEHANDLE,gt"filename")
  • die "Sorry could not create filename\n"
  • Perl provides -op file tests just like the shells.

21
Perl modules
  • TBD

22
USER DATABASE MANIPULATION
  • Most UNIX systems have a standard library called
    DBM,
  • Which allows programs to store a collection of
    key-value pairs
  • Into a pair of disk files.
  • In Perl, a hash may be associated with a DBM
    through a process
  • Similar to opening a file.
  • dbmopen function associates a DBM database with a
    DBM array

23
Database interface example
  • dbmopen(ARRAYNAME, "dbmfilename", mode)
  • dbmopen(FRED, ."mydatabase", 0644)
  • delete FRED"barney"
  • while ((key, value) each(FRED))
  • print "key has value of value\n"

24
perl debugging
  • perl -d
  • h print out a help message
  • T stack trace
  • s insgle step
  • n next
  • f finish
  • c continue
  • q quit
  • D delete all breakpoints

25
SYSTEM CALLS
  • Perl provides an interface to many UNIX system
    calls.
  • Interface is via Perl functions, not directly
    through the system call library.
  • The interface use is dependent on the
    implementation and version of Perl being used.
Write a Comment
User Comments (0)
About PowerShow.com