Perl read/write File

This is a simple example how to write into a File with Perl, and then read from it.At first we write the numbers from 1 to 6 into the file, in the second we read from the file and print it on the screen, if you don’t want the “\n” at the end use the function chomp.

If you open a File for read use ‘<‘, for write ‘>’ and for append ‘>>’.

#!/usr/bin/perl
use warnings;
use strict;

#declare filehandler
my ($fh_read,$fh_write);


#open file for write
open($fh_write, ">", './testfile') or die "Failed to open file: $!\n";

#now write
for my $num (1..6){
	print $fh_write "$num\n";
}

#close the file
close($fh_write);


#open file for read
open($fh_read, "<", './testfile') or die "Failed to open file: $!\n";

#now read
while (my $row = <$fh_read>) {
  print $row;
}

#close the file
close($fh_read);

Result looks like:

1
2
3
4
5
6

you could check if a handler is open with this Perl handler function.

Fortran high precision example

This is an example about the high precision in Fortran, at first we see a normal Program:

program hello
    implicit none
    real   :: div
    real   :: i
    Print  *,"write your number:"
    read   *, i
    i= div(i)
    print  *,"your result:",i
end program Hello

real function div( a )
  implicit none
  real  :: a
  div   = a/3
end function div

our Result looks like this:

 write your number:   
4      
 your result:   1.33333337

 

Now we use selected_real_kind(32) to get an higher precision data type:

program hello
    implicit none
    real(kind=selected_real_kind(32)):: div
    real(kind=selected_real_kind(32)) :: i
    Print  *,"write your number:"
    read   *, i
    i= div(i)
    print  *,"your result:",i
end program Hello

real(kind=selected_real_kind(32)) function div( a )
  implicit none
  real(kind=selected_real_kind(32))  :: a
  div   = a/3
end function div

Now we get this result:

 write your number:   
4   
 your result:   1.33333333333333333333333333333333327

looks much better than the first.

Fortran function example

This is an example in Fortran how to make a function:

program hello
    implicit none
    integer:: square
    integer:: i
    Print  *,"write your number:"
    read   *, i
    print  *,"your number:",i
    i= square(i)
    print  *,"your result:",i
end program Hello

integer function square( a )
  implicit none
  integer  :: a
  square   = a*a
end function square

Result:

 write your number: 
4   
 your number:           4  
 your result:          16

C# input output example

This is a little Program written in C sharp with Mono, It reads a number from the Terminal multiplies with itself and then print back:

using System.IO;
using System;

class Program
{
    static void Main()
    {
        Console.Write("write your number: ");//Info text 
        String str = Console.ReadLine();//read input
        Console.WriteLine("your number: "+str);//print input
        int i = Convert.ToInt32(str);//convert to intager
        i = i*i;//calculate
        Console.WriteLine("your result: "+i);//print result
    }
}

To compile run this:

mcs *.cs -out:main.exe

And then start with Mono :

mono main.exe

our result looks like this:

write your number: 4   
your number: 4     
your result: 16

The same Programm in Java is here : Java input output example