Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
perl_tutorial
#perl 5.22.1 use strict; use warnings; print "Hello World\n"; #scalars store any integr single value my $g = 12; my $h = 15; my $apple_variety = "Red, delicious"; #arrays store any number of ordered scalars my @first_array = (1,2,3,4,5,6,7,8,9); my @second_array = ("strings", "also", "work", "and spaces"); $first_array[3] = 99; #change single array item $first_array[-1] = 12; #- count array in reverse order my @fast_array = qw(hola hello como estas); my @numbers = (23, 23, 23, 23, 23); my @composite = (4,6,8..10,12,14..16); my @veggies = qw(spinach lettuce oninons); my @fruits = qw(apple banana pairs); my @grocery_list = (@fruits, @veggies, "milk"); #unshift operation (append to head of list and prints number of elements, shift right) print "@grocery_list\n"; print unshift(@grocery_list, "avocado"); print "\n"; print "@grocery_list\n"; #shift operation (removes top element of list and prints element removed, shift left) print shift(@grocery_list); print "\n"; print "@grocery_list\n"; #pop operation (pops out last element of the list) print pop(@grocery_list); print "\n"; print "@grocery_list\n"; #push operation (pushes in one element to the tail of the list) print push(@grocery_list, "avocado", "salt"); print "\n"; print "@grocery_list\n"; #slicing arrays #$array[1] is scalar, @array[1] is array with a single value my @video_games = qw(smash naruto minecraft cod dota); my @fav_games = @video_games[0,4]; push(@fav_games, "agario"); unshift(@fav_games, "coolgame"); print "@fav_games\n"; #iterating over arrays foreach my $x (@fav_games){ print "this game is $x\n"; } #hashes: stores scalars as unorganized key values my %first_hash = (horses => 4, swords => 7, mages => 3); print "Our army has $first_hash{horses} horses, $first_hash{swords} swords\n"; $first_hash{horses} = 10; #changing hash value print "Our army has $first_hash{horses} horses, $first_hash{swords} swords\n"; #can be delcared in many different ways my %deserts1 = (cake => "apple", cheesecake => "strawberry", sorbet => "lime"); my %deserts2 = ("cake", "apple", "cheesecake", "strawberry", "sorbet", "lime"); my %deserts3 = qw(cake apple cheesecake strawberry sorbet lime); print "$deserts1{cake} cake, with a $deserts2{cheesecake} chck and $deserts3{sorbet} sorbet\n"; #delete with delete keyword delete $deserts3{sorbet}; #hash slicing, both forms work my @desert_fruits = ($deserts1{cake}, $deserts1{cheesecake}, $deserts1{sorbet}); my @desert_fruits2 = @deserts2{"cake", "cheesecake", "sorbet"}; print "@desert_fruits\n"; print "@desert_fruits2\n"; #hash iteration, note the syntax! my @desertsx = keys %deserts1; my @fruitsx = values %deserts1; while (my ($desertsx, $fruitsx) = each %deserts1) { print "I have a $fruitsx $desertsx\n"; } #operators (same as other programming languages #numeric operators my $numx = 3; $numx *= 10; print "$numx\n"; my $bit1 = 7; #111 my $bit2 = 5; #101 #shift right $bit1 = $bit1 >> 1; #becomes 011 $bit2 = $bit2 >> 1; #becomes 010 #shift left $bit1 = $bit1 << 1; #becomes 110 $bit2 = $bit2 << 1; #becomes 100 print "$bit1\n"; print "$bit2\n"; #string operators my $stringy = "hello there"; $stringy.=" stranger "; #appending string to the end of string print "$stringy\n"; $stringy x= 3; #repeating print "$stringy\n"; #boolean values -> not real boolean values in perl my @books = qw(sapiens ai thinking); print "They are both books\n" if ($books[0] == $books[1]); #type equality print "They are both the same book\n" if ($books[0] eq $books[0]); #comparing the string #useful array operations #sort, reverse, push/pop, shift/unshift, split/join, grep, map #sort my @number_collection = qw(1 9 5 7); print "@number_collection\n"; @number_collection = reverse(@number_collection); #reversing order of array print "@number_collection\n"; @number_collection = sort(@number_collection); #sorting in numerical order print "@number_collection\n"; #reverse my @words = qw(table chair pencil apple book); print "@words\n"; @words = reverse(@words); #reversing order of array print "@words\n"; @words = sort(@words); #sorting strin array in alpahabetical order print "@words\n"; #join (array -> string) my @utensils = qw(spoon fork knife); my $utensil_string = join(" and a ", @utensils); #joins each element with a string print "$utensil_string\n"; #split (string -> array) my $sports = "swimming gym frisbee"; my @sports_array = split(" ", $sports); print "@sports_array\n"; #grep (get elements that contain certain grep) my @juices = qw(apple oraage pear apple-strawberry bannana-apple lime); my @apple_juices = grep(/apple/, @juices); print "This are the juices that contain apple: @apple_juices\n"; #can also be used in numbers my @numberss = qw(1 2 3 4 5 6 7 8 9 10); my @thic_numbers = grep{$_>5} @numberss; #both formats are acceptable my @small_numbers = grep($_<4, @numberss); print "@thic_numbers\n"; print "@small_numbers\n"; #map my @multiplied_numbers = map($_*2, @numberss); print "This are the multiplied numbers: @multiplied_numbers\n"; #maps also works with conditions my @big_numbers = map($_>10 ? $_ : (), @multiplied_numbers); print "This are the big numbers: @big_numbers\n"; #flow control #conditionals -> if, unless #loops #test to see this works
run
|
edit
|
history
|
help
0
121JHgZzG4QMkDQwLJCifHAAsZAwvpqACY
PWC 059-1
Interpolation_with_case_sensitive
Perl
Challenge 41 Task 1
main_objetos
bitSum 059-1
matrx travarsal
Array_with_unshift_function
Password Validation