Download the Ace AWS DEA-C01 Exam App: iOS - Android
How to declare and write a script with hash tables on windows and linux
A hash table, also known as a hash map, is a data structure that is used to store key-value pairs. It is an efficient way to store data that can be quickly retrieved using a unique key.
Here is an example of how to declare and write a script with a hash table in Python:
# Declare an empty hash table hash_table = {}
# Add some key-value pairs to the hash table hash_table[‘key1’] = ‘value1’ hash_table[‘key2’] = ‘value2’ hash_table[‘key3’] = ‘value3’
# Access a value using its key print(hash_table[‘key2’]) # Output: “value2”
# Modify a value using its key hash_table[‘key2’] = ‘new value’ print(hash_table[‘key2’]) # Output: “new value”
# Delete a key-value pair using the `del` statement del hash_table[‘key1’]
# Check if a key is in the hash table using the `in` operator print(‘key1’ in hash_table) # Output: False
# Output: False
In this example, we declare an empty hash table using the {} syntax. We then add some key-value pairs to the hash table using the [] syntax. We access a value using its key, modify a value using its key, delete a key-value pair using the del statement, and check if a key is in the hash table using the in operator.
I hope this helps! Let me know if you have any questions.
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};
On both Windows and Linux, you can use the grep command in combination with the | (pipe) operator to filter the output of another command. The | operator takes the output of the command on the left and passes it as input to the command on the right.
Here is an example of how to use the grep command with the | operator on both Windows and Linux:
On Linux:
# List all the files in the current directory and filter the output to show only the files that contain the word "example" ls | grep example
On Windows:
# List all the files in the current directory and filter the output to show only the files that contain the word "example" dir | findstr example
In this example, the ls (Linux) or dir (Windows) command lists all the files in the current directory, and the grep (Linux) or findstr (Windows) command filters the output to show only the lines that contain the word “example”.
You can use the grep command with the | operator in combination with other command-line utilities to perform various tasks. For example, you can use the grep command to filter the output of the ps command to show only the processes that contain a particular string in their command line arguments.
Using the rev command: The rev command is a utility that reverses the lines of a file or the characters in a string. To reverse a string, you can use the echo command to pass the string to rev:
echo"string" | rev
Using the sed command: The sed command is a powerful utility that can perform various text transformations. To reverse a string, you can use the sed command with the -r option and the 's/.*(.)/\1/g' expression:
echo"string" | sed -r 's/.*(.)/\1/g'
Using the awk command: The awk command is a programming language that is used for text processing. To reverse a string, you can use the awk command with the {print} action:
echo"string" | awk '{print $1}'
On Windows:
Using the powershell command: The powershell command is a shell that provides a command-line interface for Windows. To reverse a string, you can use the powershell command with the -C option and the '[System.Text.Encoding]::Unicode.GetString([System.Text.Encoding]::Unicode.GetBytes("string"))' expression:
Using the cmd command: The cmd command is the command-line interpreter for Windows. To reverse a string, you can use the cmd command with the for loop:
cmd /c "for /L %i in (1,1,%len%) do @echo !string:~%len%-%i,1!"
These are some ways to reverse a string on Linux and Windows. There are other ways to achieve this, using different utilities or programming languages.
Via shell script on Linux
sh-3.2# vi reverse.sh #### Start Script ##### #!/bin/bash input_string=”$1″ reverse_string=””
input_string_length=${#input_string} for (( i=$input_string_length-1; i>=0; i– )) do reverse_string=”$reverse_string${input_string:$i:1}” done
::—————- :: 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.
Today I Learned (TIL) You learn something new every day; what did you learn today? Submit interesting and specific facts about something that you just found out here.
Reddit Science This community is a place to share and discuss new scientific research. Read about the latest advances in astronomy, biology, medicine, physics, social science, and more. Find and submit new publications and popular science coverage of current research.