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 )

Tuesday, August 31, 2010

Array slices

NB the syntax for slices is

@a[1,2]

If you use $a[1,2] you'll just get $a[1] (-w gives a 'Multidimensional syntax not supported' warning).

Further note that @a[m,n] just returns elements m and n, not all elements from m to n. Use m..n for the latter (note that, as with all Perl ranges, both ends of the range must be specified).

Thursday, August 26, 2010

Perl, the clipboard and \n

If you copy stuff onto the clipboard, and then run Perl on the data (perhaps using pclip) you'll find that each line ends with 0D 0A (shown as \cM\cJ in the debugger).

chop (and chomp) only remove the 0A, not the 0D. Similarly, split(/\n/) splits on the 0A and leaves you with an array of strings each ending in 0D.

Perl's treatment of \n is very confusing, I thought that "\n" in Perl meant 0A or 0D 0A according to which platform you were on, but apparently not.

I just do another chop to get the 0D off each line - chomp does not appear to regard 0D as \n, and won't remove it.

Tuesday, August 3, 2010

.NET text file read

TextReader inpf;
inpf = new StreamReader("file");
inl = inpf.ReadLine(); // null if none left
inpf.Close();

You can also create a FileStream (as this allows Seek) and then construct a StreamReader from the FileStream (FileStream doesn't have ReadLine).

NB that the string returned by ReadLine does not have a newline on the end.

.NET text file write

Quick text file open and write:

TextWriter outf;
outf = new StreamWriter("file");
outf.WriteLine("Hello");
outf.Close();

It's possible to have the StreamWriter creation in one branch, and

outf = Console.Out;

in another, so program can be switched between stdout and file write.

Monday, August 2, 2010

C# structs

Not quite like C structs:

struct DayAnalysis { public DateTime date; public int minutes; public string lasttime;
public int ndays; public int sminutes; };
DayAnalysis DayAna = new DayAnalysis();

Followers

Blog Archive