Perl: arrays - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Perl: arrays

Description:

Perl: arrays – PowerPoint PPT presentation

Number of Views:71
Avg rating:3.0/5.0
Slides: 32
Provided by: tabr1
Category:
Tags: arrays | perl | yabba

less

Transcript and Presenter's Notes

Title: Perl: arrays


1
Perl arrays
2
  • !/usr/bin/perl -w
  • bind3.pl
  • Here's an example that takes a unix path
    (file)
  • and copies it to anothe variable (filename)
  • Then, we search for one or more of any
    character .
  • followed by a "/" character -- but we have to
    use the
  • escape metacharacter "\" so that we don't end
    the match \/.
  • Finally, we are looking for one or more
    non-white spaces (\S)
  • at the end -- to pull off the the last file
    name "FOUND"
  • path "/home/tabraun/test/bob/FOUND"
  • filename path
  • filename s/.\/(\S)/1/
  • print "filename\n"

3
!/usr/bin/perl randomSeq.pl Don't get too
uptight over this line -- it is just setting a
"seed" for the rand() fuction with a value that
approximates a random number. If you must
know, it takes a prccess ID (), shifts its
bit left 15 times, then add the process ID to the
shifted value, then does an bit-wise XOR ()
with the current time(). print "Enter length
of sequence to generate" length
ltSTDINgt srand(time() ( ( ltlt 15))
) while(length) stay in loop until have
generated enough sequence rand int rand(4)
Interger number between (0-3) inclusive rand
tr/0123/ACTG/ length length-1
decrease loop counter seq seq . rand
keep the nucleotide I just created Since I
am out of the loop, I must be done print
"seq\n"
4
  • Run Example
  • Show process ID

5
Lists and Arrays
  • list ordered collection of scalars
  • array a perl variable that contains a list
  • each element of an array is a separate scalar
    variable
  • values are ordered, from the first to the last
  • elements are indexed by small integers, starting
    at 0

"left-most"
0 1 2 3 4 5
35
12.4
"hello, world"
indices
1.72e30
"bye\n"
69
"right-most"
6
Accessing Elements of an Array
  • fred0 "yabba"
  • fred1 "dabba"
  • fred2 "doo"
  • fred3 4
  • fred3
  • fred1 s/b/B/g
  • fred 32 completely different scalar

7
Creating Elements
  • if you store into an array element that is beyond
    the end of the array, the array is automatically
    extended as needed (no limit as long as there is
    memory available)
  • intervening elements will be undef
  • rocks0 'bedrock'
  • rocks1 'slate'
  • rocks99 'chasm' 3 defined elements, 97
    undef
  • .

8
List Literals
  • shorthand for creating arrays
  • (1,2,3) list of 3 values, 1, 2, 3
  • (1, 2, 3,) same thing, last comma ignored
  • ("fred",4.5) 2 values
  • () empty list
  • (1..100) list of integers, 1 thru 100
  • aka "range operator"
  • (1..5) 1, 2, 3, 4, 5
  • (5..1) empty, only counts up
  • (0, 2..6, 10) (0, 2, 3, 4, 5, 6, 10)
  • (a..b) depends on a and b

9
Another List Shortcut
  • qw -- stands for "quoted words"
  • ("fred", "barney", "betty", "wilma", "dino")
  • same as
  • qw/ fred barney betty wilma dino /
  • same as above with less typing

10
List Assignment
  • list values may be assigned to variables
  • (fred, barney, dino) ("finstone", "rubble",
    undef)
  • can use to swap
  • (fred, barney) (barney, fred)
  • Note you cannot do this in most other
    languages (C, Java, etc)
  • Extra right side values are ignored
  • Extra left side variables will be undef
  • (fred, barney) qw/ flintstone rubble slate /
    slate ignored
  • (wilma, dino) qw/flintstone/ dino gets
    undef

11
List Assignment
  • can build array
  • (r0, r1, r2) qw/ slate granite chalk/
  • one final array shortcut -- _at_
  • _at_rocks qw/ bedrock slate lava/
  • rocks2 s/s/S/
  • _at_empty_array ()
  • _at_list (1..10) 10 elements of 1-10
  • _at_stuff (_at_list, _at_rocks) 13 elements
  • dino "dog"
  • _at_quarry (_at_rocs, "diamond", _at_list, dino)
  • Array names are replaced by the elements in its
    list

12
Copying an Array is Easy
  • _at_copy _at_quarry all elements are copied
  • for(I0IltnumI)
  • copyI quarryI

13
Array Manipulations
  • could add to arrays by storing them into elements
    with new, larger indices
  • however, this defeats the strengths of perl, and
    will actually result in slower code
  • extensive use of indices slows down perl

14
Array as a Stack
  • elements are added/removed from the "right" side
    of the list (portion with highest indices)
  • Syntax
  • push ARRAY, LIST
  • - returns new number of elements in array
  • pop ARRAY
  • - returns right most element of array/list
  • _at_array (5..9) array has
    5,6,7,8,9
  • fred pop(_at_array) fred 9, array
    5,6,7,8
  • barney pop _at_array barney 8, array 5,6,7
  • pop _at_array 7 is discarded,
    array 5, 6

15
  • push (_at_array, 0)
  • push _at_array, 8
  • _at_list (5, 6, 7)
  • push (_at_array, _at_list)

16
An Array as a Queue
  • Elements are added/removed from the left (or
    portion with lowest indices)
  • shift ARRAY
  • - returns left most element
  • unshift ARRAY, list
  • - returns new number of elements in array
  • _at_array qw/ dino fred barn /
  • a shift(_at_array) _at_array fred, barn
  • b shift _at_array _at_array barn
  • shift _at_array _at_array is empty
  • c shift _at_array _at_array still empty, c
    undef
  • unshift(_at_array,5) _at_array now has 5
  • _at_list (1..4)
  • unshift(_at_array,_at_list) _at_array 1,2,3,4,5

17
Mix and Match
  • _at_array (9..13)
  • push(_at_array, 14)
  • unshift(_at_array,8)
  • fourteen pop(_at_array)
  • nine shift _at_array
  • print "array2"

18
Interpolating into Arrays
  • Like scalars, array values my be interpolated
    into double-quoted strings
  • _at_rocks qw/ stone gravel rubble /
  • print "boulder _at_rocks marble\n" inserts spaces
    between elements of array
  • print "rocks1\n" gravel

19
Foreach loop revisited
  • foreach SCALAR (ARRAY)
  • -- steps through each item of an array executing
    one iteration each time thru the loop
  • _at_rocks qw /bedrock pebbles stone/
  • foreach rock (_at_rocks)
  • print "One rock is rock\n"

20
Control Loop Variable
  • A change to the control loop variable actually
    changes the elements of the array.
  • rock "Haggar" rock is stored and saved by
    perl
  • _at_rocks qw/ slate stone marble /
  • foreach rock (_at_rocks)
  • rock s/(.)/\U1/
  • print "_at_rocks\n" SLATE STONE MARBLE
  • print "rock\n" Haggar restored ???

21
Other Array Operators
  • reverse ARRAY
  • -- takes an array of items and reverses the
    order
  • _at_fred 5..10
  • _at_derf reverse (_at_fred) 10 9 8 7 6 5
  • _at_fred reverse _at_fred fred is now reversed too
  • reverse SCALAR
  • -- in a scalar context, such as the string
    ATGCC, reverse will reverse the order of the
    string -- CCGTA

22
Other Array Operators
  • sort ARRAY
  • -- takes a list of values and sorts them in
    character order (lexigraphically)
  • _at_rocks qw/ slate stone marble /
  • _at_nums (97, 98, 99 100, 101)
  • _at_sorted sort (_at_rocks) marble slate stone
  • _at_nums sorted (_at_nums) 100 101 97 98 99

23
Array-like Operator
  • glob EXPR
  • -- In list context, returns a (possibly empty)
    list of filename expansions on the value of EXPR
    such as the standard Unix shell /bin/csh would do
  • Ex)
  • _at_files glob ''

24
Examples
  • Multiple syntax for arrays

25
End
26
Other topics
  • sort
  • reverse
  • glob
  • split
  • system and
  • array growth/memory allocation
  • pop, push, shift, unshift
  • slice
  • building 2-d arrays
  • arrays of hashes and hashes of arrays
  • Other
  • _at_ARGV (modules, and module for long)
  • file tests
  • files and directories
  • cgi-bin
  • advanced sorting
  • strings

27
Side Note
  • More on arrays in later lecture, however,
    question of arrays of arrays

28
  • !/usr/bin/perl
  • _at_first (1,2,3,4,5)
  • _at_second (10,20,30,40,50)
  • _at_third (100,200,300,400,500)
  • \_at_ is a "reference"
  • _at_numbers (\_at_first,\_at_second,\_at_third)
  • foreach i (_at_numbers) i becomes a reference
    to an array
  • foreach j (_at_i) _at_ dereferences i (an
    reference to an aray)
  • so j is an elemetn of
    an array
  • print "j is value in arrayArray\n"
  • print "\n"

29
  • ./arrayArray.pl
  • 1 is value in arrayArray
  • 2 is value in arrayArray
  • 3 is value in arrayArray
  • 4 is value in arrayArray
  • 5 is value in arrayArray
  • 10 is value in arrayArray
  • 20 is value in arrayArray
  • 30 is value in arrayArray
  • 40 is value in arrayArray
  • 50 is value in arrayArray
  • 100 is value in arrayArray
  • 200 is value in arrayArray
  • 300 is value in arrayArray
  • 400 is value in arrayArray
  • 500 is value in arrayArray
  • 400

30
End
31
Example
  • Negative numbers in the sample source code I
    provided, I use the following REGEXP \d to
    verify that a "number" was entered.
  • Note that this will not match a negative number
    (as in the example I ran during class) because it
    does not include a "-"
  • Note, you should only set the seed ONCE doing
    so more often would actually decrease the
    approximation of a random number
  • srand (time() ( ( ltlt 15)) )
  • Examples
  • What is "random" how would you verify that your
    output is random?
  • You would expect each letter to occur about 25
    of the time.
  • ./randSeq.pl gt look
  • cat look grep A wc
  • OR
  • count the letters as you generate them and print
Write a Comment
User Comments (0)
About PowerShow.com