You are here: Home » Projects » Code Snippets

Code Snippets

This section will show some code snippets that I use from time to time. Feel free to use it as well.

1. Bash
   1.001. Setting the current DATE and TIME
   1.002. Run PEPS2003 (passing args) in a whole directory of .san files, save progress to an output file
   1.003. Run a command, inside a loop (use of for and while) iterates in directory
   1.004. Using if - testing for initial parameters
   1.005. Multiple if

2. Perl
   2.001. Standard Perl header
   2.002. Open a file for reading, iterate through lines, remove \n
   2.003. Trim, changes multiple spaces for single space
   2.004. Open all *.txt files of a directory, runs a system command to print the last line of the file
   2.005. Print blocks of text
   2.006. Formatting numbers
   2.007. Printing blocks of text to an output file
   2.008. Using hashes with arrays and iterating
   2.009. Creating a dotty file from a matrix
   2.010. Passing two arrays to a subrotine and printing
   2.011. Date and time and printf
   2.012. Match everything betwen { and }
   2.013. Open file, match pattern between ( and ), reverse and print array without ending spaces
   2.014. Filling a string with zeroes (before the number)
   2.015. Take a file with many runs and execute the confidence intervals
   2.016. Call subrotine with array and variables
   2.017. Using hashes of arrays
   2.018. Convert an array to a matrix
   2.018. Convert photos from high definition to 4x6 inches (or 10x15 cms) format (1024x768)

3. C/C++
   3.001. Solve an infinitesimal generator
   3.002. Removing equals from a list
   3.003. Removing chars from string
   3.004. Converting string to upper or lower case

4. Maple
   4.001. Solve a pepa model in Maple

5. Latex
   5.001. Figures in Latex
   5.002. Putting vertical labels in tables
   5.003. Script for cleaning latex files and compiling latex
   5.004. Full latex project for Lecture Notes in Computer Science format

6. Beamer
   6.001. A presentation in Beamer, with multiple slides
   6.002. Beamer complete list of themes

7. Miscelaneous
   7.001. A Makefile without having to tell all .c files
   7.002. Convert a .wav to .mp3 using lame
   7.003. Create a small book from a ps file
   7.004. Merging PDFs

8. gnuplot
   7.001. opções gerais do gnuplot




1. Bash programming
1.001. Setting the current DATE and TIME
#!/bin/bash
DATE=`date +%d%m%Y.%H%S`
cd `somedir`

1.002. Run PEPS2003 (passing args) in a whole directory of .san files, save progress to an output file

echo "" > file.log
dirsan=/tmp
for i in $dirsan/*.san; do           #for all existing files in 'san' directory
   ni=${i//.san/}    #removes file termination .san (san wont accept it)
   rm $dirsan/agg/ $dirsan/cnd/ $dirsan/des/ $dirsan/dsc/ $dirsan/jit/ -rf
   rm $dirsan/*.vct -rf #remove previous peps stuff
   hora=`date +%H:%M:%S%t%d/%m` #saves current time, to mark progress
   echo "running $ni at $hora" >> file.log  #save to file.log what's running
   echo 4 2 100000 1 1 $ni 2 1 1 vector 0 2 | $dirsan/peps
      #the last line does 3 things: 
        #1. calls peps setting iterations to 100000 
        #2. compiling (1 1) and 
        #3. solving (2 1 1) then exiting (0 2)
      #we are assuming that the executable is in the dirsan directory
done

1.003. Run a command, inside a loop (use of for and while) iterates in directory
t=0
c=0
o=0
cut=10
term=62
dir=$base'models/8slaves/'
while [ $t -le $term ]; do
   path=$dir'term'$t
   for i in $path/*; do
      file=$path/'8slaves_'$t'_t'$o'.out'
      `rm $file -f`
      let o+=1
      while [ $c -le $cut ]; do
         `$base''split $i $c >> $file`
         let c+=1
      done
      c=0
   done
   o=0
   let t+=1
done

1.004. Using if - testing for initial parameters
#!/bin/bash

if [ "$1" = "" ]; then
   echo "error. missing parameter n"
else
   echo "success"
fi

1.005. Multiple if
#!/bin/sh

if [ "$1" = "" ]; then
  echo "invalid command. options: get/getall/put"
else
if [ "$1" = "getall" ]; then
  echo "retrieving ALL from foo"
else
if [ "$1" = "get" ]; then
  echo "retrieving *.htm from foo"
else
if [ "$1" = "put" ]; then
  if [ "$2" = "" ]; then
    echo "choose a file."
  else
    echo "uploading $2 to foo"
  fi
fi
fi
fi
fi


2. Perl programming
2.001. Standard Perl header
#!/usr/bin/perl

use strict;
use warnings;

if (@ARGV != 1) { 
   print "missing FILE parameter. \nusage: perl file.pl FILE\n";
   exit;
}


2.002. Open a file for reading, iterate through lines, remove \n
open(INFILE, "<$file") or die("cannot open hash file named $file\n");
my(@lines) = <INFILE>;
close(INFILE);
foreach my $line (@lines) {
   $line =~ s/\n//g;
}

2.003. Trim, changes multiple spaces for single space
$line =~ s/^\s+//;  #trim at start
$line =~ s/\s+$//;  #trim at end
$line =~ s/\s+/ /g; #remove multiple spaces for single space

2.004. Open all *.txt files of a directory, runs a system command to print the last line of the file
#!/usr/bin/perl

my $path = $ARGV[0]."output/";
my @contents;
opendir MYDIR, $path;
@contents = grep !/^\.\.?$/,grep /.txt/, readdir MYDIR;
closedir MYDIR;
foreach my $file (@contents) {
   print "file=$file\n";
   system("tail -n 1 $path$file");
}

2.005. Print blocks of text

print >>END;
\\begin{table*}[!hbt]
\\begin{center}
END



2.006. Formatting numbers (requires module to be installed)
#put this on the header
use Number::Format qw(:subs :vars);
$THOUSANDS_SEP   = '.';
$DECIMAL_POINT   = ',';
$INT_CURR_SYMBOL = 'DEM';


#use it like this:
$mem = format_number($mem, 2, 1);
$complex = format_number($complex);

2.007. Printing blocks of text to an output file
open(OUTFILE, ">$name") or die("cannot open $name\n");
print OUTFILE >>END;
#!/bin/sh

set term postscript eps enhanced color
set xtics 2
plot[0:$termo][0:$lcut]   'bc_$model.txt' title "term x cut" with boxes 
END
close(OUTFILE);

#or

open(OUTFILE, ">$name") or die("cannot open $name\n");
print OUTFILE "this is some text to be printed\n";
close(OUTFILE);


2.008 Using hashes with arrays and iterating
$strut->{$lid} = {
   'gid'   =>   $gid,
   'aa'    =>   [@aa],
   'ja'    =>   [@ja],
   'ia'    =>   [@ia],
   'order' =>   $order,
   'nz'    =>   $nz,
};

#defining an array of hashes:
push @terms, $strut;

#iterating
foreach my $st (@terms) {
   foreach my $termo (keys %$strut) {
      print "gid=".($strut->{$termo}{'gid'})."\n";
      print "lid=$termo\n";
      print "order=".$strut->{$termo}{'order'}."\n";
      print "nz=".$strut->{$termo}{'nz'}."\n";
      print "id=".$strut->{$termo}{'id'}."\n";
   }
}

#finally, to print the whole structure, with arrays:
sub print_strut {
   my @lterms = @_;
   print "call to print...\n";
   foreach my $st (@lterms) {
      foreach my $termo (keys %$st) {
         print "term $termo [".($st->{$termo}{'gid'})."] nz=".$st->{$termo}{'nz'}."\n";
         my @aux=@{$st->{$termo}{'aa'}};
         print "aa: ";
         foreach my $a_ (@aux) {
            print "".$a_." ";
         }
         print "\n";
         my @aux=@{$st->{$termo}{'ja'}};
         print "ja: ";
         foreach my $a_ (@aux) {
            print "".$a_." ";
         }
         print "\n";
         my @aux=@{$st->{$termo}{'ia'}};
         print "ia: ";
         foreach my $a_ (@aux) {
            print "".$a_." ";
         }
         print "\n";
      }
      print "\n";
   }
}

2.009. Creating a dotty file from a matrix
print "digraph G {\n";
print "\tnode [shape = circle];\n";
for (my $i = 0; $i < $SIZE; $i++) {
   for (my $j = 0; $j < $SIZE; $j++) {
      if ($matrix[$i][$j] ne "") {
         my $label = "label$i$j";
         my $auxi = ($i+1) < 10 ? "0".($i+1) : ($i+1);
         my $auxj = ($j+1) < 10 ? "0".($j+1) : ($j+1);
         print "\tS".($auxi)." -> S".($auxj)." [ label = \"$label\" ]\n";
      }
   }
}
print "}\n";

2.010. Passing two arrays to a subrotine and printing

#call: verify_changes(@arr1, @arr2);

#subrotine:
sub verify_changes {
   my @v1 = @{$_[0]};
   my @v2 = @{$_[1]};
   print "printing...\n";
   for (my $i = 0; $i < $size; $i++) {
      print "$v1[$i] ";
   }
   print "\n";
   for (my $i = 0; $i < $size; $i++) {
      print "$v2[$i] ";
   }
   print "\n";
}

2.011. Date and time and printf
(my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time);
printf "%4d-%02d-%02d %02d:%02d:%02d\n",$year+1900,$mon+1,$mday,$hour,$min,$sec;
#or
my $date = sprintf("%02d/%02d/%4d %02d:%02d:%02d\n",$mday,$mon+1,$year+1900,$hour,$min,$sec);
print $date."\n";

2.012. Match everything betwen { and }
if ($line =~ /(\s*)authors=\{(.*)\}/) {
   print "authors=$2\n";
}

2.013. Open file, match pattern between ( and ), reverse and print array without ending spaces
#!/usr/bin/perl

use strict;
use warnings;

#this script will revert the .mdd final file in the same format peps works

if (@ARGV != 1) { 
   print "missing FILE parameter. \nusage: perl revert.pl FILE\nenter a .rep file with termination\n";
   exit;
}

my $rep_file = $ARGV[0];
open(INFILE, "<$rep_file") or die("cannot open $rep_file");
my(@lines) = <INFILE>;
close(INFILE);

my $name;
my $states;
my @entries;
my $cont = 0;
foreach my $line (@lines) {
   $line =~ s/\n//g;
   if ($line =~ /\(\s(.*)\s\)/) {  #for example ( 2 2 3 4 2 1 )
      $states = $1;
      (@entries) = split(" ", $states);
      @entries = reverse(@entries); #example of reverse
      foreach my $entry (@entries) {
         print "".((++$cont % @entries == 0) ? "$entry" : "$entry ");
      }
      #$cont = 0;
      print "\n";
   }
}

2.014. Filling a string with zeroes (before the number)
my $i = 10;
my $val = sprintf("%05d",$i);

2.015. Take a file with many runs and execute the confidence intervals

Download file here -> Coding language: Perl.

Download samples file here Text file containing some values.

2.016. Call subrotine with array and variables
deal(\@sizes,\@cuts,\@nzs,\@perms,$n_aut,$n_evt,$model);

...

sub deal {
   my ($sz,$ct,$nz,$pe,$auts,$evts,$model_) = @_;
   my @sizes_ = @$sz;
   my @cuts_ = @$ct;
   my @nzs_ = @$nz;
   my @perms_ = @$pe;
}

2.017. Using hashes of arrays
my %rosa = (
  'N66' => [80,0,0,0,1,1,70,100],
  'N23' => [1,70,80,100,30,3,2,1],
  'EQU' => [1,15,50,80,100,75,45,10],
  'S23' => [1,1,2,3,30,100,80,70],
  'S66' => [80,100,70,1,1,0,0,0]
);

...

foreach my $key (keys %rosa) {
   my @forces = @{$rosa{$key}};    #this is where you take the array reference
   print "$key\n";
   foreach my $f (@forces) {       #now, just use it
      print "$f ";
   }
   print "\n";
}

2.018. Convert an array to a matrix
#!/usr/bin/perl

#implementado em 18/01/2010

use strict;
use warnings;

#converte um vetor em uma matriz
#util para percorrer um vetor e imprimir a linha da matriz, acessando os indices corretos

#colocar valores multiplos para fechar matrizes quadradas (nao testado)

if (@ARGV != 2) { 
   print "falta TAMANHO ou DIMENSAO. \nuso: perl vec2mat.pl TAMANHO DIMENSAO\n\tTAMANHO: tamanho total do vetor\n\tDIMENSAO: dimensao final da matriz\n";
   exit;
}

my $SIZE = $ARGV[0];  #tamanho do vetor
my $DIM = $ARGV[1];   #cria uma matriz DIMxDIM a partir do vetor de SIZE posicoes

#inicia um vetor qualquer com valores quaisquer
my @ARR = ();
for (my $i = 0; $i < $SIZE; $i++) {
   push @ARR, $i;
}

#calcula o tamanho da linha
my $linha = @ARR / $DIM;

my $c = 0;
my $val = 0;
my $aux = 0;
for(my $i = 0; $i < @ARR; $i++) {
   my $o = ($c++ % $DIM);
   $val = $aux + ($linha * $o);
   print "$ARR[$val] ";
   if (($i + 1) % $DIM == 0) {
      print "\n";
      $aux++;
   }
}


2.019. Convert photos from high definition to 4x6 inches (or 10x15 cms) - 1024x768
#!/usr/bin/perl

use strict;
use warnings;

if (@ARGV != 1) { 
   print "missing PATH parameter. \nusage: perl convert.pl PATH\n";
   exit;
}

my $path = $ARGV[0];

my @contents;
opendir MYDIR, $path;
@contents = grep !/^\.\.?$/,grep /.JPG/i, readdir MYDIR;
closedir MYDIR;
system("rm new -rf");
system("mkdir new");
foreach my $file (sort @contents) {
   my $fp = "$path/$file";
   my $aux = $file;
   $aux =~ s/\./;/;
   my $filename;
   my $term;
   ($filename,$term) = split(";",$aux);
   my $new_name = $filename."\_.".$term;
   print "[$aux] converting $file... to $new_name\n";
   system("convert -resize 1024x768 $file $new_name");
   system("mv $new_name ./new");
}


3. C/C++ programming
3.001. Solve an infinitesimal generator

Download file here -> Coding language: C++.
Obs.: the file must have the following format:
q1
n9
-2 2 0 0 0 0 0 0 0
0 -6 1 5 0 0 0 0 0
0 0 -10 0 10 0 0 0 0
0 0 0 -6 6 0 0 0 0
5 0 0 0 -13 3 5 0 0
0 0 0 0 0 -5 0 5 0
0 0 0 0 0 0 -4 4 0
0 0 0 5 0 0 0 -7 2
0 0 0 0 5 0 0 0 -5

The call for the executable is: ./aps ITERATIONS FILE
Example: ./aps 100 file.txt
The output will be the resulting vector.

3.002. Removing equals from a list

#include <vector>
#include <list>
#include <iostream>
#include <iomanip>
#include <fstream>

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>

using namespace std;

//for minimum and maximum float value
#include <float.h>

std::vector<int> removeequals(int *y, int size) {
   std::list<int> l;
   for (int i = 0; i < size; i++) {
      l.push_back(y[i]);
   }
   l.sort();
   l.unique();
   std::vector<int> v(l.begin(), l.end());
   return v;
}

int main(char argc, char** argv) {
    
   if (argc != 1) {
      printf("Usage: ./list\n\n");
      //printf("FILE file name to be open\nmodel - model to be used\nruns - number of executions\n");
      exit(0);
   }
   int *y = (int*)malloc(sizeof(int)*10);
   y[0] = 15;
   y[1] = 11;
   y[2] = 11;
   y[3] = 12;
   y[4] = 11;
   y[5] = 14;
   y[6] = 11;
   y[7] = 14;
   y[8] = 12;
   y[9] = 11;

   std::vector<int> newv = removeequals(y, 10);
   for (int i = 0; i < newv.size(); i++) {
      int value = (int) newv[i];
      cout << "value["<<i<<"]=" << value << endl;
   }
   return 0;
}


3.003. Removing chars from string
string StringUtils::removechar(const char r, const string& in) {
   string out = in;
   while (out.find(r) != string::npos) {
      string::size_type pos = out.find(r);
      out.erase(pos,1);
   }
   return out;
}


3.004. Converting string to upper or lower case
string lcase(const string& in) {
   string out = in;
   std::transform(out.begin(), out.end(), out.begin(), ::tolower);
   return out;
}

string ucase(const string& in) {
   string out = in;
   std::transform(out.begin(), out.end(), out.begin(), ::toupper);
   return out;
}





4. Maple
4.001. Solve a pepa model in Maple

n:=2;
l:=1;
r:=3;
p:=5;
t:=4;

with(linalg):
Q := array(sparse,1..96,1..96):
read `mobile2agents.maple`:
b := array(sparse,1..96):
for i to 96 do
    Q[i,96] := 1.0
od:
b[96] := 1:
QT := transpose(Q):
P := linsolve(QT,b);
n:=2;
l:=1;
r:=3;
p:=5;
t:=4;

with(linalg):
Q := array(sparse,1..96,1..96):
read `mobile2agents.maple`:
b := array(sparse,1..96):
for i to 96 do
    Q[i,96] := 1.0
od:
b[96] := 1:
QT := transpose(Q):
P := linsolve(QT,b);


5. Latex
5.001. Figures in Latex


5.002. Putting vertical labels in tables
\usepackage{rotating}

...

\begin{tabular}{|r|r|}\hline
\begin{sideways}Paper\end{sideways} &\begin{sideways}Static\end{sideways} \\
\hline
HAR1994j & Journal \\
SWRT1996c & Conference \\
\hline
\end{tabular}

5.003. Script for cleaning latex files and compiling latex

Cleaning a latex directory with a bash script -> Script language: bash.

Compiling latex from a bash script -> Script language: bash.

5.004. Full latex project for Lecture Notes in Computer Science format

Template for LNCS project -> Latex files.


6. Beamer presentations
6.001. A presentation in Beamer, with multiple slides

Download presentation+figures
Link to some useful tips

6.002. Beamer complete list of themes

To use it, change the following line:
\usetheme{THEME}
I particularly like the Frankfurt theme. It is the most classic. He is without sections, only balls to mark where you are in the presentation.

AnnArbor
Antibes
Bergen
Berkeley
Berlin
Boadilla
CambridgeUS
Copenhagen
Darmstadt
Dresden
Frankfurt
Goettingen
Hannover
Ilmenau
JuanLesPins
Luebeck
Madrid
Malmoe
Marburg
Montpellier
PaloAlto
Pittsburgh
Rochester
Singapore
Stuttgart
Szeged
Warsaw


7. Miscelaneous
7.001. 7. A Makefile without having to tell all .c files
#
# Makefile of XXXX XXXX
# Changed by xxxx@xxx.xxxxx.xx
#

CPP=g++
INCLUDE_DIR = ./include
BIN_DIR = .
OBJ_DIR = .
LIB_DIR = ./lib

# The options of compilation (debug or optimization)
CPPFLAGS= -I/usr/local/include -Wno-non-virtual-dtor -Wno-deprecated -O3 -I$(INCLUDE_DIR)

# The options of linkage
LDFLAGS= -L$(LIB_DIR) -lyourlib

SOURCES := $(patsubst %.cpp,%.o,$(wildcard *.cpp))
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=hello

compile: $(SOURCES) $(EXECUTABLE)
	
$(EXECUTABLE): $(OBJECTS)
	$(CPP) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
	$(CPP) $(CPPFLAGS) -c $< -o $@

all: compile

clean:
	rm -f *.o hello


7.002. Convert a .wav to .mp3 using lame
for i in *.wav; do lame --preset standard $i `basename $i .wav`.mp3; done

7.003. Create a small book from a ps file
psbook IN OUT
psnup -pa4 -2 IN OUT

7.004. Merging PDFs
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=output.pdf file1.pdf file2.pdf 

8. gnuplot
8.001. 8. Opções gerais do gnuplot

Posicionamento de legendas na figura:
set key {  left | right | top | bottom | outside | below }

Segundo eixo y:
plot "ad.txt" using 1:2 axes x1y2 ls 1 with boxes title "Experimento 2 - 9"

Operações com colunas:
plot \
 "ad.txt" using 1:($3-$2) with boxes title "Experimento [3 - 2]", \
 "" using 1:2 with lines title "Experimento 2"

arquivo "ad.txt" (observe que ele contém 3 colunas, ou seja, $1, $2 e $3):
1 10 2
2 15 5
3 21 10
4 15 8
5 11 10
6 9 3
...


Exemplo com estilos de linha:
set term postscript eps color
set output "ad.eps"

set logscale xy
set xrange[9.5:16.5]
set key right bottom
set grid

set style line 1 lt 1 lc rgb "red" lw 3
set style line 2 lt 2 lc rgb "blue" lw 1
set style line 3 lt 3 lc rgb "yellow" lw 3
set style line 4 lt 4 lc rgb "#aabbcc" lw 1
set style line 5 lt 5 lc rgb "grey" lw 1

plot \
 "ad.txt" using 1:2 ls 1 with lines title "Experimento 1", \
 "" using 1:3 ls 2 with lines title "Experimento 2", \
 "" using 1:4 ls 3 with lines title "Experimento 3", \
 "" using 1:5 ls 4 with lines title "Experimento 4", \
 "" using 1:6 ls 5 with lines title "Experimento 5"


Exemplo de uso de multiplot:
set term postscript eps color
set output "results.eps"

set multiplot layout 1, 2 title "Resultados do Modelo Mestre-Escravo"

######################################################################
set title "Tempo (s) - Escala loglog"

set logscale xy
set xrange[4.5:10.5]
set key left top

plot \
 "slaves.txt" using 1:2 with lines title "Exp. 1", \
 "" using 1:3 with lines title "Experimento 2", \
 "" using 1:4 with lines title "Experimento 3", \
 "" using 1:5 with lines title "Experimento 4", \
 "" using 1:6 with lines title "Experimento 5"

######################################################################
set title "Memoria (Kb) - Escala loglog"

set logscale xy
set xrange[4.5:10.5]
set key left top

plot \
 "slaves.txt" using 1:11 ls 1 with lines title "Exp. 1", \
 "" using 1:12 ls 3 with lines title "Exp. 3", \
 "" using 1:13 ls 5 with lines title "Exp. 5", \
 "" using 1:14 ls 6 with lines title "Exp. 6", \
 "" using 1:15 ls 7 with lines title "Exp. 7", \
 "" using 1:16 ls 8 with lines title "Exp. 8"


Animação (.gif) e desenhar uma matriz:
set terminal gif animate delay 1
set output "floripa_certo.gif"

#set view 60,60
set view 60,45
#set view 0,180
#set nosurface
#set contour base
#set dgrid3d
set surface
set pm3d
set cntrparam levels auto 30
splot "floripa_certo_1.dat" matrix with lines
splot "floripa_certo_501.dat" matrix with lines 
splot "floripa_certo_1001.dat" matrix with lines