snousle: (Default)
[personal profile] snousle
For Furr - this is an example of how you write a little C# program to filter an ISO table of contents. There is an "express" version of Visual Studio you can download for free that will run this.

This is kind of interesting because C# has been a rapidly evolving language that has incorporated some very far-out concepts, such as declarative query processing. It packs it all into a tight, rational system that remains coherent and easy to use. The C-derivative languages have traditionally been austere, long-winded, and cranky, but that has changed quite a bit since I was a computer science student. Aside from the grace and simplicity that C# has acquired in itself, Visual Studio provides the coding equivalent of a Japanese toilet - it practically blow-dries your ass for you. It's also as solid as a rock. I have spent half my life inside IDEs like this, and it's so nice to have something get easier with time. 

Yes, I know there are about twenty ways this could be better, it was just something off the top of my head. Free BJ for anyone who can spot the bug, this was just a quickie thing so I'm not actually going to fix it. :-P



using System;
using System.Collections.Generic;
using System.IO;
namespace IsoEditor
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> isoEntries = new List<string>();
            List<string> isoRecords = new List<string>();
            //Read the file into a list of strings.
            string path  = "C:\\Documents and Settings\\tony\\My Documents\\Visual Studio 2010\\Projects\\IsoEditor\\IsoEditor\\IsoCatalogExample.txt";
            StreamReader inputStream = new StreamReader(path);
            while (!inputStream.EndOfStream)
                isoEntries.Add(inputStream.ReadLine());
            //Chunk them up into records, eliminating duplicates.
            //This won't work with the example file since the duplicates aren't exact.
            //A real implementation probably wants to trim whitespace.
            int recordStart = -1;
            int i = 0;
            while (i < isoEntries.Count)
            {
                if (isoEntries[i].Contains("title"))
                {
                    if (recordStart >= 0)
                    {
                        string recordString = "";
                        for (int j = recordStart; j < i; j++)
                            recordString += isoEntries[j] + "\r\n" ;
                        //Only add the record if it isn't there already.
                        if (!isoEntries.Contains(recordString))
                        {
                            isoRecords.Add(recordString);
                        }
                    }
                    recordStart = i;
                }
                i++;
            }
            //Loop through the records, and if they contain a suitable duration, write them to an output file.
            string outputPath = "C:\\Documents and Settings\\tony\\My Documents\\Visual Studio 2010\\Projects\\IsoEditor\\IsoEditor\\IsoCatalogEdited.txt";
            StreamWriter outStream = new StreamWriter(outputPath);
            foreach(string rec in isoRecords)
            {
                int durIndex = rec.IndexOf("+ duration:");
                if (durIndex > 0)
                {
                    int hrs = Convert.ToInt32(rec.Substring(durIndex + 12, 2));
                    int min = Convert.ToInt32(rec.Substring(durIndex + 15, 2));
                    int sec = Convert.ToInt32(rec.Substring(durIndex + 18, 2));
                    int duration = hrs * 3600 + min * 60 + sec;
                    if (duration < 3600 && duration > 60)
                        outStream.Write(rec);
                }
            }
        }
    }
}

Date: 2012-09-14 07:29 am (UTC)
From: [identity profile] broduke2000.livejournal.com
Oh, with a few minor changes, this could easily end up as next year's Bikerbaer Metric Birthday Algorithmâ„¢ for Duke.

Of course, it could change to Otter's Metric Birthday Algorithmâ„¢ for Duke....

Date: 2012-09-14 02:31 pm (UTC)
From: [identity profile] thornyc.livejournal.com

I love it when you guys talk dirty.

Date: 2012-09-14 03:50 pm (UTC)

Date: 2012-09-15 03:00 am (UTC)
From: [identity profile] adminbear.livejournal.com
I think this is really my first look at C#. It's not what I expected. In some way's its quite easy and pleasant to read. However the heavy typing of the variable is a bit unnerving after having worked with Python for so many years now where nothing is typed. Also the {} and ";" required constructs would give me the willies for a while until I got used to them.

Thank you for letting us see this!

Date: 2012-09-15 11:45 am (UTC)
From: [identity profile] snousle.livejournal.com
The purpose of strong typing is to move errors from run time to compile time. Why would that be unnerving?

I've been waiting years for C# to have generic types, they're incredibly useful!

Profile

snousle: (Default)
snousle

August 2013

S M T W T F S
    123
45 678910
11121314151617
1819202122 2324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 10th, 2026 09:21 am
Powered by Dreamwidth Studios