Java CSV to Array and Array to CSV

This is a little example in Java to convert a CSV String into an Multidimensional Array, after that i convert the Array back into a CSV String, you could choose the delimiter by yourself. In this case i take the “-” to see the different:

public class HelloWorld{

     public static void main(String []args){
        String str="1;2;3\n4;5;6\n7;8;9\n10;11;12";
        System.out.println(str);
        String[][] out = csv2array(str);
        System.out.print(array2csv(out,"-"));
        
     }
     
    public static int count(String s, char c) {
        return s.length()==0 ? 0 : (s.charAt(0)==c ? 1 : 0) + count(s.substring(1),c);
    }
    
    public static String[][] csv2array(String str){
        String[] parts   = str.split("\n");
        String[][] out = new String[count(str,'\n')+1][count(parts[0],';')+1];
        for(int i = 0;i< parts.length;++i){
            if(!parts[i].equals("")){
                out[i] = parts[i].split(";");
            }
        }
        return(out);
     }
     
     public static String array2csv(String[][] in,String delim){
         String out = "";
        for(int x = 0;x < in.length;x++){
        for(int y = 0;y < in[x].length;y++){
            out += (in[x][y]);
            if(y!=in[x].length-1){
                out += delim;
            }
        }
        out += ("\n");
        }
        return(out);
     }
     
}

This is the Output, fist block separated with the “;” is the input and with “-” is the output:

1;2;3
4;5;6 
7;8;9
10;11;12
1-2-3
4-5-6
7-8-9
10-11-12

perl shuffle String and Array

I wrote a little script in Perl to shuffle Arrays or strings:

sub myshuffle {
    my @str = ($#_ == 0)?split(//o, $_[0]):@_;
    my $r1 = 0;
    my $r2 = 0;
    
    for(0..($#str*2)){
        $r1 = int(rand($#str+1));
        $r2 = int(rand($#str+1))while($r1 == $r2 && $#str>4);
        ($str[$r1],$str[$r2]) = ($str[$r2],$str[$r1]);
    }
    return join '', @str;
}

This are my examples:

print myshuffle("abcdefghijklmonopqrs").$/;
print myshuffle(1,2,3,4).$/;
print myshuffle(1,2,3).$/;
print myshuffle(1,2).$/;

and we got his as result:

obrdekohnlicgsfqapjm  
2431 
231 
21

Perl reference vs. handing over Benchmark

I want to know if there is a different between a shift and normal handing over to a sub in Perl.This is only for scalar, for big data(array,hash) there is of course a different.

#!/usr/bin/perl
use strict;
use Benchmark qw(:all) ;
my $a = 100;
my $x = 100;
print "".sub_ref(\$a,\$x)."\n";
print "".sub_normal($a,$x)."\n";


cmpthese(-1, {
'ref'     => sub {sub_ref(\$a,\$x)},
'normal'  => sub {sub_normal($a,$x)},
});

sub sub_ref {
    return(${$_[0]}*${$_[1]});
}

sub sub_normal {
    return($_[0]*$_[1]);
}

Our result:

10000     
10000                          
            Rate    ref normal 
ref    2014189/s     --   -10% 
normal 2234181/s    11%     --

If its not necessary we should avoid references for scalar types, and try to iterate and not shift: Perl shift iterate Benchmark.But for Skalar values it is the fastest way to use prototypes.

Perl array vs. hash handing over Benchmark

I want to compare the hash and array in Perl and both of them with a reference and a normal handing over.That is the code, I tried to hold it as simple and fast as possible:

#!/usr/bin/perl
use strict;
use Benchmark qw(:all) ;
my @arr = (1..4);
my %hash;
@hash{(1..4)}=(1..4);
 
 
print ref_hash(\%hash);
print normal_hash(%hash);
print proto_hash(%hash);
print ref_arr(\@arr);
print normal_arr(@arr);
print proto_arr(@arr);
 
 
cmpthese(-4, {
'ref_hash'     => sub {ref_hash(\%hash)},
'normal_hash'  => sub {normal_hash(%hash)},
'proto_hash'   => sub {proto_hash(%hash)},
'ref_arr'      => sub {ref_arr(\@arr)},
'normal_arr'   => sub {normal_arr(@arr)},
'proto_arr'    => sub {proto_arr(@arr)},
});
#hash reference
sub ref_hash {
    my $out;
    for (keys %{$_[0]}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#normal hash
sub normal_hash {
    my $out;
    for (keys {@_}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#prototype hash
sub proto_hash(\%) {
    my $out;
    for (keys {@_}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#refernce array
sub ref_arr {
    my $out;
    for(@{$_[0]}){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#normal array
sub normal_arr {
    my $out;
    for(@_){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}
#prototype array
sub proto_arr(\@) {
    my $out;
    for(@_){
        $out .= ($_*2).$/;
    }
    return($out.$/."---".$/);
}

Our result :

4    
6    
8    
2    
     
---  
6    
4    
2    
8    
     
---  
6    
4    
8    
2    
     
---  
2    
4    
6    
8    
     
---  
2    
4    
6    
8    
     
---  
2    
4    
6    
8    
     
---

Looks good, the keys from the hashes are not sorted but I don’t want to waste performance with a sort.

 

Now this is the Benchmark result from 1..4 :

                Rate proto_hash normal_hash ref_hash ref_arr proto_arr normal_arr 
proto_hash  147908/s         --        -12%     -31%    -56%      -59%       -60% 
normal_hash 167716/s        13%          --     -22%    -50%      -54%       -54% 
ref_hash    215426/s        46%         28%       --    -35%      -40%       -41% 
ref_arr     333199/s       125%         99%      55%      --       -8%        -9% 
proto_arr   361439/s       144%        116%      68%      8%        --        -2% 
normal_arr  367775/s       149%        119%      71%     10%        2%         --

We could see the normal array is faster than the shift and prototype so for small data a reference is not faster than a normal use, but by the hash it is much faster than a normal handing over.and we could see array is faster than a hash, but the hash takes more information (key and values).

 

And this is the Benchmark result from 1..1000 :

              Rate proto_hash normal_hash ref_hash proto_arr normal_arr  ref_arr 
proto_hash  1014/s         --         -0%     -37%      -61%       -61%     -63% 
normal_hash 1019/s         0%          --     -37%      -61%       -61%     -63% 
ref_hash    1611/s        59%         58%       --      -38%       -39%     -42% 
proto_arr   2606/s       157%        156%      62%        --        -1%      -6% 
normal_arr  2623/s       159%        157%      63%        1%         --      -5% 
ref_arr     2759/s       172%        171%      71%        6%         5%       --

Now the reference from the array and hash are both fastest, the hash is always slower than the array.Just take a look at scalars.

Perl remove double entries in array Benchmark

Today I Benchmark two solution to remove all double entries from an array in Perl:

use strict;
use Benchmark qw(:all) ;

cmpthese(-1, {
'uniq1'  => sub {uniq1(1,1,2,2,3,4,5,6,6);},
'uniq2'  => sub {uniq2(1,1,2,2,3,4,5,6,6);},
});

sub uniq1 {
my %hash;
@hash{@_}=1;
return (sort keys %hash);
}

sub uniq2 {
    my %seen;
    grep !$seen{$_}++, @_;
}

We see the second function is 18% faster than the other one:

uniq2 395476/s    --  -16% 
uniq1 468114/s   18%    --

 

Now we have a good fast solution to remove the double entries from our array:

use strict;
#!/usr/bin/perl
my @arr = (1,1,2,2,3,4,5,6,6);

print "@arr\n";
@arr = uniq(@arr);
print "@arr\n";

sub uniq {
my %hash;
@hash{@_}=1;
return (sort keys %hash);
}

Our result:

1 1 2 2 3 4 5 6 6 
1 2 3 4 5 6

Perl shift iterate Benchmark

This little Perl Benchmark shows that its better to iterate through an array as to manipulate via shift.For more about shift look at Perl array pop, push, shift, unshift.

#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw(:all) ;

 
cmpthese(-1, {
'shift'   => sub {my @var = (1..4);print shift(@var); },
'iterate' => sub {my @var = (1..4);print $var[0]; },
});

We see its better to iterate as to shift from an array:

shift   1147836/s      --     -4%  
iterate 1191563/s      4%      --

Perl array order to highest numbers

This example Perl-Script find the highest number in the array with the highest number and set it to 1 and then count up to the lowest.

use strict;

print join($",makeRank(9,3,6,19)).$/;
print join($",makeRank(3,3,3,3,3,5,1)).$/;

sub makeRank {
my %hash;
my @rank = sort {$a<=>$b} grep {!$hash{$_}++} @_;
my @result = @_;
for my $i (0..$#rank){
    for my $x (0..$#result)
    {$result[$x] = $#rank-$i if($rank[$i]==$result[$x]);}
}
return map{++$_} @result;
}

Result looks like this:

2 4 3 1
2 2 2 2 2 1 3

Perl search in array

This Perl example is a function where I search for an element in an array, it returns the position or -1 if nothing matches.

#!/usr/bin/perl
use strict;
#tes tdata
my @arr = (2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14);
#read
print "Value:";
my $input = <>;
#test
print "position:".position(@arr,$input).$/;
#returns position or -1
sub position {
    for(0..($#_-1)){
        return $_ if($_[$_] == $_[$#_]);
    }
    return(-1);
}

As result we get for 9 -> 3 and for 4 -> -1 seems good.

sh-4.2# perl main.pl
Value:9 
position:3 
sh-4.2# perl main.pl 
Value:4     
position:-1  
sh-4.2#

Perl finding all number bigger than 10

In this example we want to filter all elements bigger than 10, for that we use grep like in this function:

#!/usr/bin/perl
use strict;
#example data
my @array = (2, 17, 10, 9, 16, 3, 9, 16, 5, 1, 17, 14);
#filter elements
@array = bigger_than_10(@array);
#print 
print "@array";
#function
sub bigger_than_10 {
return grep $_ >= 10, @_;
}

We got this as result:

17 10 16 16 17 14

Perl graph

This Perl example function shows how to generate a graph from a array.

#!/usr/bin/perl
use strict;
 
#example data
my @arr = (1,7,2,12,5,10,11,6,9);
#test sub
print graph(1, 1, 2, 3, 5, 8, 13,).$/;
print graph(@arr).$/;
#graph sub
sub graph {
    my @arr = @_;
    my $height = $arr[0];
    for(@arr){
        if($_ > $height){
            $height = $_;
        }
    }
    my $out = "   ^".$/;;
    for(my $y = $height;$y > 0;--$y){
        $out .= len_format($y."|",4);
        for my $x (0..$#arr){
            if(($arr[$x])-$y >= 0){
                $out .= len_format("*",3);
            }else{
                $out .= len_format(" ",3);
            }
        }
        $out .= $/;
    }
    for(-2..$#arr){$out .= "---";}
    $out .= ">".$/."    ";
    for(@arr){$out .= len_format($_,3);}
    return($out.$/);
}
#format strings
sub len_format{
    my $str = shift;
    my $len = shift;
    return((" "x($len-length($str))).$str);
}

The sub returns this result:

   ^                               
 13|                    *          
 12|                    *          
 11|                    *          
 10|                    *          
  9|                    *          
  8|                 *  *          
  7|                 *  *          
  6|                 *  *          
  5|              *  *  *          
  4|              *  *  *          
  3|           *  *  *  *          
  2|        *  *  *  *  *          
  1|  *  *  *  *  *  *  *          
--------------------------->       
      1  1  2  3  5  8 13          
                                   
   ^                               
 12|           *                   
 11|           *        *          
 10|           *     *  *          
  9|           *     *  *     *    
  8|           *     *  *     *    
  7|     *     *     *  *     *    
  6|     *     *     *  *  *  *    
  5|     *     *  *  *  *  *  *    
  4|     *     *  *  *  *  *  *    
  3|     *     *  *  *  *  *  *    
  2|     *  *  *  *  *  *  *  *    
  1|  *  *  *  *  *  *  *  *  *    
---------------------------------> 
      1  7  2 12  5 10 11  6  9