Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Aniki
Mar 21, 2001

Wouldn't fit...
Edit 2: This question has been answered, thank you!

I'm working on some PHP code and I am trying to use preg_match and substr to check the last two characters of the $listName string to see if they are R followed by a number. I wrote the following code, but it returns false:
code:
$listName = 'GENERIC LIST R2';

if (preg_match('/^R[0-9]/',substr($listName,2,-2))) {
	echo "TRUE";
} else {
	echo "FALSE";
}
I tried using preg_quote to make sure that the substring was in the correct format, but that didn't seem to make a diffrence. I also checked the string length, which returned 2, so there doesn't seem to be any extra characters tacked onto the beginning or end of the string.

What confuses me, is that the following code works and returns true, so it seems like my regular expression works, but I don't get why this code works:
code:
if (preg_match('/^R[0-9]/','R2')) {
	echo "TRUE";
} else {
	echo "FALSE";
}
And the code using the substr won't work? Any thoughts on what I might be doing wrong? I'm running PHP 5.2.9 if that makes a difference.

Edit: There is something about the data being returned in the substr that it is not liking.

Aniki fucked around with this message at 19:11 on Jun 6, 2011

Adbot
ADBOT LOVES YOU

nielsm
Jun 1, 2009



Try preg_match('/R[0-9]$/', $listName)

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Zombywuf posted:

I thought we were talking about the prior pdf for the position of the cow over the entire set of integers. There is no appropriate scaling factor for your function in that case.

Not that it's particularly relevant, but there's a lot of work in bayesian statistics on using so-called improper priors that have infinite mass. The math works out and the statistical methods work well; the only downside is that it's a bitch to assign meaning to a distribution with infinite mass.

csammis
Aug 26, 2003

Mental Institution

Aniki posted:

Edit: There is something about the data being returned in the substr that it is not liking.

Right, because the parameters to substr are backwards. Try substr($listName, -2, 2).


e: in the future echo out the return value of substr, not just the string length, to see if it is really returning what you think it is returning.


e2: Seriously? :crossarms:

e3: Oh I get it, you're using the $ regexp on the entire string instead of using substr.

csammis fucked around with this message at 19:13 on Jun 6, 2011

Aniki
Mar 21, 2001

Wouldn't fit...

nielsm posted:

Try preg_match('/R[0-9]$/', $listName)

Awesome, thank you!

Aniki
Mar 21, 2001

Wouldn't fit...

csammis posted:

Right, because the parameters to substr are backwards. Try substr($listName, -2, 2).


e: in the future echo out the return value of substr, not just the string length, to see if it is really returning what you think it is returning.


e2: Seriously? :crossarms:

e3: Oh I get it, you're using the $ regexp on the entire string instead of using substr.

I didn't realize that I had my parameters mixed up, but that would explain why the substr value wouldn't work when I tried running the substr through a switch. The strange thing is that I was echoing out the substr value and it was returning R2. It's possible that I was writing the substr code inconsistently, so in the future I'll store similar values to a variable, so that I can eliminate the possibility of me typing out the parameters differently.

I did actually test the updated regular expression on the full string and on the substr and it worked in both instances. I'll still check to make sure that I have the substr code written correctly though.

Zombywuf
Mar 29, 2008

ultrafilter posted:

Not that it's particularly relevant, but there's a lot of work in bayesian statistics on using so-called improper priors that have infinite mass. The math works out and the statistical methods work well; the only downside is that it's a bitch to assign meaning to a distribution with infinite mass.

Got any references for that, I need to increase my reading backlog by a few MB.

ultrafilter
Aug 23, 2007

It's okay if you have any questions.


Zombywuf posted:

Got any references for that, I need to increase my reading backlog by a few MB.

There's a little bit in Gelman's book on bayesian computing, and the theory's laid out in Hartigan's book (which you'll have to find in a library--it's pretty badly out of print). Other than that, it's spread out across the objective bayesian literature, and I don't really know where to start there.

polyfractal
Dec 20, 2004

Unwind my riddle.
Is there a good alternative to phpMyAdmin that works with very large mySQL databases? I've always used phpMyAdmin but am currently working on a database with ~300 million records totaling about 13gb of data. phpMyAdmin works only sporadically - I can view the table layouts but it chokes if I try to browse the data or alter the tables. It immediately dumps me back to the main phpMyAdmin page.

I don't believe it is a database problem since the console responds perfectly fine and is very quick.

Alternatively, are there settings to tweak in phpMyAdmin that I should look into? I've tried googling for help but every problem is related to importing large databases into phpMyAdmin, not the software choking while the database is already loaded.

EPCOT
Oct 24, 2010

Experimental Prototype Community Of Tomorrow
I'm not sure if this is exactly the right place to ask this, but here it goes:

Is there any sort of program available to turn some of the more popular threads here into a sort of live chat or live blog style, with automatic updating every time someone posts something new? This is more intended for event threads, where posts multiple posts come in a short span of time. Really I want something that auto-refreshes/keeps the newest posts on the top/no page switching.

Thanks!

baquerd
Jul 2, 2007

by FactsAreUseless

EPCOT posted:

Is there any sort of program available to turn some of the more popular threads here into a sort of live chat or live blog style, with automatic updating every time someone posts something new? This is more intended for event threads, where posts multiple posts come in a short span of time. Really I want something that auto-refreshes/keeps the newest posts on the top/no page switching.

No such exact software exists because it would have to be tied directly into the forums software, but you can use a polling program like ReloadEvery (Firefox) set to short intervals to catch updates near real-time. Making the posts appear newest first (unless there's a forum option I am unaware of) would need some custom code.

Zombywuf
Mar 29, 2008

A more pertinent question may be: "How tolerant is SA of bots scraping the forums?"

JawnV6
Jul 4, 2004

So hot ...

baquerd posted:

Making the posts appear newest first (unless there's a forum option I am unaware of) would need some custom code.

The forum option you're looking for is "the reply screen".

Teabag Dome Scandal
Mar 19, 2002


I have a folder with 1600 PDF files. I need to figure out a simple way to feed a list of names into something that will search through this folder and copy any files that have a file name that starts with these names. The file names are setup with a Last Name First Name then more info but the first name and last name are the only unique part of the file name for all the files. Is there an easy way to do this in just a batch file or something? I'm no programmer so I don't really have access to any tools.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Raymn posted:

I have a folder with 1600 PDF files. I need to figure out a simple way to feed a list of names into something that will search through this folder and copy any files that have a file name that starts with these names. The file names are setup with a Last Name First Name then more info but the first name and last name are the only unique part of the file name for all the files. Is there an easy way to do this in just a batch file or something? I'm no programmer so I don't really have access to any tools.

What OS are you on? Are they nested or just all in the folder itself?

On Unix/Linux/MacOS X you could do something like (in Terminal.app, first type "cd" and drag the starting folder into the window and hit enter):
code:
mkdir filtered
while read $name; do cp "$name"*.pdf filtered/; done < list.txt
That'll copy all the files that begin with whatever is in list.txt (assumed to be in the same location as the pdf files) and end with .pdf into a new folder called "filtered". You could also do "mv" instead of "cp" to move the files.

Teabag Dome Scandal
Mar 19, 2002


Carthag posted:

What OS are you on? Are they nested or just all in the folder itself?

On Unix/Linux/MacOS X you could do something like (in Terminal.app, first type "cd" and drag the starting folder into the window and hit enter):
code:
mkdir filtered
while read $name; do cp "$name"*.pdf filtered/; done < list.txt
That'll copy all the files that begin with whatever is in list.txt (assumed to be in the same location as the pdf files) and end with .pdf into a new folder called "filtered". You could also do "mv" instead of "cp" to move the files.

My PC is windows but I could easily get it run through OSX tho I may have to wait a day or so which I guess makes my preference Windows. Sorry, I should have specified.

They are all just in one folder.

For your commands, do I need to do anything special with the list document or just have each file be its own line?

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Raymn posted:

My PC is windows but I could easily get it run through OSX tho I may have to wait a day or so which I guess makes my preference Windows. Sorry, I should have specified.

They are all just in one folder.

For your commands, do I need to do anything special with the list document or just have each file be its own line?

Yeah, just each one its own line, like

Monroe Marilyn
Doe John
McCracken Phil

or whatever fits the naming scheme. The above basically turns it into "Monroe Marilyn*.pdf" etc and moves whatever matches that into the filtered folder. I know you can do it with a .bat file too, but I haven't used those since around 1994 so I'm a little rusty.

Teabag Dome Scandal
Mar 19, 2002


Carthag posted:

Yeah, just each one its own line, like

Monroe Marilyn
Doe John
McCracken Phil

or whatever fits the naming scheme. The above basically turns it into "Monroe Marilyn*.pdf" etc and moves whatever matches that into the filtered folder. I know you can do it with a .bat file too, but I haven't used those since around 1994 so I'm a little rusty.

Not a problem! I'll see what I can do to wrangle up a OSX box today and give it a whirl. Thanks for the help.

Teabag Dome Scandal
Mar 19, 2002


Carthag posted:

What OS are you on? Are they nested or just all in the folder itself?

On Unix/Linux/MacOS X you could do something like (in Terminal.app, first type "cd" and drag the starting folder into the window and hit enter):
code:
mkdir filtered
while read $name; do cp "$name"*.pdf filtered/; done < list.txt
That'll copy all the files that begin with whatever is in list.txt (assumed to be in the same location as the pdf files) and end with .pdf into a new folder called "filtered". You could also do "mv" instead of "cp" to move the files.

So I tried this and nothing happened :( I didn't get an error but nothing got copied.

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

Raymn posted:

So I tried this and nothing happened :( I didn't get an error but nothing got copied.

Can you post a sample of the directory contents?

e.g. On the Mac, after you've typed cd and dragged the folder in and hit enter, type ls | pbcopy (that's a pipe character, shift-\ on US keyboards) then switch to your SA post and hit Paste from the Edit menu. (We don't need all of them, feel free to snip the list down to 10.)

Smugdog Millionaire
Sep 14, 2002

8) Blame Icefrog
Is there a way to modify the data stream on your microphone before it's read by other applications? Imagine I want to create an autotune program that autotunes your voice for every application (skype, ventrilo, etc.). I'd need to A) get the input from the microphone and then B) write new data to that stream. There's plenty of information on doing A but I haven't been able to find anything about doing B.
The closest I've seen is people having their own audio device+driver but I'd rather not travel down that path if possible.
This is on Windows.

nielsm
Jun 1, 2009



Smugdog Millionaire posted:

Is there a way to modify the data stream on your microphone before it's read by other applications? Imagine I want to create an autotune program that autotunes your voice for every application (skype, ventrilo, etc.). I'd need to A) get the input from the microphone and then B) write new data to that stream. There's plenty of information on doing A but I haven't been able to find anything about doing B.
The closest I've seen is people having their own audio device+driver but I'd rather not travel down that path if possible.
This is on Windows.

If you're okay with working only on Vista and up, I think a Audio Processing Object is what you want, it hooks directly into the audio chain.

Alternatively use something like Virtual Audio Cable to feed the microphone stream into an effect application, and have the effect application play back into another virtual audio cable which you can then point your real application to.


E: Okay after a bit more reading it seems that APOs can only be shipped as part of a complete audio driver package.

nielsm fucked around with this message at 13:29 on Jun 11, 2011

Double Punctuation
Dec 30, 2009

Ships were made for sinking;
Whiskey made for drinking;
If we were made of cellophane
We'd all get stinking drunk much faster!
I have a question regarding using RSA for authentication. Right now, I'm generating a random string of bytes with a public key, sending the encrypted string, decrypting it, and sending the result back. The problem is that there's a small chance the encrypted string could be the same twice, which could result in replay attacks. Is there any way to prevent this in code?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

dpbjinc posted:

I have a question regarding using RSA for authentication. Right now, I'm generating a random string of bytes with a public key, sending the encrypted string, decrypting it, and sending the result back. The problem is that there's a small chance the encrypted string could be the same twice, which could result in replay attacks. Is there any way to prevent this in code?

That is a pretty bad way of using RSA for authentication, and replay attacks are not exactly its worst fault. Is there any reason you aren't using a standard protocol like signed Diffie-Hellman?

Vaginal Engineer
Jan 23, 2007

dpbjinc posted:

I have a question regarding using RSA for authentication.
ShoulderDaemon is absolutely correct, you should be using a protocol designed specifically for authentication. Among other things, depending on which version of RSA you're using, it may not secure against chosen ciphertext attacks (basically the most appropriate model for your situation) and so an attacker could potentially learn the private key from decryptions of specially selected ciphertexts.

shrughes
Oct 11, 2008

(call/cc call/cc)

dpbjinc posted:

I have a question regarding using RSA for authentication. Right now, I'm generating a random string of bytes with a public key, sending the encrypted string, decrypting it, and sending the result back. The problem is that there's a small chance the encrypted string could be the same twice, which could result in replay attacks. Is there any way to prevent this in code?

I don't quite get how the thing you just described is a form of authentication. Let me see if I got this straight.

1. Alice generates a random string S of bytes, using a secure random number generator.
2. Alice encrypts the string with Bob's public key BobKey.pub, producing the value E(BobKey.pub, S)
3. Alice sends E(BobKey.pub, S) to Bob.
4. Bob decrypts it using the private key only he knows, producing D(BobKey.priv, E(BobKey.pub, S)).
5. Bob sends D(BobKey.priv, E(BobKey.pub, S)) back to Alice.
6. Alice checks that the result equals S, and now... Bob? is authenticated?

Is that right?

How is this supposed to ensure to Alice that any future messages she sends will be received by Bob?

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

shrughes posted:

I don't quite get how the thing you just described is a form of authentication. Let me see if I got this straight.

Technically, it proves that Bob has access to an oracle that itself has access to BobKey.priv, by demanding that Bob act as such an oracle for Alice. Which is a very shallow authentication statement, but slightly different than zero information -- in some cases something similar might be used as part of a proof that BobKey.priv exists or is compromised, either of which may be formal statements in some high-level protocol.

It's certainly not what any serious cryptographer would claim as a working authentication protocol, but I can sort of see how an amateur might stumble onto it; as written, this protocol is essentially "Bob gives his private key to anyone who asks. Anyone who can credibly claim to have either stolen this key or be capable of talking to Bob or anyone who has stolen his key will be authenticated as Bob". This is why programmers shouldn't be allowed to write their own cryptographic protocols.

Double Punctuation
Dec 30, 2009

Ships were made for sinking;
Whiskey made for drinking;
If we were made of cellophane
We'd all get stinking drunk much faster!

ShoulderDaemon posted:

That is a pretty bad way of using RSA for authentication, and replay attacks are not exactly its worst fault. Is there any reason you aren't using a standard protocol like signed Diffie-Hellman?

I'm on a Web server that doesn't have SSL set up. It costs about $70/year for an SSL certificate, and I can't figure out how to install a self-signed certificate. I'm new to Web hosting and trying to figure out how to use it, and I did this as an experiment.

It looks like my server has GPG, though. I'll try using that instead.


Edit: Apparently I have to have a dedicated IP address to use SSL. That's not that expensive. Also, I'm bad at the Internet.

ShoulderDaemon posted:

This is why programmers shouldn't be allowed to write their own cryptographic protocols.

Yeah, I'm pretty bad at designing applications but great at coding them when I'm given a design. I wasn't intending for the system to guard corporate secrets or financial information, though, just to prevent random guys on the Internet from downloading large multimedia files I have on the server.

Double Punctuation fucked around with this message at 04:20 on Jun 13, 2011

shrughes
Oct 11, 2008

(call/cc call/cc)

dpbjinc posted:

Yeah, I'm pretty bad at designing applications but great at coding them when I'm given a design. I wasn't intending for the system to guard corporate secrets or financial information, though, just to prevent random guys on the Internet from downloading large multimedia files I have on the server.

Figure out how to use a self-signed certificate, dammit. Then have people connect and authenticate by providing a password. That's easier than programming your own thing. Of course, this only works if you give people a copy of the certificate, it's meaningless if you just expect them to click "Trust this certificate."

Or forgo the SSL and just have them provide a password, if these are just copyrighted movies or something and it's not a big deal if some man-in-the-middle hacker downloads it other than bandwidth costs.

dpbjinc posted:

Yeah, I'm pretty bad at designing applications but great at coding them when I'm given a design.

Suppose I told you that logged in users were recognized because they had a username and a 16-byte hexadecimal cookie string in their cookie. Given an in-memory Dictionary<string, string> of users and their cookie values, can you write a function that looks up the user and makes sure he has the right cookie? Here's the signature:

code:
// Returns true if cookieDict[username] is equal to suppliedCookie
bool CheckCookie(Dictionary<string, string> cookieDict, string username, string suppliedCookie)

shrughes fucked around with this message at 04:54 on Jun 13, 2011

Double Punctuation
Dec 30, 2009

Ships were made for sinking;
Whiskey made for drinking;
If we were made of cellophane
We'd all get stinking drunk much faster!
code:
// check for nulls / empty strings
if (cookieDict == null) {
  throw new ArgumentNullException("Null reference provided for dictionary.",
      "cookieDict");
}
if (username == null) {
  throw new ArgumentNullException("Null value provided for user name.",
      "username");
}
if (username == "") {
  throw new ArgumentException("The user name is empty.",
      "username");
}

// There's no need to continue if there's no cookie to check
if (String.isNullOrEmpty(suppliedCookie)) { return false; }


string realCookie = "";


// prevent other threads from changing the dictionary,
// then get the real cookie
lock ([some object]) {
  cookieDict.tryGetValue(username, out realCookie);
}


// if the string is null or empty, either the user doesn't exist,
// or the user is logged out right now. Return false in this case;
// otherwise, compare the values and return the result
return (    !String.isNullOrEmpty(realCookie)    
         &&  realCookie == suppliedCookie     );

"[some object]" would be changed to the object the threads are locking on. Obviously, the lock isn't needed if it's impossible for the application to write to cookieDict and call this method at the same time.

Aredna
Mar 17, 2007
Nap Ghost
I've been working on Project Euler problems and after seeing all of the recurring themes only ~30 problems in I've decided that I want to create one program to solve them all.

I'm having trouble coming up with a good way to organize the code to run each problem. My plan was to create a new class for each problem, with common functions organized into their own classes (fractals, primes, etc).

When the user user enters a problem number it would call the function in the appropriate class, but it seems a bit ridiculous to just have 300+ if statements depending on which problem number they enter. I'm doing this in C#. Is there a better way I should organize my code?

nielsm
Jun 1, 2009



Aredna posted:

When the user user enters a problem number it would call the function in the appropriate class, but it seems a bit ridiculous to just have 300+ if statements depending on which problem number they enter. I'm doing this in C#. Is there a better way I should organize my code?

The Abstract Factory pattern would probably be the standard generalised solution.

Orzo
Sep 3, 2004

IT! IT is confusing! Say your goddamn pronouns!

nielsm posted:

The Abstract Factory pattern would probably be the standard generalised solution.
That solves absolutely nothing in this case. Honestly, having a gigantic switch statement in this case is the simplest and cleanest way.

nielsm
Jun 1, 2009



Orzo posted:

That solves absolutely nothing in this case. Honestly, having a gigantic switch statement in this case is the simplest and cleanest way.

True. The choice is between a huge switch or a huge initialiser for a list of factories. The switch is probably less code.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Just make sure all your problem classes are named something like Problem23, obey some common interface, and have a no-argument constructor. Then you can use reflection, like this:

code:
Problem problem = (Problem) Activator.CreateInstance(Type.GetType("Problem" + n));

shrughes
Oct 11, 2008

(call/cc call/cc)
What the gently caress is this poo poo, just have a goddamned array of function pointers.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Let the linker build the lookup table for you:

code:
#include <windows.h>

typedef void (*solution_function)(void);

__declspec(dllexport) void problem1(void) {
    ...
}

__declspec(dllexport) void problem2(void) {
    ...
}

int main(int argc, char **argv) {
    char func_name[8200] = "problem";
    HINSTANCE hinstLib = LoadLibrary(argv[0]);
    strcat(func_name, argv[1]);
    ((solution_function)GetProcAddress(hinstLib, func_name))();
    FreeLibrary(hinstLib);
    return 0;
}

raminasi
Jan 25, 2005

a last drink with no ice
He's using C#, so he'll need to wrap all that with some platform invoke.

npe
Oct 15, 2004
Honestly, I'd just use a collection of delegates.

code:
        delegate void ProblemDelegate();

        static void Problem1()
        {
            // ...
        }

        static void Problem2()
        {
            // ...
        }

        static void Problem3()
        {
            // ...
        }

        static void Main(string[] args)
        {
            // TODO: validate input

            int requested = Int32.Parse(args[0]);

            ProblemDelegate[] solutions = new ProblemDelegate[]
            {
                Problem1,
                Problem2,
                Problem3
            };

            solutions[requested]();
        }

Adbot
ADBOT LOVES YOU

Zombywuf
Mar 29, 2008

shrughes posted:

What the gently caress is this poo poo, just have a goddamned array of function pointers.

Depressing isn't it?

This is what Java actually believes.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply