FSTDT Forums

Community => Science and Technology => Topic started by: Captain Jack Harkness on March 02, 2013, 02:18:10 am

Title: Coders, unite!
Post by: Captain Jack Harkness on March 02, 2013, 02:18:10 am
So I've been doing some work with some VBScript for my internship (yay).

As such, I wanted to start a thread in which scripters can discuss code.  I'm working on a script that opens a word document, accepts revisions (from the "Track Changes" feature) and then saves them as PDF files.  I'll talk about how the code works in brief a bit later.

Edit:  Changing the title to appeal to a broader sense of the thread's mission.
Title: Re: Scripters, unite!
Post by: Otend on March 05, 2013, 04:13:57 pm
I think this could work well as a thread, particularly if we expanded it to cover all forms of programming.

On that note, I have a program in stasis that would effectively be a multi-medium backlog manager that uses a RNG to select something to watch, read, hear, or play.  However, the first iteration was shit, and I'll be rewriting it.  It was originally written in Python, but it's tempting to try something else for the sake of it.
Title: Re: Scripters, unite!
Post by: Captain Jack Harkness on March 06, 2013, 12:20:15 am
I think this could work well as a thread, particularly if we expanded it to cover all forms of programming.

On that note, I have a program in stasis that would effectively be a multi-medium backlog manager that uses a RNG to select something to watch, read, hear, or play.  However, the first iteration was shit, and I'll be rewriting it.  It was originally written in Python, but it's tempting to try something else for the sake of it.

Yeah, this could expand to general programming.  So do you have the source code for the current version?  How are you dividing your methods?
Title: Re: Scripters, unite!
Post by: Distind on March 06, 2013, 08:14:47 am
I may pop in here a bit on scripting, friend's writting up an RPG and is looking for programming support. RPG maker ahoy... I've heard horror stories about the scripts for this thing.
Title: Re: Scripters, unite!
Post by: Dakota Bob on March 10, 2013, 06:59:05 pm
I ordered that book Distind suggested to me in a thread I made a while ago to pick up C++ so while I waited for that to arrive I tried some things with my current Python knowledge (still learning :P) Even accomplishing as something as simple as a Rock, Paper, Scissors game feels so good, man.
Title: Re: Scripters, unite!
Post by: Captain Jack Harkness on March 11, 2013, 12:00:32 am
I ordered that book Distind suggested to me in a thread I made a while ago to pick up C++ so while I waited for that to arrive I tried some things with my current Python knowledge (still learning :P) Even accomplishing as something as simple as a Rock, Paper, Scissors game feels so good, man.

Fuck yeah getting stuff to work rocks, man!  Good to hear.
Title: Re: Scripters, unite!
Post by: Distind on March 11, 2013, 03:29:50 pm
I ordered that book Distind suggested to me in a thread I made a while ago to pick up C++ so while I waited for that to arrive I tried some things with my current Python knowledge (still learning :P) Even accomplishing as something as simple as a Rock, Paper, Scissors game feels so good, man.
I'm working at something new myself, took two days to get the basics of the system together, including massively simplifying the damnedable database so I'd have a sane range of things to work with. Every basic thing I manage to do right feels like a victory, even after 10 years of doing this.

Also, Ruby, it's the evil love child of Visual Basic and Perl. Or perhaps no love was involved.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on March 11, 2013, 07:15:53 pm
So I've heard a few people in IRC rag on Object-Oriented Programming, and I wanted to say that while it can't teach you good coding and documenting, it sure the fuck can make troubleshooting bugs a crapton easier. :P
Title: Re: Coders, unite!
Post by: Sleepy on March 11, 2013, 10:26:18 pm
It's painful to imagine all your code as one giant clusterfuck, everything freely calling everything else, variables just floating around. Debugging would be far worse than hell.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on March 12, 2013, 07:58:36 pm
It's painful to imagine all your code as one giant clusterfuck, everything freely calling everything else, variables just floating around. Debugging would be far worse than hell.

Exactly.  Modules can be frustrating if they don't work, and when things need dependencies, that can suck sometimes.  However, it's still easier than one big monolith of code.

So who here does stuff in Perl?  I'm doing stuff, but it's not simple.  It's retarded server authentication stuff that refuses to work.
Title: Re: Coders, unite!
Post by: Sleepy on March 19, 2013, 01:05:59 pm
We're doing assembly language in my class now. T_T
Title: Re: Coders, unite!
Post by: Distind on March 20, 2013, 08:09:36 pm
We're doing assembly language in my class now. T_T
Good luck Sleepy, just think of it as doing coding one word per line.
Title: Re: Coders, unite!
Post by: Yla on March 20, 2013, 11:10:04 pm
Apart from the coding I'm doing for my thesis, I'm working at the moment on a plotter for my dad's sensor data and a bookkeeping application for my MMO guild as private projects.

Hate/love assembly language.
Title: Re: Coders, unite!
Post by: Dakota Bob on March 23, 2013, 05:16:20 pm
Well, that C++ book finally arrived after more than three weeks.
Code: [Select]
#include <iostream>
int main() {
std::cout << "Holy fuck, Amazon";
std::cout << "Could you have taken any longer?";
return 0;
}
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on March 23, 2013, 05:24:06 pm
I'll say more later because I have to go, but Perl is fucking awesome. <3
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 04, 2013, 01:05:02 am
So I learned a few cool things.  Apparently the memory watch in Visual Studio can evaluate complex statements.  Also, I now know how to run a VBScript from a Perl Script.  Being able to integrate two different languages and have one invoke the other feels freaking epic.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 07, 2013, 05:47:52 pm
In case you weren't aware, it's possible to substitute string patterns in Perl with a simple regular expression.

Code: [Select]
use strict;
use warnings;


my $test_string = 'test%20string%20fuck%20yeah%20bitches';
$test_string =~ s/%20/ /g;
print $test_string;

Let me explain.  The =~ indicates a regular expression.  The s stands for substitution.  The %20 is what you want to replace, the ' ' (Space) is what you want as the replacement, and the g is for global.  Having a global switch just means that you'll replace all instances, as opposed to just the first.

Note that on letters, they'll be case sensitive unless you denote it with an i at the very end next to the g.
Title: Re: Coders, unite!
Post by: Joey on April 07, 2013, 05:56:58 pm
I fucking love regular expressions! Everyone in my classes seems to hate them, I cannot for the life of me understand why. :scratch:
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 07, 2013, 06:01:19 pm
I fucking love regular expressions! Everyone in my classes seems to hate them, I cannot for the life of me understand why. :scratch:

They're simple and powerful indeed.  Also, even VBScript supports them, albeit in a rather unusual way.  You have to create a regular expression object and set the properties and all that jazz.
Title: Re: Coders, unite!
Post by: Distind on April 08, 2013, 02:37:55 pm
I fucking love regular expressions! Everyone in my classes seems to hate them, I cannot for the life of me understand why. :scratch:
Wait till you need to read someone else's.

I hate the things because of the damned syntax, I understand the concepts just fine, but the syntax is just miserable.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 18, 2013, 04:07:06 am
So I just noticed something cool.  As long as the logic's not too complex, you can use a ternary statement to replace a switch statement in Perl.  I mean, you COULD use the Switch statement, but you'd have to have the Switch.pm module on the machine running the script.  If you were to instead use a ternary operator, you could do the logic with nothing more than Perl's native language methods.

(click to show/hide)

I could see such logic working particularly well for a menu system.

Edit:  Shit, why hardcode a value for selection?  You can shift and pass it through the command line when testing out the script running it.  Doing so made me realize that you can test for multiple conditions with the right syntax.

(click to show/hide)

Yeah, this works pretty slickly.  In fact, I can show you some sample tests from my machine.

(click to show/hide)

So you can take this logic one step further and do multiple variable assignment with your ternary statement.

(click to show/hide)

When you run this, you get the following results:

(click to show/hide)

I'm not quite sure how far you can take this thinking in other languages because I haven't tested it.  However, I now know that it works in Perl, and that's awesome.
Title: Re: Coders, unite!
Post by: Distind on April 18, 2013, 06:33:12 am
Perl is a natural language for hacking the crap out of, just beware of efficiency concerns should you ever use this in a place where that would be critical.
Title: Re: Coders, unite!
Post by: Yla on April 18, 2013, 06:50:22 am
@B-Man:

If you've gone this far, you've gone too far. :-p (http://us.thedailywtf.com/Articles/Macro_Polo.aspx)
Title: Re: Coders, unite!
Post by: Sigmaleph on April 18, 2013, 05:53:23 pm
I mostly do some simple mathy stuff for school on Matlab. Nothing more complicated than Gauss-Jordan elimination, so far.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 18, 2013, 07:52:20 pm
@B-Man:

If you've gone this far, you've gone too far. :-p (http://us.thedailywtf.com/Articles/Macro_Polo.aspx)

I see the bug.

(click to show/hide)
Title: Re: Coders, unite!
Post by: Yla on April 19, 2013, 06:25:58 pm
There are two conceptually bigger problems with that example. One, it uses 26 checks for a single character, when you can do it more easily with 2. (easily as in, it's faster to code and you don't invite typos, in addition to the performance gain). Second, there's almost certainly a library function for that, so you don't need to code it at all. Always use the appropriate tool.
(That blog, btw, is an enjoyable read and quite informative in showing what not to do.)

I used Matlab during my internship. The language is quite nice, although I despaired at the fact that array indices started counting at 1. :o :o My brain was full of fuck until I got accustomed to the new way of thinking. :D

I'll now be working with generated code for my thesis. Vector intrinsics have a very unwieldy syntax, and working with a rather large block of code that uses them almost exclusively would be a bitch otherwise. (The first draft I wrote by hand using only copy&paste. It was a monotonous, error-prone affair and, oh, it didn't work either)
Title: Re: Coders, unite!
Post by: Sleepy on April 19, 2013, 07:46:50 pm
I used Matlab during my internship. The language is quite nice, although I despaired at the fact that array indices started counting at 1. :o :o My brain was full of fuck until I got accustomed to the new way of thinking. :D

I've used R a decent amount, and its indices also start at 1. It's quite maddening, especially since I'm in three comp sci courses right now that use several different languages.
Title: Re: Coders, unite!
Post by: RavynousHunter on April 21, 2013, 01:35:30 am
I'm thinking of creating my own mod for Minecraft, while following Pahimar's (https://www.youtube.com/user/Pahimar) most excellent Let's Mod series.  Having played with a few of the FTB packs (Mindcrack, Beta, Direwolf20, and Ultimate), I've noticed that steam doesn't get a lot of love.  Yeah, Railcraft has boilers and steam, but using them for anything is painfully inefficient, not to mention a fair bit inelegant with all but the snazziest of setups.

So, my solution?  A steampunk-themed mod inspired by Thief!  I wanna use something kinda resembling how real-life steam works, you have the boilers which have to have water and such to keep running, with steam lines and such, BUT...you need to keep an eye on your pressure levels, which will be conveniently handled by gauges (steampunk isn't steampunk without gauges!) that you can place on steam lines or boilers to give you a visual indication of the pressure (in PSI) in a given steam system.  Steam system being defined as either a boiler or a network of contiguous pipes and any machines to which they're connected.

What would happen if you overpressurize a system, with no emergency release (also craftable, attaches to either a boiler or steam pipe)?  Depends on what you're talkin about, but in general, things explode.  A standard iron boiler would likely end up with roughly the power of half a block of dynamite, whereas an industrial-grade boiler would explode with the force of 4 blocks of dynamite, and the other grades and types will scale between the two.  If its pipes that are overpressurized, a random pipe on the system will be chosen, and it'll blow up, but while it won't do any structural damage (this may change, depending on pipe grade), any living things nearby will take a pretty large hit from the boiling-hot steam and shrapnel.

But!  Its not all steam, after all, Thief isn't all technology.  There's some magic, as well, and I've always been fond of well-done magitech.  One thing I plan on making is steam golems.  They're similar to iron golems, but require more in the way of resources, have a bit less health, but will hit much harder.  Now that I think about it...I wonder how cool it'd be to have a golem that would be able to build a building off blueprints.  Just mark a chest (or other inventory) for them to draw from, give them a blueprint to work with, and they'll set to building!  Hell, maybe work in something to where you can have more than one working on the same building project, as a team...

Anywho, whaddya think?
Title: Re: Coders, unite!
Post by: Dan on April 21, 2013, 11:17:59 am
I used Matlab during my internship. The language is quite nice, although I despaired at the fact that array indices started counting at 1. :o :o My brain was full of fuck until I got accustomed to the new way of thinking. :D

I've used R a decent amount, and its indices also start at 1. It's quite maddening, especially since I'm in three comp sci courses right now that use several different languages.
Fortran also uses one-based arrays. I find it convenient that the number of array elements is equal to the array's upper bound.

In Basic of course you can define your own lower bound. The default is zero, but some dialects let you change that.
Title: Re: Coders, unite!
Post by: Sleepy on April 21, 2013, 11:26:28 am
I used Matlab during my internship. The language is quite nice, although I despaired at the fact that array indices started counting at 1. :o :o My brain was full of fuck until I got accustomed to the new way of thinking. :D

I've used R a decent amount, and its indices also start at 1. It's quite maddening, especially since I'm in three comp sci courses right now that use several different languages.
Fortran also uses one-based arrays. I find it convenient that the number of array elements is equal to the array's upper bound.

In Basic of course you can define your own lower bound. The default is zero, but some dialects let you change that.

It was admittedly handy since we used R for a lot of statistical analysis and simulations, so we didn't have to second-guess ourselves when using and plotting array elements.

For my object-oriented programming class, we're currently making Facebook. It's quite fun, I must say, and I can't wait to see how it turns out. The worst part is the persistence layer, because we're manually saving files to disk, then manually repopulating the system at bootstrap time. We've been fooling around with CSS templates since we want our page to look pretty, but they're cranky for some reason.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 28, 2013, 04:14:32 am
So I was a bit bored and had a flash of inspiration on how to convert strings to all lowercase or uppercase with Perl.  Sure, you could use the built in lc() and uc() methods, but I wanted to do something a bit more fun.

Code: [Select]
use strict;
use warnings;

my $test;
my $x;
my ( $uppercaseTest, $lowercaseTest );
my $character;

$test = $ARGV[0];
$x = 0;

unless
( defined $test
)
{
$test = "pONy";
}

($uppercaseTest, $lowercaseTest) = ("", "");

while
( $x < length $test
)
{
$character = substr $test, $x, 1;

if
( $character =~ m/[a-z]/i
)
{
$uppercaseTest .= $character | " ";
$lowercaseTest .= $character & "_";
}
else
{
$uppercaseTest .= $character;
$lowercaseTest .= $character;
}
$x++;
}

print $uppercaseTest, "\n";
print $lowercaseTest, "\n";

So I realize this could be simpler with the built in functions for uppercase and lowercase conversion, but I felt like having fun.  I hope you guys get what's going on.  If you don't, feel free to ask.
Title: Re: Coders, unite!
Post by: Joey on April 28, 2013, 05:40:07 am
I see your Perl and raise you a Java:

Code: [Select]
package blah;

public class StringChanger {
   
    public static void main (String[] args) {
       
        String test = "pONy 2012";
        String upper = "";
        String lower = "";
       
        for (int i=0; i<test.length(); i++) {
       
            char x = test.charAt(i);
            lower += x >= 65 && x <= 90  ? (char)(x+32) : x;
            upper += x >= 97 && x <= 122 ? (char)(x-32) : x;
        }
       
        System.out.println(upper + "\n" + lower);
    }
}

Btw, can I ask what this part of your code does?

Code: [Select]
$uppercaseTest .= $character | " ";
$lowercaseTest .= $character & "_";

I get that it's appending the new strings with upper or lower-case characters, but what does the | " " and & "_" mean exactly?
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 28, 2013, 01:43:47 pm
I see your Perl and raise you a Java:

Code: [Select]
package blah;

public class StringChanger {
   
    public static void main (String[] args) {
       
        String test = "pONy 2012";
        String upper = "";
        String lower = "";
       
        for (int i=0; i<test.length(); i++) {
       
            char x = test.charAt(i);
            lower += x >= 65 && x <= 90  ? (char)(x+32) : x;
            upper += x >= 97 && x <= 122 ? (char)(x-32) : x;
        }
       
        System.out.println(upper + "\n" + lower);
    }
}

Btw, can I ask what this part of your code does?

Code: [Select]
$uppercaseTest .= $character | " ";
$lowercaseTest .= $character & "_";

I get that it's appending the new strings with upper or lower-case characters, but what does the | " " and & "_" mean exactly?

Sure, I can explain that.  To understand what those mean, you have to understand that | and & are not boolean operators in their current context, but bitwise operators.  We're actually doing a bit of "binary math."  The explanation is a bit long, so have some spoiler tags.

(click to show/hide)

I hope that clears things up.

Also, " " and "_" can be written a bit less cryptically in hexidecimal as chr 0x20 and chr 0xDF, respectively. They can also be written in binary chr 0b010000 and chr 0b1011111, respectively.

Code: [Select]
$uppercaseTest .= $character | chr 0x20;
$lowercaseTest .= $character & chr 0xDF;

or

Code: [Select]
$uppercaseTest .= $character | chr 0b0100000;
$lowercaseTest .= $character & chr 0b1011111;

Haha, okay, I just realized another way of doing things.  Instead of explicitely stating the inverse, you can use the unary "not" bitwise operator.  You can use this on the hex or binary code,, or you can use decimal numbering to represent the flag value.

Code: [Select]
$uppercaseTest .= $character | chr  32;
$lowercaseTest .= $character & chr ~32;
Title: Re: Coders, unite!
Post by: Joey on April 28, 2013, 08:24:40 pm
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 28, 2013, 08:30:44 pm
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.

Cool, I'm glad you get it.  Now that I think about it, I want to use bitwise operators outside of an ASCII character context now. XD
Title: Re: Coders, unite!
Post by: Yla on April 29, 2013, 06:41:32 am
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.

Cool, I'm glad you get it.  Now that I think about it, I want to use bitwise operators outside of an ASCII character context now. XD
Bitwise operators are often used for poor man's flag field or compressed bitmaps. Store up to 32 booleans inside one int and use these to access.

Also, both of you could pimp your code to be more understandable. Joey: A person reading your code would end up wondering about the significance of the numbers 65, 90, 97 and 122. You need to know the ASCII table in detail for the why. Simply write 'a', 'z', 'A' and 'Z' and it's immediately clear what these checks are doing and why. B-Man: In your case I'd recommend the reverse. You OR (or add) 32 to the character. That 32 is whitespace in ASCII is irrelevant for that, and confusing to someone trying to understand your code. Even moreso for the underscore. I'd recommend writing it in binary, since then you immediately see which bits are being manipulated.
Title: Re: Coders, unite!
Post by: Captain Jack Harkness on April 29, 2013, 08:43:47 am
Makes perfect sense. I'd be willing to be it is slightly more efficient than adding numerical values to characters.

Cool, I'm glad you get it.  Now that I think about it, I want to use bitwise operators outside of an ASCII character context now. XD
Bitwise operators are often used for poor man's flag field or compressed bitmaps. Store up to 32 booleans inside one int and use these to access.

Also, both of you could pimp your code to be more understandable. Joey: A person reading your code would end up wondering about the significance of the numbers 65, 90, 97 and 122. You need to know the ASCII table in detail for the why. Simply write 'a', 'z', 'A' and 'Z' and it's immediately clear what these checks are doing and why. B-Man: In your case I'd recommend the reverse. You OR (or add) 32 to the character. That 32 is whitespace in ASCII is irrelevant for that, and confusing to someone trying to understand your code. Even moreso for the underscore. I'd recommend writing it in binary, since then you immediately see which bits are being manipulated.

I was thinking about commenting my code.  I originally came up with it in a flash of inspiration, which is why I initially didn't.  Besides, I wanted to see if you could figure out what's going on first.