Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Sascha Tayefeh http://www.tayefeh.de

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Count the appearence of certain chars

Count the appearence of certain chars using regular expression query and
using perl's string-operator

#!/usr/bin/perl
# Count the appearence of certain chars using regular expression query
#


$string="MNVDPFSPHSSDSFAQAASPARKPPRGGRRIWSGTREVIAYGMPASVWRDLYYWALKVSWPVFFASLAALFVVNNTLFALLYQLGDAPIANQSPPGFVGAFFFSVETLATVGYGDMHPQTVYAHAIATLEIFVGMSGIALSTGLVFARFARPRAKIMFARHAIVRPFNGRMTLMVRAANARQNVIAEARAKMRLMRREHSSEGYSLMKIHDLKLVRNEHPIFLLGWNMMHVIDESSPLFGETPESLAEGRAMLLVMIEGSDETTAQVMQARHAWEHDDIRWHHRYVDLMSDVDGMTHIDYTRFNDTEPVEPPGAAPDAQAFAAKPGEGDARPV";

$neg = ($string =~ tr/[DE]//);
$pos = ($string =~ tr/[RK]//);
$his = ($string =~ tr/[H]//);
$total = $pos+$his-$neg;
$totalNoHis = $pos-$neg;

print "NEG = $neg ; POS = $pos ; HIS = $his; TOT = $total (without HIS = $totalNoHis)\n";

Comfortable Exif Writer using ExifTools

I felt strange lazy using all that command-line stuff. I wanted to have kind of a platform-independent command-line front-end for exiftools. Here's what I got:


#!/usr/bin/perl
# 
# writeExif.pl V0.3
# 2007 by S Tayefeh
# http://www.tayefeh.de
#
# This perl-script uses exiftools (http://www.sno.phy.queensu.ca/~phil/exiftool/)
# to write most common tags to a file. usage:
# 
# writeExif.pl [FILENAME]
# 
# (WINDOWS using ActivePerl:
# perl C:/WINNT/writeExif.pl [FILENAME]
# )
#
#
# Just edit the variables here:
#

########## Proceed (=1) or Skip (=0) groups ? #####
$do{"date"}		=1; 	# anything dealing with date/time
$do{"technical"}	=1;	# technical details
$do{"description"}	=1;	# anything dealing with descriptions/keywords/title etc
$do{"place"}		=1;	# City/State/Country
$do{"geo"}		=1;	# GeoTags (Latitude/Longitude/Altitude)
$do{"rights"}		=1;	# Owner/Copyright/Software

########## Place ##########
if($do{'place'})
{
   $para{"City"}		="Heidelberg";
   $para{"State"}		="Baden-Wuertemberg";
   $para{"Country"}		="Germany";
}

########## Get this from e-g. http://www.addressfix.com/ ##########
if($do{'geo'})
{
   $para{"GPSLatitudeRef"}	="N"; 		# N=North S=South
   $para{"GPSLatitude"}		="49.41222269";	# "dd/1,mm/1,ss/1.", or float 49.8724240
   $para{"GPSLongitudeRef"}	="E"; 		# E=East W=West
   $para{"GPSLongitude"}	="8.71022701";
   $para{"GPSAltitude"}		="20"; 		# [meters]
}

########## Technical Details ##########
#
if($do{'technical'})
{
   $para{"Make"}		="MINOLTA";	# Camera Vendor
   $para{"Model"}		="X-300";	# Camera Model
   $para{"ISO"}			="100";
   $para{"Lens"}		="24mm f/2.4";	# Lens Type
   $para{"FocalLength"}		="24";		# Focal Length, e.g: "29.0mm (35mm equivalent: 43.0mm)"
   $para{"FNumber"}		="2";		# Actually used focal tatio, e.g. for f/2.7 Fnumber=2.7 
   $para{"ExposureTime"}	="1/60";	# Exposure time [s]
}

########## Time/Date ##########
$myTime{"Hour"}			='23';
$myTime{"Minute"}		='11';
$myTime{"Second"}		='00';
$myTime{"Day"}			='17';
$myTime{"Month"}		='10';
$myTime{"Year"}			='2005';


########## Description ##########
if($do{'description'})
{
   $para{"Title"}		="Thalia Theater";
   $para{"ImageDescription"}	="Thalia Theater in Hamburg";
   $para{"Keywords"}		="Something, Some, Test, My";
}

########## Rights ##########
if($do{'rights'})
{
   $para{"Owner"}		="Sascha Tayefeh";
   $para{"Software"}		="writeExif.sh by Sascha Tayefeh (http://www.tayefeh.de)";
}


########## Generic Variables ##########
# You do not need to edit these. However, 
# if you want some more specifics, edit these, too:

if($do{'date'})
{
   $para{"DateTimeOriginal"}	="$myTime{'Year'}:$myTime{'Month'}:$myTime{'Day'} $myTime{'Hour'}:$myTime{'Minute'}:$myTime{'Second'}";
   $para{"ModifyDate"}		=$para{'DateTimeOriginal'};
   $para{"CreateDate"}		=$para{'DateTimeOriginal'};
}


if($do{'rights'})
{
   ($sec,$min,$hour,$mday,$mon,$year,$wday, $yday,$isdst)=localtime(time); $year=$year+1900;
   $para{"Copyright"}		=$year." by $para{'Owner'}"; #Copyright
}

if($do{'description'})
{

   $para{"Comment"}		=$para{'ImageDescription'};
   $para{"ObjectName"}		=$para{'Title'};
   $para{"Description"}		=$para{'ImageDescription'};
   $para{"UserComment"}		=$para{'Comment'};
   $para{"Caption-Abstract"}	=$para{'Comment'};
   $para{"Subject"}		=$para{'Tags'};
}

########## -------------- STOP EDITING ---------------  #########


if (!$ARGV[0]) {die "***ERROR: Missing Filename\nUSAGE: writeExif.pl [FILENAME]\n";}

$exifOptions="";
while (($tag, $value) = each(%para))
{
   $exifOptions=sprintf ("%s \"-%s=%s\"",$exifOptions,$tag, $value);
}

#print "$exifOptions\n";

if($exifOptions)
{
$bool=system ("exiftool $exifOptions $ARGV[0]") ;
$bool=system ("exiftool $ARGV[0]") ;
} else
{
   print "All groups switched off... nothing to do\n";
}

Dynamically Parse Multirow/Multicolumn Textfile

This example demonstrates howto parse a multicolumn and multirow textfile delimited by TABs. It dynamically determines the number of rows and columns.

#!/usr/bin/perl -w
# Parse Multicolumn Textfile
# (c) 2005 by Sascha Tayefeh
# http://www.tayefeh.de/
#
# This example demonstrates howto parse a 
# multicolumn and multirow textfile delimited
# by TABs. It dynamically determines the
# number of rows and columns.
#

use strict;

my ($infile,$i,$j,$hello);
my (@line,@sum);

$infile="multi-row-column.dat.txt";

open(IN, "< $infile") or die "Could not open $infile\n";

while (<IN>)
{
    chomp;
    if($_ =~ /^[0-9]/) # only add if line starts with a digit
    { 
	push @line, [split '\t', $_]; # make an array and push it into @line
    }
}

print "Table read:  $#line rows and  $#{$line[0]} columns.\n";
print "$line[1][2] \n";

for $i (0 .. $#line)
{
    for $j (0 .. $#{$line[$i]})
    {
	print $line[$i][$j]," ";
	$sum[$j]+=$line[$i][$j];
    }
    print "\n";
}

# Do something (not so) interesting
for (@sum)
{
    print "$_ ";
}
print "\n";

Using Perl's Math-class

A little demo that shows how to perform some vector-algebra using Math::-methods.

#!/usr/bin/perl

#use strict;
use Math::VectorReal qw( :all );
use Math::Trig qw( acos );

$a=vector(10,0,0.1);
$b=vector(0,0,1);

$lnorm=($a->length)*($b->length);
$scalprod= $a . $b;
$arccosthis=$scalprod/$lnorm;
$rangle=acos($arccosthis);
$dangle=($rangle/3.1415926535897931)*180;

print $a."".$b."\n";
print "Angle = ".$dangle."\n";

Simple command-line Mailinglist

This code reads an email-list stored in the file"emailist.txt".
This file contains all adresses delimited by "\n", i.e.
one column of emails. However, you have to have sendmail installed.

#!/usr/bin/perl
#
#	Mailing-List.pl
#	2005 by Sascha Tayefeh
#	http://www.tayefeh.de
#
#	This code reads an email-list stored in a file:	emailist.txt
#	This file contains all adresses delimited by \n, i.e.
#	one column of emails.
#

use strict;

my ($absender, $empfaenger, $betreff, $body, $content,$file,$line,$i,$k);
my (@line,@liste);

$file="./emailist.txt"; # a one-column textfile providing the emails (one per line)
#$empfaenger = "singleReceiver\@singleReceiver.single"; # E-mail of a single-receiver
$absender = "sender\@sendersEmail.sender>"; # E-mail of the sender
$betreff = "Header here"; # header of the email
$body = "\nThis is the body of the messag \n\n Bye\n"; # The message body


$line[0]= sprintf("From: %s \n",$absender);
$line[1]= sprintf("To: %s \n",$empfaenger);
$line[2]= sprintf("Subject: %s \n",$betreff);
$line[3]= sprintf("%s \n",$body);

$i=0;
open(LIST, $file);
while (defined ($line=<LIST>)) {
    chomp $line;
    $liste[$i] = $line;
    $i++;
}
close(LIST);

print $body;

printf("%d emails to sent\n",$i);

for ($k=0; $k<$i; $k++) {
    open(SENDMAIL, "|/usr/lib/sendmail -oi -t -odq") or die "fork fuer sendmail fehlgeschlagen: $!\n";
    $line[1]= sprintf("TO: %s \n",$liste[$k]);
    printf("Sending mail %d to %s\n",$k+1,$liste[$k]);
    foreach $content (@line) {
	print SENDMAIL $content;
    }
    close(SENDMAIL) or warn "sendmail wurde nicht recht beendet...";
    sleep 1;
}

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS