Showing posts with label perl. Show all posts
Showing posts with label perl. Show all posts

Tuesday, 19 November 2019

Perl Series III

How to remove the repeated words in a line, like shown in file below

===============================

vlsispace = "bin/trail/world", "bin/drive/space", "usr/bin/vlsi", "bin/trail/world";

string1 = "bangalore", "chennai", "newyork", "bangalore";

fruit = "apple", "orange", "grape","apple"; 

==================================

In above file few words are repeated in each line and those extra words need to be removed from the lines

First using grep expression try to capture the line and then split the pattern using a split command. This step will convert the string to a array format .
@fields = split(/delimiter/, “pattern”);
Next step try to remove the repeated word in the array. Finally once all the words are unique, join the array to string with the delimiter that was used to split the string. Join command is used to join the words in array
$string=join(“delimiter”,@array)

There are many ways to write the logic and one of the way is given below

==========================================================

#!/usr/bin/perl

open(rh,"","new_file") or die "couldnt open the file, $!;

foreach $line() {

if($line =~/(\".*\");$){

@fields split(/, /,$1);

my %hashes;

my @unique;

foreach my $value(@fields){

if(!$hashes{$value}){

push @unique $value;

$hashes{$value}=1;

}}

$string = join(", ",@unique);

print wh "$string;\n";

}

else {

print wh $line;

}}

close(wh);

close(rh);

==========================================================

This perl scprit will remove the repeated word from the line and prints a new file with a unique word in a line.

Saturday, 5 October 2019

Perl_Series II

Hashes
A hash is an un-ordered group of KEY-VALUE pairs and keys are unique strings(duplication of keys are not allowed). The values are scalar and they can be either a number ,a string or a reference. These are also called as associative arrays.

Before using the hash we have to first declare it 

my %hash ;

As I said above duplication of keys are not allowed and this property can be used to list out the elements without any repetition. Let us consider a array of numbers with repetition.
@numbers =(0,2,3,4,1,4,2,2,3,5,5)

my @unique;
my %hashes;
my @numbers =(0,2,3,4,1,4,2,2,3,5,5);
foreach my $value (@numbers) {
  if (! $hashes{$value}) {    
push @unique, $value;   $hashes{$value} = 1; 
}}
print @unique;

output:023415

Friday, 4 October 2019

Perl_Series I

Assume the file contains 10 words and we need to find the longest word and print on the console with help of perl script. Following are different ways to open a file and read the content using perl

  • less than < sign indicates that file has to be opend in read-only mode.
    open(DATA, “<vlsispace.txt”)
  • greater than > sign indicates that file has to be opend in the writing mode.
    open(DATA, “>vlsispace.txt”)
  • To open a file for updating without truncating it 
    open(DATA, “+<vlsispace.txt”)
  • To truncate the file first 
    open DATA, “+>vlsispace.txt”
  • In this mode, writing point will be set to the end of the file and append new data to file
    open(DATA,”>>vlsispace.txt”)
  • To read the data in a file during append process
    open(DATA,”+>>vlsispace.txt”)

#!usr/bin/perl
open r,”<data.txt” or “cannot open the file”;
@string_words;
while($a=<r>)
{
chomp($a);
$i++;
@string_words[$i]=$a;
}
foreach $d(@string_words)
{
$i=length($d);
if($max<$i)
{
$word=$d;
$max=$i;
}
}
print “@string_words\n”;
print “the longest word:$word\n”;
print “the length of the longest word:$max\n”;
close r;

Physical Cells :TAP CELLS, TIE CELLS, ENDCAP CELLS, DECAP CELLS

Tap Cells (Well Taps) :  These library cells connect the power and ground connections to the substrate and n­wells, respectively.  By plac...