Add new key-value in hashtable:
$states.Add(“Manitoba”,”Winnipeg”)
Remove key-value in hashtable:
$states.Remove(“Manitoba”,”Winnipeg”) Change value in hashtable:
$states.Set_Item(“Ontario”,”Ottawa”) Retrieve value in hashtable:
$states.Get_Item(“Alberta”) Find key in hashtable:
$states.ContainsKey(“Alberta”) Find Value in hashtable:
$states.ContainsValue(“Calgary”) Count items in hashtable:
$states.Count Sort items by Name in hashtable:
$states.GetEnumerator() | Sort-Object Name -descending Sort items by Value in hashtable:
$states.GetEnumerator() | Sort-Object Value -descending
Hash tables with perl on linux or windows
Declaration:
my %hash = (); #Initialize a hash
my $hash_ref = {}; # Initialize a hash reference. ref will return HASH Clear (or empty) a hash
for (keys %hash)
{
delete $hash{$_};
} Clear (or empty) a hash reference
for (keys %$href)
{
delete $href->{$_};
} Add a key/value pair to a hash
$hash{ ‘key’ } = ‘value’; # hash
$hash{ $key } = $value; # hash, using variables
Using Hash Reference
$href->{ ‘key’ } = ‘value’; # hash ref
$href->{ $key } = $value; # hash ref, using variables Add several key/value pairs to a hash
%hash = ( ‘key1’, ‘value1’, ‘key2’, ‘value2’, ‘key3’, ‘value3’ );
%hash = (
key1 => ‘value1’,
key2 => ‘value2’,
key3 => ‘value3’,
);
Copy a hash
my %hash_copy = %hash; # copy a hash
my $href_copy = $href; # copy a hash ref Delete a single key/value pair
delete $hash{$key};
delete $hash_ref->{$key};
::—————-
:: Calc Var Length
::—————-
:: %*=Str to Check
:: Returns %Len%
:: —————
:StrLen %*
set Data=%*
for /L %%a in (0,1,80) do (
set Char=!Data:~%%a,1!
if not “!Char!”==”” (
set /a Len=%%a+1
) else (exit /b)
)
exit /b
::—————
:: Reverse String
::—————
:: %* Str to Reverse
:: Returns %Reverse%
::——————
:ReverseStr %*
set Data=%*
call :StrLen %Data%
for /L %%a in (!Len!,-1,0) do (
set Char=!Data:~%%a,1!
set Reverse=!Reverse!!Char!
)
exit /b
Let’s find how to prompt and read input variables from keyboard while executing a script using shell, perl, python, batch and powershell (windows and Linux)
On Linux via shell
read -p “Enter your name: ” name
echo “Hi, $name. Let’s be friend!”
On Windows via powershell
$name=read-host “Enter your name:”
write-host “Hi $name, Let’s be friend!”
On Windows via batch
Set /p Name=”Enter your name:”
echo “Hi %name%, Let’s be friend!”
On Windows or Linux via perl
print “Enter your name “;
my $name = ;
chomp $name; # Get rid of newline character at the end
print “Hello $name, let’s be friend”;
On Windows or Linux via python
name=input(“Enter your name: “)
print (“Hello ” + name + ” let’s be friend”)
How to browse the internet via command line on Linux and Windows?
On Linux
lynx http://google.ca
If you don’t have lynx on your linux installation, you will have to install it. On Linux Red hat, install it like this:
yum list lynx (to check the availability of the package)
yum -y install lynx (to install the package)
you can also use: curl -0 http://yoursite/index.html to get the source code of a specific file.
On Windows
start /max http://google.ca
Will open the url using your default browser.
How to check how many CPU cores I have on Windows & Linux?
What is a core in a CPU?
In summary, a core is a small CPU or processor built into a big CPU or CPU socket. It can independently perform or process all computational tasks. From this perspective, we can consider a core to be a smaller CPU or a smaller processor within a big processor.
Today, CPUs have been two and 18 cores, each of which can work on a different task. A core can work on one task, while another core works a different task, so the more cores a CPU has, the more efficient it is.
Open a command prompt (Windows) or Terminal (Linux) and type:
Windows: WMIC CPU Get /Format:List
Linux: cat /proc/cpuinfo | grep processor | wc -l
For more details on Linux: ls /sys/devices/system/cpu/
What does 4 CPU cores mean?
A quad-core CPU has four processing cores in a single chip. It is similar to a dual-core CPU, but has four separate processors (rather than two), which can process instructions at the same time.