Whats a Scalar - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Whats a Scalar

Description:

Whats a Scalar – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 28
Provided by: acmCseB
Category:

less

Transcript and Presenter's Notes

Title: Whats a Scalar


1
Whats a Scalar?
  • Simplest kind of data Perl manipulates
  • Either a number or a string of characters (Perl
    uses them interchangeably)
  • Can use operators on scalars, usually generating
    another scalar
  • Can store scalar value into scalar variable

2
Scalar Variables ()
  • To assign into a scalar variable, use a sign
  • foo 27 a scalar number
  • bar 'Joel Grow' a scalar string

3
Numbers
  • 3
  • -9000000000000
  • 3.1415926536
  • 3.46e10
  • For the C geeks/curious really all are
    internally double precision floats

4
Numerical Operators
  • 2 3
  • 5.1 - 2
  • 3 12
  • 10.2 / 3
  • 2 3
  • 10 3

5
Strings
  • Sequences of 0 or more characters
  • ""
  • 'Joel'
  • "This is a long string with numbers 8 1 33 and
    !_at__at_ characters"
  • Any character (8 bit, 0 thru 256)
  • Not just ASCII 32 thru ASCII 126
  • Can be single quoted or double quoted

6
Single-Quoted Literal Strings
  • Does NOT do variable interpolation--treats the
    string as literal
  • 'camel' five chars, 'c', ..., 'l'
  • 'don\'t' includes single quote char
  • '' null string, no chars
  • 'a\\b' 'a', '\', 'b'

7
Double Quoted Literal Strings
  • DOES do variable interpolation
  • backslashes are special
  • "big camel\n" 2 words and a newline
  • "bond \007" 007 is bell char in octal
  • "coke\tsprite" tab between words
  • "\cGding" bell char (ctrl-C) and ding
  • "a\\b" 'a', '\', 'b'
  • "c\temp" what's this?
  • "hello foo\n" replaces foo with it's value

8
Operators for Strings
  • "tall" . " turtle" concatenation
  • 'what "color" is that?' . "\n"
  • "a" . " " . "b"
  • a eq 'bcd'
  • f ne g
  • g gt "mcgd"
  • jake lt fred
  • a le b
  • c ge d
  • (32) x 4 is "5555
  • "-" x 50 is what?

9
Numeric vs String comparisons...
  • Context is very important in comparisons!
  • a 5
  • b 16
  • c 'joelg'
  • d 'joel'
  • if ( a lt b ) numeric, true
  • if ( a lt b ) string, false
  • if ( c gt d ) string, true
  • if ( c gt d ) numeric, false

10
More on numeric vs. string comparisons
  • Here's a common gotcha, using instead
  • of eq, (or vice versa)
  • a 5
  • b 16
  • c 'joelg'
  • d 'joel'
  • if ( c d ) numeric, true!
  • if ( c eq d ) string, false

11
Alternative Quotes
  • Can use q like single quotes and qq like
    double quotes
  • you can actually use any matchable delimiter (
    ) ltgt
  • qhello 'world'!
  • qqhello foobar!
  • qA 'starring' \role\
  • qqDon't "quote" me!
  • qltDon't "quote" me!gt

12
Assigning into a variable
  • one 1
  • two 2.0
  • first_name "Joel"
  • last_name 'Grow'
  • another_one one
  • copy_last_name last_name

13
Controlling Case examples
  • big uc('hello') HELLO
  • little lc('WorlD') world
  • big "\Uhello" HELLO
  • big "\uhELLo" HELLo
  • little "\LWorlD" world
  • little "\lwORLD" wORLD
  • beast "dromedaRY"
  • capitalize ucfirst(beast) DromedaRY
  • capitalize "\u\Lbeast" Dromedary

14
Here Documents
  • Can print multi-line string easily
  • print ltltEnd_of_HTML
  • ltHTMLgt
  • ltBODYgt
  • ltTABLEgtltTRgtltTDgtHello world!lt/TDgtlt/TRgt
  • lt/TABLEgt
  • lt/BODYgt
  • lt/HTMLgt
  • End_of_HTML

15
Scalar Operators and Functions
  • a 4
  • b a 3
  • b b 2
  • d (c 4) assign 4 to c and d
  • d c 4 same thing
  • b 4 (a 3) what is a? b?
  • Note, just because you can do something in
  • Perl doesn't always mean you should. That
  • last line is not very obvious to the code
  • reader.

16
Binary Assignment Operators
  • a 3 same as a a 3
  • b - 1 b b - 1
  • d / 3 d d / 3
  • r 4 r r 4
  • b (a 4) valid, but weird.
  • Please don't do this

17
More binary assignment
  • concatenate
  • str . 'foobar'
  • same as str str . 'foobar'

18
Autoincrement/autodecrement
  • means 'add one' -- means 'subtract one'
  • a 2
  • a same as a a 1, a now equals 3
  • pre and post increment
  • print a prints 3, changes a to 4
  • print a prints 5, a now 5
  • print --a prints 4, a now 4
  • print a-- prints 4, a now 3

19
Scalar Interpolation
  • a "joel"
  • this will print "hi joel!" and a newline
  • b "hi a!\n"
  • c "no such variable foo\n"
  • if foo is undefined then c is
  • "no such variable \n"

20
More scalar interpolation
  • x 'fred'
  • y "hey x" y is 'hey fred'
  • if you don't want interpolation
  • z "hey \x"
  • z is what?

21
More scalar Interpolation
  • we want to assign "It's payday" to barney
  • fred "pay"
  • fredday "Monday"
  • barney "It's fredday!"
  • what is barney?

22
More scalar interpolation
  • fred "pay"
  • fredday "Monday"
  • use if needed
  • barney "It's fredday!" Its payday!
  • or do this
  • barney "It's fred"."day!"

23
Chop
  • chop removes (and returns) the last
  • character in a string.
  • x "hello world"
  • chop(x) x is now "hello worl"
  • last_char chop(x) what is last_char?
  • x chop(x) replaces x with its last
  • character. weird but valid

24
Chomp
  • var "abc\n"
  • chomp(var) var becomes "abc"
  • chomp only removes the "record separator
  • character", usually a newline.
  • It's a safe chop.
  • chomp is much more common than chop.
  • you will probably use chomp a lot and chop
  • very little, if ever

25
Scalar ltSTDINgt
  • Reads input from the user
  • (on the STDIN filehandle--more on filehandles in
    a few weeks)
  • x ltSTDINgt wait for user to type
  • something...
  • chomp (x) get rid of newline
  • or, do above in one step
  • chomp (x ltSTDINgt)
  • print "You just typed 'x'\n"

26
Scalars in Action
my name "Joel Grow" my email 'joelg' my
age 28 my GPA 3.72 if ( GPA gt 3 )
print "Good job name!\n" print "I'll send
email to email.\n" else print "You need
to study more name!\n"
27
Where do we go from here?
  • Arrays--a big ol' list of scalars!
Write a Comment
User Comments (0)
About PowerShow.com