Friday, 19 March 2021

perl data structures tutorial - part1

Perl provides a rich set of data structures that can be used to store and manipulate data. In this tutorial, I'll cover the most commonly used data structures in Perl, along with code examples.

Arrays

Arrays are ordered lists of scalar values. To declare an array, you use the @ symbol followed by the array name. Here's an example:

@my_array = (1, 2, 3, 4, 5);


You can access individual elements of the array using the array name followed by the index of the element in square brackets. Here's an example:

print $my_array[0]; # prints 1


You can also use the scalar function to get the number of elements in an array:

print scalar(@my_array); # prints 5


Read more »

Labels:

Friday, 8 May 2020

perl program to Merging Files into a Single File

This Perl script reads all the files with a given extension in the current directory and merges them into a single file. The merged file name can be passed as a command-line argument.

Method 1:Using file handles

#!/usr/bin/perl

my ($ext, $merged_file) = @ARGV;

my @files = glob "*.$ext";

open my $out_fh, '>', $merged_file or die "Couldn't open $merged_file: $!";

foreach my $file (@files) {

    open my $in_fh, '<', $file or warn "Couldn't open $file: $!";

    while (my $line = <$in_fh>) {

        print $out_fh $line;

    }

    close $in_fh;

}

close $out_fh;

Save the script as merge.pl and run it with perl merge.pl txt merged.txt. This will merge all the files with the extension "txt" into a single file called "merged.txt".

Method 2: Using cat command

This method uses the cat command to concatenate multiple files into a single file.

#!/usr/bin/perl

use strict;

use warnings;

my ($output_file, @input_files) = @ARGV;

system("cat @input_files > $output_file");


Method 3: Using File::Slurp

This method uses the File::Slurp module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use File::Slurp;

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= read_file($input_file);

}

write_file($output_file, $output_contents);


Method 4: Using IO::All

This method uses the IO::All module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use IO::All;

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= io($input_file)->all;

}

io($output_file)->print($output_contents);

Save the script as merge_files_method4.pl and run it with perl merge_files_method4.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.


Method 5: Using File::MergeSort

This method uses the File::MergeSort module to merge sorted files into a single file.

#!/usr/bin/perl

use strict;

use warnings;

use File::MergeSort;

my ($output_file, @input_files) = @ARGV;

my $sorter = File::MergeSort->new();

$sorter->cmp(sub { $_[0] cmp $_[1] });

foreach my $input_file (@input_files) {

    $sorter->load($input_file);

}

$sorter->save($output_file);

Save the script as merge_files_method5.pl and run it with perl merge_files_method5.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt, sorted.


Method 6: Using Path::Tiny

This method uses the Path::Tiny module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use Path::Tiny;

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= path($input_file)->slurp;

}

path($output_file)->spew($output_contents);

Save the script as merge_files_method6.pl and run it with perl merge_files_method6.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.


Method 7: Using File::Slurper

This method uses the File::Slurper module to read each input file and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use File::Slurper qw(read_text write_text);

my ($output_file, @input_files) = @ARGV;

my $output_contents = '';

foreach my $input_file (@input_files) {

    $output_contents .= read_text($input_file);

}

write_text($output_file, $output_contents);

Save the script as merge_files_method7.pl and run it with perl merge_files_method7.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.


Method 8: Using File::ReadBackwards

This method uses the File::ReadBackwards module to read each input file backwards and append its contents to the output file.

#!/usr/bin/perl

use strict;

use warnings;

use File::ReadBackwards;

my ($output_file, @input_files) = @ARGV;

open(my $out_fh, '>', $output_file) or die "Can't open $output_file: $!";

foreach my $input_file (@input_files) {

    my $in_fh = File::ReadBackwards->new($input_file) or warn "Can't open $input_file: $!";

    while (defined(my $line = $in_fh->readline)) {

        print $out_fh $line;

    }

}

close($out_fh);

Save the script as merge_files_method8.pl and run it with perl merge_files_method8.pl output_file.txt input_file1.txt input_file2.txt. This will create a file called output_file.txt that contains the contents of both input_file1.txt and input_file2.txt.

Labels:

Tuesday, 31 January 2023

Perl Datastructures - Hash of Hash

Perl hash of hashes is a data structure that consists of a hash that contains other hashes as values. Each hash contained in the main hash can be accessed using a unique key.

Here's an example of how to create a hash of hashes in Perl:

my %HoH = (
   'John' => {
      'age' => 35,
      'occupation' => 'Programmer',
      'salary' => 50000,
   },
   'Mary' => {
      'age' => 27,
      'occupation' => 'Designer',
      'salary' => 40000,
   },
   'Mark' => {
      'age' => 42,
      'occupation' => 'Manager',
      'salary' => 70000,
   },
);
Read more »

Labels:

Tuesday, 20 August 2024

Understanding the Impact of <<>> on @ARGV in Perl

You might have encountered a puzzling issue while working with Perl where you expected a deep copy of an array to be preserved, but found that modifications to @ARGV using the <<>> operator affected the original array. Here’s a breakdown of why this happens and how you can avoid such pitfalls.

Read more »

Labels:

Saturday, 16 November 2024

Safest Ways to Iterate Through Perl Hash Keys

When working with Perl hashes, choosing the right way to iterate through keys is essential for avoiding unexpected behavior and ensuring efficient memory usage. Here are the most common methods, their advantages, and the potential pitfalls associated with each.

Iterating with each

The each function retrieves one key-value pair at a time. This is memory-efficient and works well for large hashes or tied hashes.

Read more »

Labels:

Wednesday, 8 July 2020

perl program to Renaming Files with a Prefix

This Perl script renames all the files in the current directory with a given prefix. The prefix can be passed as a command-line argument.

Method 1: Using rename function

#!/usr/bin/perl

my $prefix = shift @ARGV || '';

opendir my $dh, '.' or die "Couldn't open current directory: $!";

while (my $filename = readdir $dh) {

    next if $filename =~ /^\./; # Skip dotfiles

    my $newname = $prefix . $filename;

    rename $filename, $newname or warn "Couldn't rename $filename: $!";

}

closedir $dh;


or


#!/usr/bin/perl

use strict;

use warnings;

use File::Glob;


my $prefix = 'new_';

my $pattern = '*.txt';


foreach my $file (glob $pattern) {

    my $new_file = $prefix . $file;

    rename $file, $new_file or die "Can't rename $file to $new_file: $!";

}

Save the script as rename.pl and run it with perl rename.pl newprefix_. This will add the prefix "newprefix_" to all the files in the current directory.


Method 2: Using File::Copy

This method uses the File::Copy module to copy files with a new name and then delete the old file. We can use the File::Glob module to get a list of files matching a pattern and then use the File::Copy module to copy each file with the new name.

#!/usr/bin/perl

use strict;

use warnings;

use File::Glob;

use File::Copy;

my $prefix = 'new_';

my $pattern = '*.txt';

foreach my $file (glob $pattern) {

    my $new_file = $prefix . $file;

    copy $file, $new_file or die "Can't copy $file to $new_file: $!";

    unlink $file or die "Can't delete $file: $!";

}

Save the script as rename_files_method2.pl and run it with perl rename_files_method2.pl. This will rename all .txt files in the current directory with the prefix new_.


Method 3: Using regular expressions

This method uses regular expressions to modify the filenames with a prefix. We can use the File::Glob module to get a list of files matching a pattern and then use regular expressions to modify each filename.

use strict;

use warnings;


use File::Glob;

my $prefix = 'new_';

my $pattern = '*.txt';

foreach my $file (glob $pattern) {

    my $new_file = $file;

    $new_file =~ s/^/$prefix/;

    rename $file, $new_file or die "Can't rename $file to $new_file: $!";

}

Save the script as rename_files_method3.pl and run it with `perl rename_files

all .txt files in the current directory with the prefix new_.


Method 4: Using File::Find

This method uses the File::Find module to recursively find and rename files with a prefix. The File::Find module provides a way to traverse a directory tree and apply a function to each file found. We can use the rename function within the wanted function to rename each file with the specified prefix.

#!/usr/bin/perl

use strict;

use warnings;

use File::Find;

my $prefix = 'new_';


sub rename_file {

    if (-f && /^.*\.txt$/) {

        my $new_file = $prefix . $_;

        rename $_, $new_file or warn "Can't rename $_ to $new_file: $!";

    }

}

find(\&rename_file, '.');

Save the script as rename_files_method4.pl and run it with perl rename_files_method4.pl. This will recursively find and rename all .txt files in the current directory and its subdirectories with the prefix new_.


Method 5: Using opendir and readdir

This method uses the opendir and readdir functions to find and rename files with a prefix. The opendir function opens a directory, and the readdir function reads the contents of the directory. We can use regular expressions to match the filenames and then use the rename function to rename each file with the specified prefix.

#!/usr/bin/perl

use strict;

use warnings;

my $prefix = 'new_';

opendir(my $dh, '.') or die "Can't open current directory: $!";

while (my $file = readdir($dh)) {

    next unless $file =~ /^.*\.txt$/;

    my $new_file = $prefix . $file;

    rename $file, $new_file or warn "Can't rename $file to $new_file: $!";

}

closedir $dh;

Save the script as rename_files_method5.pl and run it with perl rename_files_method5.pl. This will find and rename all .txt files in the current directory with the prefix new_.


Method 6: Using File::Copy

This method uses the File::Copy module to copy files with a prefix to a new directory and then rename the files. The File::Copy module provides functions for copying and moving files. We can use the copy function to copy the original files to a new directory with the prefix, and then use the rename function to rename the files with the specified prefix.

use strict;

use warnings;

use File::Copy;

my $prefix = 'new_';

my $new_dir = 'new_files';

mkdir $new_dir unless -d $new_dir;

opendir(my $dh, '.') or die "Can't open current directory: $!";

while (my $file = readdir($dh)) {

    next unless $file =~ /^.*\.txt$/;

    my $new_file = $prefix . $file;

    copy($file, "$new_dir/$new_file") or warn "Can't copy $file: $!";

}

closedir $dh;

opendir($dh, $new_dir) or die "Can't open $new_dir: $!";

while (my $file = readdir($dh)) {

    next unless $file =~ /^.*\.txt$/;

    my $new_file = $prefix . $file;

    rename("$new_dir/$file", "$new_dir/$new_file") or warn "Can't rename $file: $!";

}

closedir $dh;

Save the script as rename_files_method6.pl and run it with perl rename_files_method6.pl. This will copy all .txt files in the current directory to a new directory named new_files with the prefix new_, and then rename the files with the specified prefix.


Method 7: Using File::Find::Rule

This method uses the File::Find::Rule module to find and rename files with a prefix. The File::Find::Rule module provides a simple interface for finding files that match specified criteria. We can use the name method to match filenames and then use the rename function to rename each file with the specified prefix.

use strict;

use warnings;

use File::Find::Rule;

my $prefix = 'new_';

my @files = File::Find::Rule->file()->name('*.txt')->in('.');

foreach my $file (@files) {

    my $new_file = $prefix . $file;

    rename $file, $new_file or warn "Can't rename $file to $new_file: $!";

}

Save the script as rename_files_method7.pl and run it with perl rename_files_method7.pl. This will find and rename all .txt files in the current directory with the prefix new_.

Labels:

Wednesday, 6 November 2019

perl - array and hash common differences

     S.No
                             ARRAYS
                           HASHES

         1.

         2.


         3.

          
          4.

     
          


          5.

          



         6.





           7.


           
         8.


















          

        9.






      10.









      11.




     12.



      13.






       14.






       15.

It is ordered data collection

Data Can be accessed based on index value

Stack operations performed using push,pop,shift,unshift

Duplicate Elements will be exist in Array




For Large data, Arrays will not be in speed as getting values from hash keys.


Arrays can be created using () brackets @array=(1,3,5,6,2);
For More methods refer this link.

for accessing array index
$array[1];


Array can be accessed in loops as below

1.For(@array)
{
}

2.foreach(@array1,@array2)
   {
       Print $_;
   }

3.print grep{},@array;

4. print map{},@array;







Array slices can be used as below
@array[2];





To find length of an array


 $#array+1;
       or
scalar(@array);
      or
$a=@array;


For Emptying Array
$#array = -1;
     or
@array = ( );

To reverse an array
print reverse @array;


To sort array Elements
ascending order
@a=sort{$a <=> $b }@array;
desending order
@a=sort{$b <=> $a }@array;


To add array elements
$array[0]='55';




To Delete array elements
delete $array[1];
 
It is unordered Data Collection

Data Can be accessed based on Key value

Stack operations cannot be performed


Keys will not have Duplicate elements but Values would have Duplicate elements.

Based on hash Keys Data retrival would be fast


Hash is also possible to create using () bracket %hash=(1,3,5,6,2);
For More methods refer this link.
https://kaavannan-perl.blogspot.com/2019/10/perl-hashes-what-is.html


For accessing hash index use the key name
$hash{1};


Hash can be accessed based on keys and values


While(($k,$vals) = each %h)
{
    Print “$k,$vals\n”;
}

Foreach $k (keys %h)
{
Print “$k => $hash{$k}\n”;
}

Foreach $values (values %hash)
{
Print “$values\n”;
}



Hash slices can be used as below,
$hash{'1'}







To find length of hash

@array=keys %hash;
scalar(@array);
     or
$#array+1;
     or
$a=@array;



For Emptying hash
%hash=( );




To reverse a hash
print reverse %hash;



To sort hash elements

 foreach $k (sort keys %hash)
{
   print "$k => $hash{$k}\n";
}



To add hash Elements
$hash{1}='one';





To Delete hash elements
delete($hash{'1'});


Labels:

Friday, 16 August 2024

Efficiently Iterating Over Java Map Entries

 


Iterating over entries in a Java Map is a common task in software development, especially when you need to access both keys and values during processing. The efficiency of iteration can vary based on the Map implementation you choose, and understanding these differences is crucial for optimizing performance. Here’s a comprehensive guide on how to iterate over a Java Map, covering different methods and their implications depending on the Map implementation.

Read more »

Labels: