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:

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:

Monday 18 November 2019

Perl Unlink - what is?

Perl Unlink:
perl unlink is used to remove the files. based on our requirements we can delete more than single file using unlink function.
by storing array or list elements  as filename we can achieve group of filenames deletion.


Cons:
1. Unlink will not notify which files could not remove. but it can be manageable using perl warning functions.
2. Unlink will not work on many os.
3. Super user permission necessary for Unlinking a directory although it is not recommended for usual coding.

Read more »

Labels: ,

Wednesday 13 November 2019

perl sort keys - hash alphabetically


%hashmap=('awk','perl','code','things','sed','need');

Method 1:[alphabetically]


foreach $keys (sort keys %hashmap) {

    print "$food is $hashmap{$keys}.\n";

}


Method 2:[Sort Based on Value]
foreach $keys (sort { $hashmap{$a} cmp $hashmap{$b} }

                keys %hashmap)

{

    print "$keys is $hashmap{$keys}.\n";

}

Method 3:[sort Based on value Length]
@length = sort { length($hashmap{$a}) <=> length($hashmap{$b}) } keys %hashmap;

foreach $keys (@length) {

    print "$keys is $hashmap{$keys}.\n";

}

kaavannan perl blogspot

Labels: , ,

Tuesday 10 September 2019

How Many Possible ways to remove Duplicates in an array using perl?


Method 1:

sub filter {
    my %hash;
    grep !$hash{$_}++, @_;
}

my @array = qw(e f g h i);
my @final= filter(@array);


Method 2:

use List::MoreUtils qw(uniq);
my @array = qw(1 1 1 2 3 4 4);
my @final= uniq(@array);


Method 3:

my %hash = ();
foreach my $item (@array)
{
    $hash{$item}++;
}
my @final = keys %hash;


Method 4:

my @array=qw( 3 2 3 4 4 3 2);
my @final=keys %{{ map{$_=>1}@array}};
print join ' ', sort{$a<=>$b} @final;


Method 5:

%hash=();
my @array = qw(e j g h i);
@final = grep { ! $hash{$_} ++ } @array;


Method 6:

my @array;
my @finals;

foreach my $var ( @array )
{
    if (! grep(/$var/, @finals ))
    {
          push( @finals, $var );
     }
}


Method 7:

my @array = qw(e f g h i);
my %hash;
@hash{@array}=(1..@array);
@final=keys%hash;


Method 8:

my @final = keys {map {$_ => 1} @array};


Method 9:

@array = qw(e f g h i);
%hash1   = map { $_ => 1 } @array;
@final = keys %hash1;


Method 10:
my @array = qw(e f g h i);
my %hash;
foreach my $v (@array){
        $hash{$v}=1;
}
@final=keys%hash;


kaavannan perl blogspot

Labels: , ,