1 / 7

Perl Chapter 4

Perl Chapter 4. Arrays. foreach loop. Iterative statement to process array elements control based on number of elements in array or list literal foreach [scalar_var] (list_literal or array_name) { … } [scalar_var] optional - $_ used [scalar_var] local to the loop. examples.

loe
Download Presentation

Perl Chapter 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl Chapter 4 Arrays

  2. foreach loop • Iterative statement to process array elements • control based on number of elements in array or list literal foreach [scalar_var] (list_literal or array_name) { … } • [scalar_var] optional - $_ used • [scalar_var] local to the loop

  3. examples foreach $age (@ages) { $age++; } #age is now undef $sum=0; foreach $index (0..99){ $sum += $list[$index]; }

  4. list operators • reverse - @rnames = reverse @names; • sort - @sorted_names = sort @names; • list of numbers sorted as STRINGS • Chapter 6 talks more about sorting

  5. split function • opposite of join • takes strings apart – assigned to array often • split /pattern/, expression, limit • where /pattern/ is a regular expression

  6. split /pattern/, expression, limit /:/ “lsk:jfkd:skld:” absent is whitespace $stringVar // $_ splits into individual characters @chars=split //, “kumquats”;  “k”, “u”, “m”, “q”, “u”, “a”, “t”, “s”

  7. $line=<STDIN>; type in Bobby Sue Jones ($fname, $mname, $lname)= split / /, $line; or ($fname, $mname, $lname) = split $line; • limit specifies max number of substrings to produce @fruit=split /,/, apples,grapes,pineapples,bananas”, 3; results in @fruit  (“apples”, “grapes”, “pineapples,bananas”) • /pattern/ can get quite complex  Chapter 7

More Related