• 1

    ..

  • 2

    ...

  • 3

    ...

Tuesday, 5 April 2016

Perl – File Read Write Example

Perl – File Read Write Example


In this post, let’s see how to remove the new line character or line break in a text file and save the result in another text file.

Refer to other posts File handling, RegEx.

To accomplish the removal of new line character, we use RegEx substitution operator. The code for substitution is as below,
    $a =~ s/\n//;   # $a is the variable in which the replacement takes place


·         The input to the script is through command line arguments. The command line inputs are stored in the array variable @ARGV (please note the CAPS). First we check the size. If no command line arguments is specified, the size of ARGV is -1. If so, the script prints the usage & exits, using the in-built function die().

unless($#ARGV <0) {
    $in_file=$ARGV[0];
} else {
    die "\nUsage:\n remove_new_line.pl <input_file>\n";
}


·         If valid input is provided, then open the specified file in read mode as below. If unable to open the file, exit with a note printed.  $! Is the Perl system variable which prints the error from the OS.
  open FIN, "<$in_file" or die "Unable to read '$in_file' :$!";


·         Create the output file by appending the name “new_” to the input file name.
open FOUT, ">new_$in_file" or die "Unable to create 'new_$in_file' :$!";


·         Then create a loop, that executes for each line of the input file. Then remove the newline character and write back the result to output file.
while ($a=<FIN>) {
  # remove \n character
    $a =~ s/\n//;
  # write the new data to text file
    print FOUT $a;
 }

Sample output of this script:
            Top file is the input & bottom file is the output.







Monday, 14 March 2016

Perl string manipulation with examples


  Perl offers many easier ways to manipulate string, which is yet powerful with RegEx. In this post, the various functions available in Perl for string manipulation is explained with examples. 

Saturday, 12 March 2016

Perl Regular expression - Perl RegEx with examples

   A regular expression or RegEx is a string of characters that define the pattern or patterns you are viewing. The syntax of regular expressions in Perl is very similar to what you will find within other regular expression, supporting programs, such as sedgrep, and awk


   In this post, Perl regex is illustrated with examples.


Friday, 11 March 2016

Glitch Free Clock Gating - verilog good clock gating


   Clock gating is a popular technique used in many synchronous circuits for reducing dynamic power dissipation. This saves power by adding more logic to a circuit to the clock by disabling clock switching, so that the flip-flops in them do not have to switch states. As a result, the switching power consumption goes to zero, and only leakage currents are incurred.

   Clock gating logic can be added into a design in a variety of ways:
  1. Coded into the RTL code as enable conditions that can be automatically translated into clock gating logic by synthesis tools.
  2. Inserted into the design manually by the RTL designers (typically as module level clock gating) by instantiating library specific ICG (Integrated Clock Gating) cells to gate the clocks of specific modules or registers.
  3. Semi-automatically inserted into the RTL by automated clock gating tools. These tools either insert ICG cells into the RTL, or add enable conditions into the RTL code. These typically also offer sequential clock gating optimisations.
   Poor clock gating produces glitches in the output clock, making unwanted clock transitions which may lead to timing violations,etc., and increased power consumption.
   Here is an Verilog example illustrating the RTL code for clock gating & its issues.

The below code produces simple clock gating mechanism with an 2-input AND gate, with inputs as CLK & CLK_EN. But the greatest disadvantage is that it produces glitches in output as in the below waveform.
//BAD clock gating, can cause glitches in output
 assign clk_out1 = c_en && clk;

To overcome the glitches, a latching needs to be added to change the enable only when CLK is high/low. By this way, glitches are avoided & produces a good clock for the rest of the block. 
//GOOD clock gating & glitch free
 always @ (c_en or clk) begin
     if (!clk)
        en_out2 = c_en; // build latch
 end
 assign clk_out2 = en_out2 && clk;

Circuit synthesized for the above codes:
Verilog RTL - Clock gating- circuit :: ELecDude

Waveform for the above code:




If you enjoyed this post plz let us know your views via comments.
This helps us to do much more better.
Thankyou.


Wednesday, 23 December 2015

Download Verilog codes for SPI Master and Slave modules



Sharing below the links for SPI Verilog codes for Master and Slave modules.


SPI means Serial Pheripheral Interface, is a synchronous serial data link operating in full duplex mode.

For detailed information of the Verilog code of SPI Master and Slaves modules, proceed to the below links.

 For Verilog code of SPI Master and Slaves modules code download & discussions, click here.



Monday, 2 November 2015

PERL TUTORIAL PART 4 - Working with files

FILE HANDLING


     The basics of handling files are simple, just associate a filehandle with an external file and then use a variety of operators and functions within Perl to read and update the data stored within the data stream associated with the filehandle. The filehandle is the name for an I/O connection between the Perl process and the outside world.
     A filehandle is a named internal Perl structure that associates a physical file with a name. The mode of operation must be specified for the filehandle is opened.
     Three basic file handles are - STDIN, STDOUT, and STDERR which represent standard input, standard output and standard error devices respectively. Apart from these, any number of filehandles can be added in the code.
The different file operations are:
1) Opening a file
2) Closing the file
3) File Reading
4) File Writing
5) File tests

1) Opening a file

The syntax for opening a file is as follows:
open(FILEHANDLE,"<mode>file_name");
or simply
open(FILEHANDLE,"mode","file_name");
where mode specifies the type of file access.
MODE SYMBOL
ACCESS TYPE
< or r
Read Only Access
> or w
Creates, Writes, and Truncates
>> or a
Writes, Appends, and Creates
+< or r+
Reads and Writes
+> or w+
Reads, Writes, Creates, and Truncates
+>> or a+
Reads, Writes, Appends, and Creates
Most commonly used are <, >, >>.
It is a good practice to use capital letters for filehandles. And for safe operation, it is always recommended to use the DIE(....) function for file open which will terminate the program when it fails to open the file.
open(FILEHANDLE,"mode","file_name") or die "Unable to open the file(file_name)";

Example:
open(FH1R,"<","file1.txt") or die "Unable to open the file(file1.txt) in read mode!";
open(FH2W,">file2.txt") || die "Unable to open the file(file2.txt) to write!";
$logfilename="log.txt"
open(LOG,">>",$logfilename) || die "Unable to open the file(log.txt) in append mode!!!";

2) Closing the file

To close a file simply call the CLOSE(...) function with the filehandle as input. All the filehanldes are automatically closed when the Perl program terminates. The syntax is
close(FILEHANDLE);

3) Reading from a file

To read the contents from the file, <> operator is used, just like STDIN.
Example:
$line_1=<FH1R>; #reads only one line
@lines=<FH1R>; #reads the entire file, & places in the array

4) Writing to a file

Writing data to a file is simply like printing to STDOUT, except the filehandle is used between the print keyword & data.
Example:
print FH2W "Hello!! This is 1st line...\n";

5) File testing

File tests are performed on file handles to determine the file properties. For example –e FileHandle returns true if the file pointed by FileHandle exists in the system. The below table shows the different file test that can be performed in Perl. Note that the CAPITAL and small letter tests produces different results.
PARAMETER
DESCRIPTION
-r
File or directory is readable
-w
File or directory is writable
-x
File or directory is executable
-o
File or directory is owned by user
-R
File or directory is readable by real user, not effective user
(differs from -r for setuid programs)
-W
File or directory is writable by real user, not effective user
(differs from -w for setuid programs)
-X
File or directory is executable by real user, not effective user
(differs from -x for setuid programs)
-O
File or directory is owned by real user, not effective user
(differs from -o for setuid programs)
-e
File or directory exists
-z
File exists and has zero size (directories are never empty)
-s
File or directory exists and has nonzero size (the value is the size in bytes)
-f
Entry is a plain file
-d
Entry is a directory
-S
Entry is a socket
-p
Entry is a named pipe (a "fifo")
-t
isatty() on the filehandle is true
-T
File is "text"
-B
File is "binary"
-c
Entry is a character-special file
-M
Modification age in days
-A
Access age in days
-C
Inode -modification age in days

Example:
if (-e FH1)
print "File FH1 exists\n";
if (-T FH1)
print "File FH1 exists & is a Text file\n";
$size= -s FH1;
print "File size is $size bytes...\n";
We welcome your valuable comments and suggestion.
It helps us to do better in future.
Thank you.

Saturday, 17 October 2015

Sort numbers using C# with and without .net feature.

The following Program is a Example of getting 10 integer numbers (from -1000 to 1000) from User and Store it in an array and sort them and Display. .



WITH .NET Feature :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
   class Program
   {
       static void Main(string[] args)
       {
           String num_str;
           int[] num = new int[10];
           int temp;
           bool num_flag = false;

           Console.WriteLine("Enter 10 integer number:");
           for(int i = 0 ; i < 10 ; i++)
           {
               Console.WriteLine("enter {0} number",i);
               num_str = Console.ReadLine();
               num_flag = int.TryParse(num_str, out temp);

               while (num_flag == false)
               {
                    Console.WriteLine("please Enter interger");
                    num_str = Console.ReadLine();
                    num_flag = int.TryParse(num_str, out temp);
               }
               if ((temp >= -1000) && (temp <= 1000))
               {
                   num[i] = temp;
               }
               else
               {
                   Console.WriteLine("Please Enter number (-1000 <= x  <= 1000");
                   i--;
               }
           }
           
           Array.Sort(num);
       foreach (int value in num)
       {
           Console.Write(value);
           Console.Write(' ');
       }
           Console.ReadLine();
       }
   }
}

WITHOUT .NET Feature :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication6
{
   class Program
   {
       
           static void Main(string[] args)
           {
           String num_str;
           int[] num = new int[10];
           int temp;
           bool num_flag = false;

           Console.WriteLine("Enter 10 integer number:");
           for(int i = 0 ; i < 10 ; i++)
           {
               Console.WriteLine("enter {0} number",i);
               num_str = Console.ReadLine();
               num_flag = int.TryParse(num_str, out temp);

               while (num_flag == false)
               {
                    Console.WriteLine("please Enter interger");
                    num_str = Console.ReadLine();
                    num_flag = int.TryParse(num_str, out temp);
               }
               if ((temp >= -1000) && (temp <= 1000))
               {
                   num[i] = temp;
               }
               else
               {
                   Console.WriteLine("Please Enter number (-1000 <= x  <= 1000");
                   i--;
               }
           }
             
           //Array.Sort(num);

           for (int x = 0; x < num.Length; x++)
           {
               for (int y = 0; y < num.Length - 1; y++)
               {
                   if (num[y] > num[y + 1])
                   {
                       int temp1 = num[y + 1];
                       num[y + 1] = num[y];
                       num[y] = temp1;
                   }
               }
           }
       foreach (int value in num)
       {
           Console.Write(value);
           Console.Write(' ');
       }
           Console.ReadLine();

       }
   }
}


If you enjoyed this post plz let us know your views via comments.
This helps us to do much more better.
Thankyou.
 

Saturday, 29 August 2015

APPLICATION WAIT TIMER USING VBSCRIPT

tim = InputBox("Please enter wait time [min:sec]")
a=Split(tim,":")
sec=a(1)*1000
min=a(0)*60*1000
Wscript.sleep (min + sec)
msgbox "Hey! " & a(0) & " min " & a(1) & " secs elapsed!!!"




If you enjoyed this post plz let us know your views via comments.
This helps us to do much more better.
Thankyou.

join us 

Saturday, 7 March 2015

VHDL Reserved Words - ELECDUDE

VHDL Reserved Words

KEYWORD
DESCRIPTION
abs
operator, absolute value of right operand. No () needed.
access
used to define an access type, pointer
after
specifies a time after NOW
alias
create another name for an existing identifier
all
dereferences what precedes the .all
and
operator, logical "and" of left and right operands
architecture
a secondary design unit
array
used to define an array, vector or matrix
assert
used to have a program check on itself
attribute
used to declare attribute functions
begin
start of a begin end pair
block
start of a block structure
body
designates a procedure body rather than declaration
buffer
a mode of a signal, holds a value
bus
a mode of a signal, can have multiple drivers
case
part of a case statement
component
starts the definition of a component
configuration
a primary design unit
constant
declares an identifier to be read only
disconnect
signal driver condition
downto
middle of a range 31 downto 0
else
part of "if" statement, if cond then ... else ... endif;
elsif
part of "if" statement, if cond then ... elsif cond ...
end
part of many statements, may be followed by word andid
entity
a primary design unit
exit
sequential statement, used in loops
file
used to declare a file type
for
start of a for type loop statement
function
starts declaration and body of a function
generate
make copies, possibly using a parameter
generic
introduces generic part of a declaration
group
collection of types that can get an attribute
guarded
causes a wait until a signal changes from False to True
if
used in "if" statements
impure
an impure function is assumed to have side effects
in
indicates a parameter in only input, not changed
inertial
signal characteristic, holds a value
inout
indicates a parameter is used and computed in and out
is
used as a connective in various statements
label
used in attribute statement as entity specification
library
context clause, designates a simple library name
linkage
a mode for a port, used like buffer and inout
literal
used in attribute statement as entity specification
loop
sequential statement, loop ... end loop;
map
used to map actual parameters, as in port map
mod
operator, left operand modulo right operand
nand
operator, "nand" of left and right operands
new
allocates memory and returns access pointer
next
sequential statement, used in loops
nor
operator, "nor" of left and right operands
not
operator, complement of right operand
null
sequential statement and a value
of
used in type declarations, of Real ;
on
used as a connective in various statements
open
initial file characteristic
or
operator, logical "or" of left and right operands
others
fill in missing, possibly all, data
out
indicates a parameter is computed and output
package
a design unit, also package body
port
interface definition, also port map
postponed
make process wait for all non postponed process to suspend
procedure
typical programming procedure
process
sequential or concurrent code to be executed
pure
a pure function may not have side effects
range
used in type definitions, range 1 to 10;
record
used to define a new record type
register
signal parameter modifier
reject
clause in delay mechanism, followed be a time
rem
operator, remainder of left operand divided by rightop
report
statement and clause in assert statement, string output
return
statement in procedure or function
rol
operator, left operand rotated left by right operand
ror
operator, left operand rotated right by right operand
select
used in selected signal assignment statement
severity
used in assertion and reporting, followed by a severity
signal
declaration that an object is a signal
shared
used to declare shared objects
sla
operator, left operand shifted left arithmetic by right op
sll
operator, left operand shifted left logical by rightop
sra
operator, left operand shifted right arithmetic by right
srl
operator, left operand shifted right logical by rightop
subtype
declaration to restrict an existing type
then
part of if condition then ...
to
middle of a range 1 to 10
transport
signal characteristic
type
declaration to create a new type
unaffected
used in signal waveform
units
used to define new types of units
until
used in wait statement
use
make a package available to this design unit
variable
declaration that an object is a variable
wait
sequential statement, also used in case statement
when
used for choices in case and other statements
while
kind of loop statement
with
used in selected signal assignment statement
xnor
operator, exclusive "nor" of left and right operands
xor
operator, exclusive "or" of left and right operands

Search Here...

Loading...