Monday, 29 July 2024

How to Check if a Directory Exists in a Bash Shell Script

When working with Bash shell scripts, one common task is checking if a specific directory exists. This can be crucial for ensuring that scripts do not fail due to missing directories. Below, we’ll explore various methods to check for the existence of a directory, complete with updated code examples for 2024.

Read more »

Labels:

Friday, 2 October 2020

write 1000 uniq perl interview questions and answers to master in perl - part1

  1. Q: In Perl, what does the sigil $_ represent?

    A: In Perl, the sigil $_ represents the default argument to a subroutine. It is a global variable that is set automatically by Perl whenever a subroutine is called.

  2. Q: In Perl, what does the backslash \ do in front of a variable name?

    A: In Perl, the backslash \ in front of a variable name is used to force a string interpretation of the variable, rather than a numeric interpretation. This can be useful when working with non-numeric variables, or when you want to avoid potential side effects caused by Perl's built-in operations on numbers

Read more »

Labels:

Monday, 27 January 2025

Perl Built-ins Quick Reference

1. String Functions

length

Returns the length of a string.

my $str = "Hello, World!";
my $len = length($str);
print "Length: $len\n";  # Output: Length: 13

substr

Extracts a substring from a string.

my $str = "Hello, World!";
my $sub = substr($str, 0, 5);
print "Substring: $sub\n";  # Output: Substring: Hello

index

Returns the position of a substring within a string.

my $str = "Hello, World!";
my $pos = index($str, "World");
print "Position: $pos\n";  # Output: Position: 7

rindex

Returns the last position of a substring within a string.

my $str = "Hello, World! World!";
my $pos = rindex($str, "World");
print "Last Position: $pos\n";  # Output: Last Position: 14

uc

Converts a string to uppercase.

my $str = "Hello, World!";
my $uc_str = uc($str);
print "Uppercase: $uc_str\n";  # Output: Uppercase: HELLO, WORLD!

lc

Converts a string to lowercase.

my $str = "Hello, World!";
my $lc_str = lc($str);
print "Lowercase: $lc_str\n";  # Output: Lowercase: hello, world!

ucfirst

Converts the first character of a string to uppercase.

my $str = "hello, world!";
my $ucfirst_str = ucfirst($str);
print "Ucfirst: $ucfirst_str\n";  # Output: Ucfirst: Hello, world!
Read more »

Labels:

Sunday, 8 March 2020

Perl - Backtick Tutorial

backticks are used to execute shell commands and capture the output. The general syntax for using backticks in Perl is:

$output = `command`;


Here, the command is enclosed in backticks, and the output is captured into the variable $output.

For example, if you want to capture the output of the ls command, you can use:

$files = `ls`;

The variable $files will now contain the output of the ls command.

You can also use variables in the command that is enclosed in backticks:

$dir = "/path/to/directory";

$files = `ls $dir`;

Here, the value of the $dir variable is used in the ls command.

Backticks can also be used in combination with other Perl functions to process the output. For example, you can split the output into an array:

$files = `ls`;

@file_list = split(/\n/, $files);

Here, the split function is used to split the output of the ls command into an array, with each element containing a file name.

Backticks can also be used in conditional statements. For example, you can check if a file exists using the test command:

$file = "myfile.txt";

if (`test -e $file`) {

    print "$file exists\n";

} else {

    print "$file does not exist\n";

}

Here, the test command is enclosed in backticks and used in the conditional statement to check if the file exists.

It's important to note that using backticks to execute shell commands can be a security risk if the input is not properly sanitized. Make sure to validate and sanitize any user input before using it in a shell command.

Labels:

Thursday, 17 March 2022

how to atomically update values in a ConcurrentHashMap in Java using Compute and ComputeIfAbsent methods?

Hi,  ConcurrentHashMap is a thread-safe implementation of the Java Map interface, designed to be used in multi-threaded environments. It allows multiple threads to access and modify the map simultaneously without causing data corruption or race conditions.

One of the key features of ConcurrentHashMap is that it provides atomic operations to update its elements. Atomic operations are operations that are performed as a single, indivisible unit of work, which means they are guaranteed to be executed completely or not at all. This is important in multi-threaded environments where multiple threads may try to modify the same element at the same time.

Read more »

Labels: , ,

Sunday, 15 January 2023

Perl Interview Questions and Answers - March 2023 Updated 2

 Find Lowest Missing Numbers in perl

The given program is a Perl script designed to find the lowest missing numbers from an array of integers. The script begins by initializing an array of integers named "@arr", which contains a set of random integers. The script then creates a hash called "%hash" that maps each integer in "@arr" to the value 1.

Next, the script finds the maximum integer in "@arr" using the "sort" function and saves it to a variable named "$max". It also sets the value of the minimum integer to -1, and initializes an empty array called "@missing" to store the missing numbers.

Read more »

Labels:

Monday, 4 November 2024

Changing an Element’s Class with JavaScript: Simple and Modern Techniques

In JavaScript, modifying the CSS classes of HTML elements allows for dynamic styling changes based on user interactions or other events. Changing classes is especially useful for toggling visibility, updating themes, or applying specific effects. Here’s a look at different ways to change an element’s class with JavaScript, from basic to more advanced techniques.

1. Directly Setting the className Property

The simplest way to change an element’s class is to use the className property. This approach overrides any existing classes, so it’s best used when you want to replace the current class entirely.

document.getElementById("myElement").className = "newClass";

This method sets the class to "newClass" on the specified element. For multiple classes, you can provide a space-separated list:

document.getElementById("myElement").className = "class1 class2";
Read more »

Labels:

Friday, 6 June 2025

Multiple-Choice Quiz on Python Dictionaries

  1. What is the primary structure used to store data in a Python dictionary?
    A) List
    B) Set
    C) Key-Value pairs
    D) Tuple

  2. Which of the following is true about the keys in a Python dictionary?
    A) They can be mutable data types
    B) They must be unique
    C) They can be of any data type, including lists
    D) They are automatically sorted

  3. How do you access the value associated with a key in a Python dictionary?
    A) Using the index number
    B) Using the key itself in square brackets
    C) Using the key in parentheses
    D) Using the key with a dot notation

  4. What will be the output of the following code: d = {'a': 1, 'b': 2}; print(d['b'])?
    A) 1
    B) ‘b’
    C) 2
    D) KeyError

  5. Which method would you use to add a new key-value pair to an existing dictionary?
    A) append()
    B) add()
    C) update()
    D) insert()

  6. If you want to remove a key-value pair from a dictionary, which method would you typically use?
    A) delete()
    B) pop()
    C) remove()
    D) discard()

  7. What will happen if you try to access a key that does not exist in a dictionary?
    A) It returns None
    B) It creates a new key with a default value
    C) It raises a KeyError
    D) It returns an empty string

  8. Which of the following methods can be used to retrieve all keys in a dictionary?
    A) keys()
    B) get_keys()
    C) list_keys()
    D) all_keys()

  9. How can you check if a specific key exists in a Python dictionary?
    A) Using the in operator
    B) Using the exists() method
    C) Using the check() method
    D) Using the includes() method

  10. In Python dictionaries, what is the time complexity for accessing a value by key?
    A) O(n)
    B) O(log n)
    C) O(1)
    D) O(n^2)

Answers:

  1. C
  2. B
  3. B
  4. C
  5. C
  6. B
  7. C
  8. A
  9. A
  10. C

 

Labels: