from terminal
type cpan
you get the cpan prompt
then
o conf init pushy_https
yes
from terminal
type cpan
you get the cpan prompt
then
o conf init pushy_https
yes
when trying
cpan install Tk
I was getting lot of errors
the solution is
just install strawberry perl
then from command line
cpanm https://github.com/Lamprecht/perl-tk.git@Strawberry-5.38-patch
Source:https://stackoverflow.com/questions/78323022/installing-tk-on-strawberry-perl-leads-to-compiler-errors
use strict;
use warnings;
use LWP::Simple;
sub main{
print "Downloading ... \n";
print get("http://www.google.com/");
print "Finished\n";
}
main();
In this exercise, you will need to loop through and print out all even numbers from the @NUMBERS
array in the same order they are received. Don't print any numbers that come after 237 in the array.
@NUMBERS = (951,402,984,651,360,69,408,319,601,485,980,507,725,547,544,615,83,165,141,501,263,617,865,575,219,390,237,412,566,826,248,866,950,626,949,687,217,815,67,104,58,512,24,892,894,767,553,81,379,843,831,445,742,717,958,609,842,451,688,753,854,685,93,857,440,380,126,721,328,753,470,743,527);
for($i=0;($i<=$#NUMBERS);$i+=1) {
if ($NUMBERS[$i]==237)
{
last;
}
if ($NUMBERS[$i]%2==0)
{
print $NUMBERS[$i]."\n";
}
}
Perl way
@NUMBERS = (951,402,984,651,360,69,408,319,601,485,980,507,725,547,544,615,83,165,141,501,263,617,865,575,219,390,237,412,566,826,248,866,950,626,949,687,217,815,67,104,58,512,24,892,894,767,553,81,379,843,831,445,742,717,958,609,842,451,688,753,854,685,93,857,440,380,126,721,328,753,470,743,527);
foreach $number (@NUMBERS) {
if ($number % 2 == 0) {
print $number . "\n";
}
last if ($number == 237);
}
An array @family holds a list of family member names. The first hash %shoe_color contains favorite shoe color per person name. The second hash %shoe_size contains shoe size per person name.
Evaluate and print the favorite shoe color and shoe size per each family member. For shoe sizes 10 and above, add the word 'large' to the output line.
Output lines should be in the format: "Homer wears large brown shoes size 12".
Note: not all family members may be included in the hash variables, so you better conditionally check if they exist or not (using the exists operator). If a name does not exist, add the key/value pair into the hash variables - for show color add: black; for shoe size add 99.
Solution
@family = ('Homer', 'Moe', 'Maggie');
%shoe_color = ('Lisa' => 'red', 'Homer' => 'brown', 'Maggie' => 'pink', 'Marge' => 'blue', 'Bart' => 'yellow');
%shoe_size = ('Moe' => 9, 'Lisa' => 7, 'Homer' => 12, 'Bart' => 8, 'Maggie' => 4);
$default_shoe_color = "black";
$default_shoe_size = 4;
$member = $family[0];
if (!exists $shoe_color{$member}) {
$shoe_color{$member} = $default_shoe_color;
}
if (!exists $shoe_size{$member}) {
$shoe_size{$member} = $default_shoe_size;
}
$is_large = ($shoe_size{$member} >= 10) ? " large " : " ";
print "$member wears$is_large$shoe_color{$member} shoes size $shoe_size{$member}\n";
$member = $family[1];
if (!exists $shoe_color{$member}) {
$shoe_color{$member} = $default_shoe_color;
}
if (!exists $shoe_size{$member}) {
$shoe_size{$member} = $default_shoe_size;
}
$is_large = ($shoe_size{$member} >= 10) ? " large " : " ";
print "$member wears$is_large$shoe_color{$member} shoes size $shoe_size{$member}\n";
$member = $family[2];
if (!exists $shoe_color{$member}) {
$shoe_color{$member} = $default_shoe_color;
}
if (!exists $shoe_size{$member}) {
$shoe_size{$member} = $default_shoe_size;
}
$is_large = ($shoe_size{$member} >= 10) ? " large " : " ";
print "$member wears$is_large$shoe_color{$member} shoes size $shoe_size{$member}\n";
Assign the hash variable called car_catalog to include the following car models and their showroom prices in dollars. Use the car model name as the hash key. The cars and prices are:
Model: BMW Series 5, price: 100000
Model: Mercedes 2000, price: 250000
Model: Toyota Corolla, price: 20000
Model: Lexus 3, price: 95000
Assign an array variable called my_wishlist with the two cars you want to buy: the first array element is the full model name of the BMW car and the second array model is the full model name of the Toyota car. Use the array variable contents as keys to the hash variable in order to print lines in the following format: "I would like to buy one for the price of Dollars."
Solution
%car_catalog = ("BMW Series 5" => 100000 , "Mercedes 2000" => 250000, "Toyota Corolla" => 20000,"Lexus 3"=>95000);
@my_wishlist = ("BMW Series 5","Toyota Corolla");
print "I would like to buy one $my_wishlist[0] for the price of $car_catalog{$my_wishlist[0]} Dollars.\n";
print "I would like to buy one $my_wishlist[1] for the price of $car_catalog{$my_wishlist[1]} Dollars.\n";
Source:https://www.learn-perl.org/en/Variables_and_Types
@item_price_list = (5 , 8 , 24);
@item_name_list = ("Apple", "Banana", "Mushroom");
print "The price of one $item_name_list[0] is $item_price_list[0] gold coins.\n";
print "The price of one $item_name_list[1] is $item_price_list[1] gold coins.\n";
print "The price of one $item_name_list[2] is $item_price_list[2] gold coins.\n";
%item_catalog = ("Apple" => 5 , "Banana" => 8, "Mushroom" => 24);
# note the required backslash to escape the double-quotes around the key string Apple
print "The price of one Apple is $item_catalog{\"Apple\"} gold coins.\n";
$item_name = "Banana";
print "The price of one $item_name is $item_catalog{$item_name} gold coins.\n";
@item_name_list = ("Apple", "Banana", "Mushroom");
print "The price of one $item_name_list[2] is $item_catalog{$item_name_list[2]} gold coins.\n";
sub printMat {
#write the code for making and printing the matrix here
#use can use \n to move numers to next line in the matrix
#use " " to add space between numbers in matrix
#print "Write your code below"; #comment out this line when you start writing cod
for ($i = 0; $i < 4 ; $i++) {
for ($j = 0; $j < 4; $j++) {
if ($i==$j)
{
$val=0;
}
elsif($j<$i)
{
$val=-1;
}
else{
$val=1;
}
$comparisonAdjectives[$i][$j] = $val;
}
}
for ($i = 0; $i < 4 ; $i++) {
for ($j = 0; $j < 4; $j++) {
if ($comparisonAdjectives[$i][$j]>=0){
print " ".$comparisonAdjectives[$i][$j]." ";
}
else
{
print $comparisonAdjectives[$i][$j]." ";
}
}
print "\n";
}
}
printMat();
@alphabets = (A .. F);
print "Original: @alphabets \n";
push (@alphabets, 'G'); # add G to the last index array
print "Pushing G in the array: @alphabets\n";
pop (@alphabets); # removing last element of array
print "Removing last element from array: @alphabets\n";
pop (@alphabets); # removing last element of array
print "Removing last element from array: @alphabets\n\n";
In Perl, we can use the index
method to get the first position/occurrence of a substring
in another string. If the substring does not exist, the index
returns -1
.