Personal tools
You are here: Home / cmgui / Wiki / Bits of Perl
Log in

Forgot your password?

Bits of Perl

How to take an ordered collection of jpg images (can be extended to other types) primed to make a movie and then create a new ordered collection which copies each imagea $nvers times so that the movie will run slower (if anyone has a better way please add to wiki)

$basename = $ARGV[0];
$start    = $ARGV[1];
$end      = $ARGV[2];
$nvers    = $ARGV[3];
for($i=$start; $i < ($end+1); $i++) {

  $origfile = "$basename"."$i".".jpg";

  for($j=0; $j < $nvers; $j++) {

      $copynum= $num*$nvers + $j;
      $copyname = sprintf("field%05d.jpg",$copynum);
      print "Copy ",$origfile," to ",$copyname," \n";
      system("cp $origfile $copyname");



How to initialise a doubly indexed array

@bob = ([ 6, 3, 5 ],[ 7, 8, 9]);
print "$bob[0][0]\n"

How to iterate through each array and access the elements

foreach my $x (@bob) {
  #$x is an array ref
  print $x->[0]."\n"; # should print 6 then 7

How to push an array into bob

@jim = [ 0, 1, 4];
push @bob, [@jim];

Note that this is probably not what you wanted

push @bob, @jim; # puts each element of jim into bob

Hashes of hashes

Can define hashes of hashes many ways eg for explicit naming
my %bob = (
           'fred1'  => {},
           'fred2'  => {},

$bob{'fred1'}->{'0.2'};  returns the result of the fred/0.2
key/value pair

to get the number of keys in fred2
$number_of_fred2_keys = scalar(keys( %{$bob{'fred2'}} ));

Need to add E when the fortran output left them off, use like perl bob.exdata > bob2.exdata

while (<>)
  $_ =~ s/(\d{5})-(\d{3})/$1E-$2/;

If modifying a file from the shell, it's better to use several of the flags on the "perl" command to do the hard work for you.

  • -e : execute the following as a perl command
  • -i : edit a file "inplace" - i.e. read and write the same file. Can create a backup first by appending a suffix to -i (e.g. -i.bak)
  • -n : iterate over each line (same as : while (<>) { ... })
  • -p : as for -n, but print $_ after each iteration

So to add the "E" as per above, a shell one-liner would be:

$ perl -i -pe 's/(\d{5})-(\d{3})/$1E-$2/' bob.exdata

or with a backup to bob.exdata.bak:

$ perl -i.bak -pe 's/(\d{5})-(\d{3})/$1E-$2/' bob.exdata

To remove carriage returns from a file generated in windows:

perl -pi -e 's/\r//g;' test.exnode

How to do a global search and replace on multiple files. The following line will find all files with the extension .html and replace all occurrences of the pattern h2 with the pattern h3 (ie this will mean all h2 headers are converted to h3 headers).

find . -name "*.html" -exec perl -pi -e 's/h2/h3/g' {} \;

If you want to replace $ variables in a file (say an ipfile) then you can use the evaluate s/// operator to do most of the work, NOTE the use of CORE:: to call perl functions which are also cmiss functions:

#---------Read in file and evalute variable that are contained in that file ---------
my $BCL = 200;
CORE::open(INFO, 'template_FK_BR.ipcell');
CORE::open(INFO2, ">FK_BR_BCL.ipcell");
while (defined(my $Values = <INFO>) ){
   $Values =~ s/(\$\w*)/eval($1)/eg;
  print INFO2 $Values;
close (INFO);
close (INFO2);