Lec8Handouts - CS2044 Advanced Unix Tools Spring 2009...

Info icon This preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CS2044 - Advanced Unix Tools Spring 2009 Lecture 8 David Slater dms236 at cornell.edu March 7, 2009 David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Organization Homework 2 Due Monday March 8th Hopefully You Started Early! Turn In Code That Works! Follow Directions Homework 3 Will be on the website by tomorrow night Homework 3 Due Next Sunday at 11:59PM David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 2
Review / Clarification /abc(?=def)/ matches abc only if it is directly followed by def In for loops we can use the range operator .. for $x (0 .. 10) { print $x; } David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
You can specify the delimiter for regular expression by using the m option /http:\/\/www\.cam\.cornell\.edu\/~slater\/cs2044\/index\.html/ Becomes m#http://www\.cam\.cornell\.edu/~slater/cs2044/index\.html# And you can escape everything by using \Q followed by \E m#\Qhttp://www.cam.cornell.edu/~slater/cs2044/index.html\E# David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 4
The Perl Motto ”There’s more than one way to do it” The following is just fine Print $x if ( $x eq /someregexp/); (Read Right to Left) David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arrays Perl can do standard arrays and associative arrays (dictionaries). We’ll talk about associative arrays later. An array variable is just like a scalar variable except it starts with @: @names = ("David", "James", "Joan", "Sarah"); print "The elements of names are @names\n"; print "The first element is $names[0] \n"; print "There are ", scalar(@names)," elements in the array\n"; Prints: The elements of @names are David James Joan Sarah The first element is David There are 4 elements in the array David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 6
Arrays When referring to more than one variable use @array When referring to a single element use $array[i] $array would refer to a scalar variable called array. @array[i,j,k] to refer to multiple elements @array[i..j] to refer to a range of elements $#array to refer to the last index $scalar = @array will set $scalar to the length of @array. David Slater dms236 at cornell.edu CS2044 - Advanced Unix Tools
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Arrays and for loops Looping over elements in an array is easy for $x (@array) { print "$x \n; } foreach $x (@array) { print "$x \n; } Both are equivalent.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern