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};
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”)
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.
submitted by /u/Outrageous-Catch4731 [link] [comments]
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.