Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Variables and Data Types: references/matricies

Similar presentations


Presentation on theme: "More Variables and Data Types: references/matricies"— Presentation transcript:

1 More Variables and Data Types: references/matricies
References: can used to save memory usage or create complex data structures. = (1,3,5); #an array my $array_ref = #a reference my $new_arr_ref = []; #initialize an empty array ref Deferencing variables is done with a leading ‘$’. ie: ${$scalar} or or ${%hash} Matricies: N-dimensional arrays (Matricies) can be constructed from lists of arrays using references. This table: Is represented by this structure: = (‘A’,1,’Two’); #first row = (‘X’,’V’,9); #second row =(.04,’T’,’you’); #third row = #build the matrix print “Row 1 Col 1:” . $matrix[0][0] . “| Row 1 Col 2:” . $matrix[0][1] . “\n”; A 1 Two X V 9 .04 T you

2 System Calls: system() and exec()
Any command you can execute on the command line, can be executed from within Perl The two functions to accomplish this are system() and exec() perldoc -f system [or exec] for more info. Both take as an argument a string which represents a valid command, or a command in the form of an array (where the command, and all its arguments are held in consecutive elements of the array). It is most common to use command strings. system(): will wait while the command is executing and you can capture any output from the command as well as evaluate the exit code. Once the command has finished running, the calling perl script will continue to run. exec(): will run the command and exit the perl script at this point. This is generally not desired- use system() If in doubt.

3 Regular Expressions if ($string =~ /world/) { print “passes\n”;
From wikipedia: “A regular expression (abbreviated as regexp or regex, with plural forms regexps, regexes, or regexen) is a stringthat describes or matches a set of strings, according to certain syntax rules.” Perl is considered to have the most full featured, and more importantly, consistant regex syntax. Most current languages which provide regex support try to make their implementations Perl compatible. Example: my $string = “hello world”; #the special operator =~ is used for regular expressions #our variable is being tested (using the =~) for the pattern described between the // #in this case, we are testing if the substring ‘world’ is contained in our string #if there is a match, this operation will return true, if not it returns false if ($string =~ /world/) { print “passes\n”; } else { print “fails\n”; }

4 Regular Expressions cont.
Regular expressions are a vast topic. It is easy to get started, and just as easy to get frustrated when building complex regular expressions. Be patient, and play around with them to get a feel for how they work. Some Excellent Resources Are: Learning Perl Regex Chapter (handout) Regex Summary: (handout) and Wikipedia General Overview: Wikipedia Perl examples: Good Intro Guide: From above guide : Regular expressions is a HUGE area of knowledge, bordering on an art. Rather than regurgitate the contents of the PERL documentation or the plethora of PERL books at your local bookstore, this page will attempt to give you the 10% of regular expressions you'll use 90% of the time.

5 Exercises


Download ppt "More Variables and Data Types: references/matricies"

Similar presentations


Ads by Google