Anti-nuisance lawsuit warning: The purpose of these notes is to remind me, Zoegond, of stuff or to help me work stuff out. They may contain mistakes.

Quick

  • ($a, $b....) = unpack("A2A7...", $packed)
  • push( array, list )

Friday, February 26, 2010

Perl regular expressions and interpolation

Q. How to use, in a s/// substitution, a string defined elsewhere in your program, or supplied by the user, and that contains variable substitions/grouping references?

A. Use double evaluation, and ensure the string has quotes actually in it, and (in the case of a string defined in the program) that any references to groups are escaped so that they aren't interpreted at compile time. Escaping group references should not be done in a user-supplied string, or a string read from a file etc.

(On the pattern side of the s///, you only need watch out for $, @ etc which might cause unwanted interpolation in your pattern).

$replace = '"\$1z"'; # check the quotes carefully! it's ' " blah " '
$text = "apple";
$pattern = "a(..)";
$text =~ s/$pattern/$replace/ee;
# or $text =~ s/$pattern/eval $replace/e;

# or
$replace = "\$1z";
$text = "apple";
$pattern = "a(..)";
$text =~ s/$pattern/qq("$replace")/ee;
# NOT $text =~ s/$pattern/"$replace"/ee;

Remember that in all these examples, the string the user types, or the string in the file etc, should not have interpolation escapes, eg user should type literally

"$1z"

(or, if the qq("") form is being used, just

$1z
)

As the internet points out

What is likely confusing is that both of these statements yield the same result:

$text2 =~ s/$match/$replace/;
$text2 =~ s/$match/$replace/e;

Thursday, February 18, 2010

Arcs

a rx ry x-axis-rotation large-arc-flag sweep-flag x y

/x-axis-rotation/ is (I think) the orientation of the minor (shorter) axis of the ellipse, measured clockwise with East being 0 deg. Obviously for an arc which is a portion of a circle, the rotation has no effect.

/large-arc-flag/ and /sweep-flag/ work together. To understand them you have to appreciate that there are always two possible circles or ellipses on whose circumference the start and end points can fall:
  • The circle where the shortest route from start to end is the anticlockwise one ('anticlockwise short')
  • The circle where the shortest route from start to end is the clockwise one ('clockwise short')

/large-arc-flag/ determines whether the longer, or the shorter path between the start and end points is used.

/sweep-flag/ determines which direction (0=negative/anticlockwise, 1=positive/clockwise) we move in round the circle when going from start to end.

Basically the combination of /large-arc-flag/ and /sweep-flag/ chooses between one of the possible circles and the other, and dictates whether we use the larger or smaller way round that circle.

That's to say, the combination 0 0 uses the same circle as 1 1 (anticlockwise short), and 0 1 uses the same circle as 1 0 (clockwise short) - and 0 1 and 1 1 take the long way round the chosen circle.

(Good diagram of this here)

NB that it is a lot easier to use the circle element instead of trying to draw a complete circle with arcs.

Wednesday, February 17, 2010

Spreadsheet lookups

In Excel, VLOOKUP(key, range, column-in-range, FALSE) looks in the leftmost column of /range/ for /key/, and if it finds it, returns the corresponding value in /column-in-range/, where the key column is number 1.

/range/ should not include the column header row if any. Also you probably should specify it as an absolute value eg $A$2:$B$10, if you intend to repeat your formula in a series of cells.

Monday, February 15, 2010

Attribute case

SVG is case-sensitive, and it appears to silently ignore attributes whose case is wrong. Eg in the marker tag, refx= is silently ignored, and only refX= will be acted on.

Markers

(For our purposes the 'vertex' is the point being marked - the line start, end or vertex depending on what kind of marker we are defining.)

markerwidth and markerheight give the actual size of the space that the marker is fitted into. The unit is (by default) the stroke-width of the line being marked. How this space is positioned relative to the vertex depends on the position of refx, refy relative to the viewbox origin - see below.

viewbox="minx miny width height" establishes a coordinate grid - the stuff you draw as the marker has its co-ordinates expressed in terms of the viewbox. Eg if box is "0 0 10 10" then you are saying 'Divide the marker space into a grid with width 10, height 10, and let its top left-hand corner have the co-ord 0,0.'

refx,refy establish the co-ords of the vertex in terms of the viewbox. Eg with refx="5" and refy="5", you are saying 'Let the vertex have the co-ords 5,5 in our system', which with the viewbox above would put the vertex in the middle.

(This is all a matter of convention: if the viewbox size, refx and refy, and the dimensions of the marker components are all increased by the same proportion, the result is exactly the same. The idea, I suppose, is that you can use whatever co-ord system you find most useful.)

Btw orient="auto" is not the default. You should specify "auto" if you want something like an arrowhead to point in the same direction that the line does.

Thursday, February 11, 2010

Perl debugger

The debugger does not seem to step through DESTROY destructors.

Wednesday, February 10, 2010

Perl file times

The conversion from file operator times to epoch times is

$^T - ((-M $file) * 86400)

(the result of -M has to be converted from days to seconds, then subtracted from Perl start time)

Consequently the current age of a file in seconds is

(-M $file) * 86400

That may seem obvious but I've got it the wrong way round more times than I care to remember.

Monday, February 1, 2010

Batch language archive cleanup

To delete from the current directory all files listed in an archive:

for /f "tokens=5" %g in ('7z l archive.7z') do del %g

Followers

Blog Archive