\w
, \W
, etc.).
?
, *
, +
, {n}
, {n,}
, {n,m}
)^
, $
, \b
, \B
)%%perl
use feature qw(say);
$string = "a b c" ;
say $string if $string=~ /\w+/;
%%perl
use feature qw(say);
$string = "abc" ;
say $string if $string=~ /\W+/;
$string = ",." ;
say $string if $string=~ /\W+/;
%%perl
use feature qw(say);
$string = "abba" ;
say $string if $string=~ /^a.+a$/;
$string = "aa" ;
say $string if $string=~ /^a.+a$/;
()
\1
i,g
[]
|
%%perl
use feature qw(say);
$string = "abba" ;
say $string if $string =~ /(\w)(\w)\2\1/
%%perl
use feature qw(say);
$string = "ABBA" ;
say $string if $string =~ /(a)(b)\2\1/i
%%perl
use feature qw(say);
$string = "10 GB";
say $string if $string =~ /\d+ [GKMT]B/
%%perl
use feature qw(say);
$string = "10 PB";
say $string if $string =~ /\d+ [^GKMT]B/
s///
$1
in replacement)%%perl
use feature qw(say);
$string = "Hello Class! Class!";
$string =~ s/Class!/World!/g;
say $string;
%%perl
use feature qw(say);
$string = "410-455-1000";
$string =~ s/(\d\d\d)-(\d\d\d-\d\d\d\d)/($1) $2/;
say $string;
%%bash
cat to_sort1.txt
sort to_sort1.txt
%%bash
head -n2 to_sort1.txt
tail -n2 to_sort1.txt
%%bash
var=10
str="A String"
echo $var $str
%%bash
arr=(10 20 30 40 50)
echo $arr ${arr[0]}
echo ${arr[@]}
%%bash
end="the end"
echo "I am interpolating ${end}ing."
<(command)
Syntax%%bash
ls -lh *.ipynb > ls.txt
head ls.txt
%%bash
ls *.ipynb | sort | head
if then else fi
[[ ]]
or [ ]
-eq
, -ne
,=
,!=
, etc.)case pattern) ;; esac
%%bash
a=10
if [[ $a -gt 5 ]]; then
echo "YAY"
else
echo "NAY"
fi
%%bash
b="Hello"
case $b in
*a)
echo "Ends with an A"
;;
He*)
echo "Contains He"
;;
*)
echo "Default"
;;
esac
for ((x = S; x < N; x++)); do ... done
for f in *.html
%%bash
for ((x = 10; x < 20; x++)); do
echo $x
done
%%bash
for f in *2{0..9}.html; do
wc -l "$f"
done
while CONDITION; do
#CODE_HERE
done
read
read
and while
echo
and printf
.
%%bash
while read line; do
echo $line
done < to_sort1.txt
(( ))
Syntax$(( ))
to save results%%bash
x=0
#((x=0))
((x++))
echo $((x + 2))
%%bash
grep -Pc "\d\d\d-\d\d\d-\d\d\d\d" *.html | grep -P ":[^0]$"
return
keyword actually do%%bash
function mystery(){
diff $1 $2
if [[ $? -ne 0 ]]; then
echo "The files $1 and $2 are different"
else
echo "The files $1 and $2 are the same"
fi
}
mystery "Lecture20.html" "Lecture20.html"
<-
)%%script R --no-save -q
a <- 4
print(a)
c
list
%%script R --no-save -q
vec <- c(1,2,3,4)
vec2 <- c(1,"2",3,4)
print(vec)
print(vec2)
%%script R --no-save -q
l1 <- list(1,2,3,4)
l2 <- list(1,"2",3,4)
print(l1)
print(l2)
matrix
%%script R --no-save -q
m <- matrix(c(1,2,3,4,5,6),nrow=2)
print(m)
%%script R --no-save -q
v2m <- c(1,2,3,4,5,6)
print(v2m)
dim(v2m) <- c(2,3)
print(v2m)
%%script R --no-save -q
df1 <- data.frame(int=c(1,2,3,4),float=c(1.01,2.0,3.0,4.0),str=c("One","Two","Three","Four"))
print(df1)
%%script R --no-save -q
df2 <- read.csv("usm.csv")
print(df2)
%%script R --no-save -q
m <- matrix(c(1,2,3,4,5,6),nrow=2)
print(m + 5)
%%script R --no-save -q
m <- matrix(c(1,2,3,4,5,6),nrow=2)
print(m * m)
[]
and [[]]
operations$
operator for Data Frames%%script R --no-save -q
v2 <- c('a','b','c','d')
print(v2[1])
%%script R --no-save -q
v3 <- list('a','b','c','d')
print(v3[1])
print(v3[[1]])
%%script R --no-save -q
v4 <- data.frame(letters=c('a','b','c','d'),numbers=c(1,2,3,4),symbols=c('!','#','$','@'))
print(v4$letters)
%%script R --no-save -q
v5 <- c(1,2,3,4,5)
print(v5[-1])
print(v5[v5 %% 2 == 1])
for ... in
lapply
%%script R --no-save -q
a <- 1
if(a > 0)
{
print("This is Positive")
} else {
print("This is Negative")
}
%%script R --no-save -q
b <- c(1,2,3,4,5)
for(x in b){
print(x)
}
%%script R --no-save -q
my_f <- function(a,def=1){
print(def)
a * a
}
print(my_f(10))
print(my_f(10,d=10))
%%script R --no-save -q
`%+%` <- function(a,b){
a * b
}
print(10 %+% 10)
%%script R --no-save -q
`fir<-`<-function(x,value){
x[1] <- value
x
}
y <- c(1,2,3,5)
fir(y)<-10
print(y)
structure
and class
UseMethod
%%script R --no-save -q
per<-function(name,age){
structure(list(m_name=name,m_age=age),class="per")
}
form<- function(ob){
UseMethod("form")
}
form.per <- function(obj){
paste(obj$m_name,"is",obj$m_age)
}
form.default <- function(obj){
"Not a Person"
}
p <- per("Bob",20)
print(form(p))
print(form(10))
grep
vs grepl
perl=TRUE
%%script R --no-save -q
print(grep("\\w\\w\\w",c("AABDF","AG","EOT"),perl=TRUE))
print(grepl("\\w\\w\\w",c("AABDF","AG","EOT"),perl=TRUE))
How does a HTTP Request Work?
HTML
<script>
tag<script src="location.js"></script>
<script src="https://example.com/remove_script.js"></script>
<script>
//Embed Code
</script>
==
and ===
%%html
<p id="results">To Be Replaced</p>
<script>
var abc = 1;
function scope_checker(){
var abc = 2
}
scope_checker()
document.getElementById("results").innerHTML = abc;
</script>
%%html
<p id="results1">To Be Replaced</p>
<script>
var abc = 1;
if(true){
let abc = 2
}
document.getElementById("results1").innerHTML = abc;
</script>
%%html
<p id="results2">To Be Replaced</p>
<script>
var s = "10"
var i = 10
if(s === i){
document.getElementById("results2").innerHTML = "Triple Equal";
}
else if(s == i){
document.getElementById("results2").innerHTML = "Double Equal";
}
else{
document.getElementById("results2").innerHTML = "Not Equal";
}
</script>
%%html
<p id="results3">To Be Replaced</p>
<script>
var arr = new Array();
arr.push(1);
arr.push(2);
document.getElementById("results3").innerHTML = JSON.stringify(arr);
</script>
%%html
<p id="results4">To Be Replaced</p>
<script>
var arr = [3,4,5];
document.getElementById("results4").innerHTML = JSON.stringify(arr);
</script>
%%html
<p id="results5">To Be Replaced</p>
<script>
var arr = ["Sand","Dirt","Water"];
function s(a,b){
a.charAt(a.length-3) - b.charAt(b.length-3);
}
document.getElementById("results5").innerHTML = JSON.stringify(arr.sort(s));
</script>
if, else if, else
for, while
for .. in
vs for .. of
%%html
<p id="results8">To Be Replaced</p>
<script>
s = " ";
arr = [10,9,8,7,6,5];
for(i in arr){
s += " " + i;
i++;
}
document.getElementById("results8").innerHTML = JSON.stringify(s);
</script>
%%html
<p id="results9">To Be Replaced</p>
<script>
s = " ";
arr = [10,9,8,7,6,5];
for(i of arr){
s += " " + i;
i++;
}
document.getElementById("results9").innerHTML = JSON.stringify(s);
</script>
function
keyword)%%html
<p id="results10">To be Replaced</p>
<script>
do_something();
function do_something(){
document.getElementById("results10").innerHTML = "Hoisted";
}
</script>
this
and new
keywords{ }
syntax%%html
<p id="results11">To be Replaced</p>
<script>
function Course(department, number, section){
this.department = department;
this.number = number;
this.section = section;
this.print = function(){
return "This is " + this.department + this.number + ",section " + this.section;
}
}
today = new Course("CMSC",433,"01");
document.getElementById("results11").innerHTML = today.print();
</script>
%%html
<p id="results12">To be Replaced</p>
<script>
Vehicle = {go: function(){
this.speed = 10;
}}
function Car(color){
this.color = color;
this.print = function(){
return "The " +this.color + ' car is going ' + this.speed + " mph";
}
}
Car.prototype = Vehicle;
vroom = new Car("red");
vroom.go();
document.getElementById("results12").innerHTML = vroom.print();
</script>
%%html
<p class="something"></p>
<div class="something"></div>
<script>
els = document.querySelectorAll(".something");
for(i = 0; i < els.length; i++){
els[i].innerHTML = "Hello " + i;
}
</script>
%%html
<p id="event">To Respond</p>
<script>
document.getElementById("event").addEventListener('click',
function(e){
e.target.innerHTML = "Clicked";
});
</script>
%%html
<input type="text" id="event2" />
<script>
document.getElementById("event2").addEventListener('focus',
function(e){
e.target.value = "In Focus";
});
document.getElementById("event2").addEventListener('blur',
function(e){
e.target.value = "Out of Focus";
});
</script>
open
onreadystatechange
send
%%html
<p id="ajaxResult"></p>
<script>
var xhr = new XMLHttpRequest();
xhr.open("GET","usm.csv");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.status == 200){
document.getElementById("ajaxResult").innerHTML = xhr.responseText
}
}
};
xhr.send();
</script>
<?php ?>
)$
, $$
, etc.%%script php
<?php
$a = "b";
$b = 10;
echo $$a . " " . $b;
%%script php
<?php
$a = 10;
if($a > 10){
?>
<strong> 10 is less than 10</strong>
<?php
} else { ?>
<strong> 10 is not less than 10</strong>
<?php }?>
%%script php
<?php
$list = [10,20,30,40,50];
foreach($list as $el){
echo $el * 2;
}
?>
.
echo
%%script php
<?php
$a = 10;
echo "This is interpolated: $a";
echo 'This isnt $a';
count
function%%script php
<?php
$indexed = array();
$indexed[] = 1;
$indexed[] = 2;
echo count($indexed);
%%script php
<?php
$indexed = [1,2];
print_r($indexed);
%%script php
<?php
$assoc = ['lunes'=>'monday','martes' => 'tuesday','miercoles' => 'wednesday'];
$assoc['jueves'] = 'thursday';
asort($assoc);
print_r($assoc);
ksort($assoc);
print_r($assoc);
sort($assoc);
print_r($assoc);
function
) keywordglobal
%%script php
<?php
$a = 10;
function scope_tester(){
$a .= "Hello";
}
scope_tester();
echo $a;
%%script php
<?php
$a = 10;
function scope_tester(){
global $a;
$a .= "Hello";
}
scope_tester();
echo $a;
$_FILES
%%script php
<?php
$fp = fopen("usm.csv",'r');
while($line=fgets($fp)){
echo $line;
}
%%script php
<?php
$stuff = file_get_contents("usm.csv");
echo $stuff;
%%script php
<?php
$stuff = file("usm.csv");
print_r($stuff);
$_GET
, $_POST
, $_SESSION
$_SERVER
->
syntax%%script php
<?php
class Money{
private $dollars;
private $cents;
function __construct($d = 0, $c =0){
$this->dollars = $d;
$this->cents = $c;
}
function __toString()
{
return '$' . $this->dollars . "." . $this->cents;
}
function getDollars(){
return $this->dollars;
}
}
$currency = new Money(10,10);
echo $currency;
echo "\n";
echo $currency->getDollars();
preg_
%%script php
<?php
preg_match("/([\w\.]+)@\w+\.\w\w\w/","My email address is bryan.wilkinson@umbc.edu",$matches);
print_r($matches);
%%script php
<?php
echo preg_replace('/\*\*(.+?)\*\*/','<strong>$1</strong>',"Outside **Make this Strong**");