PLEAC = Language Examples Alike Cookbook PLEAC examples are drawn from the "Perl Cookbook" by Tom Christiansen & Nathan Torkington, published by O'Reilly. They provide a nice range of examples oriented toward data munging, the type of work I tend to want to do first when learning a new language.
The Julia examples below are principally translations from the Python version
I'm learning as I go, so the code below probably doesn't represent best practice. Your suggestions are welcome! Please file an issue or make a pull request to the github repo.
IJulia_PLEAC uses IJulia notebook, whose format appears to be incompatible with the PLEAC file format.
The examples are not complete. Missing items are generally noted in comments.
Let's try out some string literals.
mystr = "\n" # a string containing 1 character, which is a newline
"\n"
mystr = "\\\n" # two characters, \ and n
"\\\n"
length(mystr)==2
true
mystr = "Jon 'Maddog' Orwant" # literal single quote inside double quotes
"Jon 'Maddog' Orwant"
mystr = """Jon "Maddog" Orwant""" # literal double quote inside triple quotes
"Jon \"Maddog\" Orwant"
mystr = "Jon \"Maddog\" Orwant" # escaped double quote
"Jon \"Maddog\" Orwant"
mystr = """
This is a multiline string literal
enclosed in triple double quotes.
"""
"This is a multiline string literal\nenclosed in triple double quotes.\n"
#julia does not currently have an equivalent to python docstringss, but triple-quoted comments are allowed
function fn(x)
"""we can put a comment here"""
return 2x
end
fn(3)
6
offset=2; count=3;
mystr[offset:offset+count]
"his "
mystr[offset:] # offset to end
"his is a multiline string literal\nenclosed in triple double quotes.\n"
data = b"abcdefghijklmnopqrstuvwxyz"; # byte array 0x61-0x7a
leading, s1, s2, trailing = data[1:5], data[9:16], data[17:24], data[25:]
([0x61,0x62,0x63,0x64,0x65],[0x69,0x6a,0x6b,0x6c,0x6d,0x6e,0x6f,0x70],[0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78],[0x79,0x7a])
typeof(data)
Array{Uint8,1}
a=Array{Uint8,1}[]
0-element Array{Array{Uint8,1},1}
push!(a,data[1:5])
1-element Array{Array{Uint8,1},1}: [0x61,0x62,0x63,0x64,0x65]
i=1
count=5
fivers=Array{Uint8,1}[] # 1 dimensional array of bytearrays
while i<=length(data)
chunk = data[ i : min(i+4,length(data)) ]
push!(fivers, chunk )
i=i+count
end
fivers
6-element Array{Array{Uint8,1},1}: [0x61,0x62,0x63,0x64,0x65] [0x66,0x67,0x68,0x69,0x6a] [0x6b,0x6c,0x6d,0x6e,0x6f] [0x70,0x71,0x72,0x73,0x74] [0x75,0x76,0x77,0x78,0x79] [0x7a]
mystr = "This is what you have"
# julia indexes are 1-based
first = mystr[1] # "T"
'T'
start = mystr[6:7] # "is"
"is"
rest = mystr[14:] # "you have"
"you have"
last = mystr[end:] # "e" a string
"e"
mystr[end] #a char
'e'
mystr[end-1] #a char
'v'
end4 = mystr[end-3:] # "have"
"have"
piece = mystr[end-7:end-4] # "you"
"you "
mystr[1:10]
"This is wh"
mystr[10+1:end]
"at you have"
replace(mystr, " is ", " wasn't ")
"This wasn't what you have"
txt = "at"
contains(mystr,txt)
true
beginswith(mystr, txt)
false
endswith(mystr,txt)
false
isdefined(:astr)
false
astr=None
None
astr == None
true
isdefined(:astr) # defined but None
true
:(astr)
:astr
function isassigned(myvar::Symbol)
"""return true if the Symbol is defined and not None"""
DEBUG=false
if DEBUG println( :(myvar) ) end
if DEBUG println( isdefined(myvar) ) end
if isdefined(myvar)
varval = eval(myvar);
if DEBUG println( "varval $varval" ) end
if varval==None
if DEBUG println( "$myvar==None ") end
return false
elseif varval==""
if DEBUG println( """$myvar=="" """) end
return false
else
if DEBUG println("$myvar has an assigned value") end
return true
end
else
if DEBUG println("not defined") end
return false
end
end
isassigned (generic function with 1 method)
isassigned( :nosuchvar )
false
isassigned( :astr )
false
ex=999
isassigned(:ex)
true
mydefault = "blah"
"blah"
ystr = isdefined(:(astr)) & (astr!=None) ? astr : mydefault
ystr
"blah"
ystr = isassigned(:astr) ? astr : mydefault
"blah"
function f(x::Int)
"""a function that can return None"""
y = (x==4) ? x : None
return y
end
f (generic function with 1 method)
myvar = f(3)
myvar== (myvar==None) ? "blah" : myvar
None
function fdef(prefix="pre")
"""a function that can return None"""
prefix * " duh"
end
fdef (generic function with 2 methods)
fdef("asd")
"asd duh"
fdef()
"pre duh"
# use b if b is defined, else c
a = isdefined(:b) ? b : 21
21
ENV["USERNAME"]
"KEITHC"
haskey(ENV,"USERNAME")
true
function get_username()
if haskey(ENV,"USERNAME")
usr=ENV["USERNAME"]
elseif haskey(ENV,"LOGNAME")
usr=ENV["LOGNAME"]
elseif haskey(ENV,"USER")
usr=ENV["USER"]
else
usr = None
end
return usr
end
get_username (generic function with 1 method)
get_username()
"KEITHC"
# v1, v2 = v2, v1
x, y = (4, 3);
x
4
y
3
one, two, three = split("January March August")
3-element Array{String,1}: "January" "March" "August"
[one, two, three]'
1x3 Array{ASCIIString,2}: "January" "March" "August"
one, two, three = two, three, one
("March","August","January")
one
"March"
char = 'a'
'a'
ascii_value = int(char)
97
Base.char(120) # char(120) should work but I'm getting a namespace collision
'x'
println("number $(int(char)) is character '$char'")
number 97 is character 'a'
ascii_character_numbers = [int(c) for c in "sample"]
6-element Array{Int64,1}: 115 97 109 112 108 101
join([c for c in "sample"],"")
"sample"
join([Base.char(i) for i in ascii_character_numbers],"")
"sample"
hal="HAL"
join([Base.char(int(i)+1) for i in hal],"")
"IBM"
myarray = [c for c in "whatever"]'
1x8 Array{Char,2}: 'w' 'h' 'a' 't' 'e' 'v' 'e' 'r'
for mychar in "whatever"
println(mychar)
end
w h a t e v e r
cset = Set([c for c in "Whatever"]...)
Set{Char}('r','a','h','W','v','t','e')
split("whatever","")'
1x8 Array{String,2}: "w" "h" "a" "t" "e" "v" "e" "r"
sset =Set( split("whatever","")... )
Set{ASCIIString}("a","e","r","v","w","t","h")
csort = sort([c for c in cset])'
1x7 Array{Any,2}: 'W' 'a' 'e' 'h' 'r' 't' 'v'
println("unique chars are: ", join(csort,","))
unique chars are: W,a,e,h,r,t,v
mystr="whatever"
ascvals= [int(c) for c in mystr];
println("""Total is $(sum(ascvals)) for "$mystr".""")
Total is 870 for "whatever".
function checksum(mystr)
vals = [ int(c) for c in mystr ]
chksum = sum(vals) % (2^16) - 1
return chksum
end
checksum (generic function with 1 method)
checksum("whatever")
869
fl = open("c:/temp/blah.txt","w")
write(fl, "what up, gee?")
close(fl)
flstr = readall("c:/temp/blah.txt")
"what up, gee?"
checksum(flstr)
1140
reverse([1,2,3])
3-element Array{Int64,1}: 3 2 1
reverse("Stuff")
"ffutS"
mystr="a bunch of words"
myarray = split(mystr)
4-element Array{String,1}: "a" "bunch" "of" "words"
join(reverse(myarray), " ")
"words of bunch a"
word = "reviver"
is_palindrome = (word == reverse(word))
true
replace("adfa\t ",'\t'," ")
"adfa "
replace(" blah", " ",'\t')
"\t\tblah"
#-----------------------------
rows=24; cols=80
text = "I am $(rows) high and $(cols) long"
print( text)
I am 24 high and 80 long
re = r"\d+"
r"\d+"
ismatch(re,"Are you 43?")
true
ismatch(re,"Are you XXIII?")
false
m=match(re,"Are you 43?")
RegexMatch("43")
dump(m)
RegexMatch match: SubString{UTF8String} "43" captures: Array(Union(Nothing,SubString{UTF8String}),(0,)) [] offset: Int64 9 offsets: Array(Int64,(0,)) []
m.offset
9
m.match
"43"
replace("Are you 23 or 43?",re,"xx")
"Are you xx or xx?"
m.captures
0-element Array{Union(Nothing,SubString{UTF8String}),1}
rec=r"(\d+)"
r"(\d+)"
m=match(rec,"Are you 43?")
dump(m)
RegexMatch match: SubString{UTF8String} "43" captures: Array(Union(Nothing,SubString{UTF8String}),(1,)) ["43"] offset: Int64 9 offsets: Array(Int64,(1,)) [9]
parseint( m.captures[1])
43
mystr = "Are you 43?"
m = match(rec,"Are you 43?")
replace(mystr, m.captures[1], string(2*parseint(m.captures[1])) )
"Are you 86?"
function default_string(string_symbol::Symbol, default="[Not Defined]")
def_str= isdefined(string_symbol) ? eval(string_symbol) : "[Not Defined]"
return def_str
end
default_string (generic function with 2 methods)
"""who is $(isdefined(:z) ? z : "[Not Defined]")"""
"who is [Not Defined]"
"""who is $(default_string(:z))"""
"who is [Not Defined]"
uppercase("bo peep")
"BO PEEP"
lowercase("Bo Peep")
"bo peep"
mystr="bo Peep"
"bo Peep"
function capitalcase(mystr)
"""capitalize the first letter of each word in a string"""
return join([ucfirst(s) for s in split(mystr)], " ")
end
capitalcase (generic function with 1 method)
capitalcase("bo peep went to fetch")
"Bo Peep Went To Fetch"
ucfirst("bo peep")
"Bo peep"
#skipped random case letters
n=45
"I have $(n + 1) guanacos."
"I have 46 guanacos."
Use templates from Package Mustache.jl to substitute from a dictionary.
using Mustache
mytemplate = mt"
To: {{address}}
From: Your Bank
CC: {{cc_number}}
Date: {{date}}
Dear {{name}},
Today you bounced check number {{checknum}} to us.
Your account is now closed.
Sincerely,
the management
"
MustacheTokens({{"text","\nTo: ",0,7},{"name","address",7,20},{"text","\nFrom: Your Bank\nCC: ",20,43},{"name","cc_number",43,58},{"text","\nDate: ",58,65},{"name","date",65,75},{"text","\n\nDear ",75,82},{"name","name",82,92},{"text",",\n\nToday you bounced check number ",92,126},{"name","checknum",126,140},{"text"," to us.\nYour account is now closed.\n\nSincerely,\nthe management\n",140,203}})
person = {"address"=>"Joe@somewhere.com",
"name"=>"Joe",
"date"=>"2012-04-22",
"cc_number"=>1234567890,
"checknum"=> 512
}
{"date"=>"2012-04-22","checknum"=>512,"cc_number"=>1234567890,"address"=>"Joe@somewhere.com","name"=>"Joe"}
print(render(mytemplate, person))
To: Joe@somewhere.com From: Your Bank CC: 1234567890 Date: 2012-04-22 Dear Joe, Today you bounced check number 512 to us. Your account is now closed. Sincerely, the management
indenting here documents
not clear we need to do anything --there are no leading blanks by default
var = """
your text
goes here
"""
"your text\ngoes here\n"
print(var)
your text goes here
poem = """
Here's your poem:
Now far ahead the Road has gone,
And I must follow, if I can,
Pursuing it with eager feet,
Until it joins some larger way
Where many paths and errand meet.
And whither then? I cannot say.
--Bilbo in /usr/src/perl/pp_ctl.c
"""
"Here's your poem:\nNow far ahead the Road has gone,\n And I must follow, if I can,\nPursuing it with eager feet,\n Until it joins some larger way\nWhere many paths and errand meet.\n And whither then? I cannot say.\n --Bilbo in /usr/src/perl/pp_ctl.c \n"
print( poem)
Here's your poem: Now far ahead the Road has gone, And I must follow, if I can, Pursuing it with eager feet, Until it joins some larger way Where many paths and errand meet. And whither then? I cannot say. --Bilbo in /usr/src/perl/pp_ctl.c
print( join( [lstrip(ln) for ln in split(poem,'\n')[2:end-2] ], "\n"))
Now far ahead the Road has gone, And I must follow, if I can, Pursuing it with eager feet, Until it joins some larger way Where many paths and errand meet. And whither then? I cannot say.
require("textwrap")
using TextWrap
txt = """\
Folding and splicing is the work of an editor,
not a mere collection of silicon
and
mobile electrons!
"""
"Folding and splicing is the work of an editor,\nnot a mere collection of silicon\nand\nmobile electrons!\n"
print(TextWrap.wrap(txt, width=20, initial_indent=4, subsequent_indent=2))
Folding and splicing is the work of an editor, not a mere collection of silicon and mobile electrons!
"""Expected result:
01234567890123456789
Folding and
splicing is the
work of an editor,
not a mere
collection of
silicon and mobile
electrons!
""";
# merge multiple lines into one, then wrap one long line -- SKIP
Will not solve.
mystr = " blah "
" blah "
lstrip(mystr)
"blah "
rstrip(mystr)
" blah"
strip(mystr)
"blah"
ezcsv = """a,b,c
1,2,3
4,5,6"""
"a,b,c\n1,2,3\n4,5,6"
line = """XYZZY,"","O'Reilly, Inc","Wall, Larry","a \\"glug\\" bit,",5,"Error, Core Dumped,","""
"XYZZY,\"\",\"O'Reilly, Inc\",\"Wall, Larry\",\"a \\\"glug\\\" bit,\",5,\"Error, Core Dumped,\","
sio = IOBuffer(ezcsv)
IOBuffer([0x61,0x2c,0x62,0x2c,0x63,0x0a,0x31,0x2c,0x32,0x2c,0x33,0x0a,0x34,0x2c,0x35,0x2c,0x36],true,false,true,false,17,9223372036854775807,1)
data_array =readdlm(sio, ',')
3x3 Array{Any,2}: "a" "b" "c" 1.0 2.0 3.0 4.0 5.0 6.0
close(sio)
function stringdlm(source::String, delim::Char; has_header=false, use_mmap=false, ignore_invalid_chars=false)
"""parse a string of delimited text"""
sio = IOBuffer(source)
data_array =Base.readdlm(sio, ',')
close(sio)
return data_array
end
stringdlm (generic function with 1 method)
stringdlm(ezcsv,',')
3x3 Array{Any,2}: "a" "b" "c" 1.0 2.0 3.0 4.0 5.0 6.0
For example "Wall, Larry" should not have been split below.
#bug in readdlm()?
for ln in stringdlm(line,',')'
println(ln)
end
XYZZY "" "O'Reilly Inc" "Wall Larry" "a \"glug\" bit " 5.0 "Error Core Dumped "
stringdlm("""'Bush,Reggie','Morris,Alfred'""", ',')
1x3 Array{Any,2}: "'Bush" "Reggie'" "'Morris"
test0.csv:
"Name","Age","Weight"
"Bob",26,160
"Sue",24,110
test1.csv -- the name field includes embedded commas:
"Name","Age","Weight"
"Jones,Bob",26,160
"Collins,Sue",24,110
readdlm() does not seem to know about quotes around strings.
readdlm("test0.csv",',')
WARNING:
new not defined at In[148]:1 in mmap_array at mmap.jl:133
as a consequence, readdlm() has trouble with test1.csv:
readdlm("test1.csv",',')
backtraces on your platform are often misleading or partially incorrect
new not defined at In[149]:1 in mmap_array at mmap.jl:133
However, it does not appear to have an option to using an IOBuffer at the moment.
using DataFrames
readtable("test0.csv")
2x3 DataFrame: Name Age Weight [1,] "Bob" 26 160 [2,] "Sue" 24 110
readtable("test1.csv")
2x3 DataFrame: Name Age Weight [1,] "Jones,Bob" 26 160 [2,] "Collins,Sue" 24 110
Will not solve.
;ipython nbconvert 1_pleac_string.ipynb
[NbConvertApp] Using existing profile dir: u'C:\\Users\\keithc\\.ipython\\profile_default' [NbConvertApp] Converting notebook 1_pleac_string.ipynb to html [NbConvertApp] Support files will be in 1_pleac_string_files\ [NbConvertApp] Loaded template html_full.tpl [NbConvertApp] Writing 322023 bytes to 1_pleac_string.html