As was foretold, we've added advertisements to the forums! If you have questions, or if you encounter any bugs, please visit this thread: https://forums.penny-arcade.com/discussion/240191/forum-advertisement-faq-and-reports-thread/
Options

The beginner programming thread

1235763

Posts

  • Options
    mausmalonemausmalone Registered User regular
    edited November 2007
    Rohaq wrote: »
    (snip)

    So either I've messed up, or the information on the sheet's incorrect, which is it, and why? ó_O

    The information on the sheet is incorrect ... or rather too vague. There are multiple integer types in C++, and a signed 16-bit integer has a cap of 32,767. Technically I think that you are actually wrong in that Visual C++ can handle larger integers than 2.1bn (but I don't know if any of them are prime). What you could do (and this would be very cross platform) is:
    unsigned long int ulStartPoint = (~)((unsigned long int)0);
    

    This will give you an unsigned long int with all the bits set to 1. In VC++ specifically you can do:
    unsigned long int ulStartPoint = ULONG_MAX;
    

    This max is about 4.2bn. Counting down from there using your basic solver should give you the highest prime integer that VC++ can handle (with built in types).

    16bit integers haven't been the standard since well over a decade ago.
    LoneIgazra wrote:
    Speaking of which, I could never get those fucking things to work, so my GL remains rudimentary.

    Here, in a nutshell, is how to render with a vertex array in OpenGL:
    /***************************
    PART I: DEFINING OUR TYPES
    ****************************/
    // Using structs here requires that you link with
    // packed structures.
    struct Vertex_S {
    	float x, y, z;
    };
    
    struct TexCoord_S
    {
    	float u,v;
    };
    
    struct Face_S
    {
    	unsigned short p1,p2,p3;
    };
    
    /***************************
    PART II: INSTANTIATING DATA
    ****************************/
    Vertex_S *vtx_list;
    Vertex_S *normal;
    TexCoord_S *tex_coords;
    Face_S *face_list;
    
    /*********************
    ASIDE: HOW THE FACE LIST WORKS:
    	Instead of drawing a triangle by sending first a normal, 
    	then tex coord, then the vertex 3 times,
    	we send all the vertices, normals, and tex coords at once
    	then a face list that tells what order they go in. OpenGL
    	will go through the face list one item at a time and use it
    	to create the shape.  This is equivalent to going through the
    	entire face list array like this:
    	
    	glBegin(GL_TRIANGLES);
    	for (i=0;i<numFaces;i++) {
    		glNormal3f(normal[face_list[i].p1].x, normal[face_list[i].p1].y, normal[face_list[i].p1].z);
    		glTexCoord2f(tex_coord[face_list[i].p1].u, tex_coord[face_list[i]p1].v);
    		glVertex3f(vtx_list[face_list[i].p1].x, vtx_list[face_list[i].p1].y, vtx_list[face_list[i].p1].z);
    		glNormal3f(normal[face_list[i].p2].x, normal[face_list[i].p2].y, normal[face_list[i].p2].z);
    		glTexCoord2f(tex_coord[face_list[i].p2].u, tex_coord[face_list[i]p2].v);
    		glVertex3f(vtx_list[face_list[i].p2].x, vtx_list[face_list[i].p2].y, vtx_list[face_list[i].p2].z);
    		glNormal3f(normal[face_list[i].p3].x, normal[face_list[i].p3].y, normal[face_list[i].p3].z);
    		glTexCoord2f(tex_coord[face_list[i].p3].u, tex_coord[face_list[i]p3].v);
    		glVertex3f(vtx_list[face_list[i].p3].x, vtx_list[face_list[i].p3].y, vtx_list[face_list[i].p3].z);
    	}
    	glEnd();
    	
    	But because of the bulk memory transfer this goes a lot faster.
    
    **********************/
    
    /**************************
    PART III: DRAWING THE VERTEX ARRAY
    ***************************/
    // Up here we set up our normal drawing parameters
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_BLEND);
    // etc ...
    
    // Send Vertex Array
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3, GL_FLOAT, 0, vtx_list); // 3 dimensional vertices, floating point, no bytes between them, and our pointer
    // Send Normal Array
    glEnableClientState(GL_NORMAL_ARRAY);
    glNormalPointer(GL_FLOAT, 0, (Vertex_S *)normal); // floating point, no bytes between them, and our pointer (3D assumed for normals)
    // Send Texture Coordinate Array
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glTexCoordPointer(2, GL_FLOAT, 0, tex_coords); // 2D vertices, floating point, no bytes between them, and our pointer
    
    // Draw The Objects that we sent using a face list
    // using Triangles, we have 3*num_faces vertices total, they're indexed using unsigned shorts, and stored in that pointer
    glDrawElements(GL_TRIANGLES, 3*num_faces, GL_UNSIGNED_SHORT, face_list);
    
    // Unload the arrays
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    

    hope that helps!

    mausmalone on
    266.jpg
  • Options
    RohaqRohaq UKRegistered User regular
    edited November 2007
    I mailed the module leader about the discrepancy, since I already submitted the work. The sheet definitely says the highest a signed 32-bit integer can be if 0x7FFF, so I guess that's a whoopsie on their part.

    Also, thanks maus, but we're sticking to signed integers for the time being, and haven't gotten around to unsigned integers yet - I'd rather submit work through my own research too, rather than just copying your code without knowing precisely how it works - kind of defeats the purpose of learning :)

    Rohaq on
  • Options
    mausmalonemausmalone Registered User regular
    edited November 2007
    Rohaq wrote: »
    I mailed the module leader about the discrepancy, since I already submitted the work. The sheet definitely says the highest a signed 32-bit integer can be if 0x7FFF, so I guess that's a whoopsie on their part.

    Also, thanks maus, but we're sticking to signed integers for the time being, and haven't gotten around to unsigned integers yet - I'd rather submit work through my own research too, rather than just copying your code without knowing precisely how it works - kind of defeats the purpose of learning :)

    The giant block of code wasn't for you :P Only the beginning part suggesting that unsigned integers can go higher. Heck, you found INT_MAX, surely you would've found ULONG_MAX with a little more research.

    The rest of it is OpenGL stuff for LoneIgazra to kick around.

    mausmalone on
    266.jpg
  • Options
    JaninJanin Registered User regular
    edited November 2007
    Rohaq wrote: »
    I mailed the module leader about the discrepancy, since I already submitted the work. The sheet definitely says the highest a signed 32-bit integer can be if 0x7FFF, so I guess that's a whoopsie on their part.

    Also, thanks maus, but we're sticking to signed integers for the time being, and haven't gotten around to unsigned integers yet - I'd rather submit work through my own research too, rather than just copying your code without knowing precisely how it works - kind of defeats the purpose of learning :)

    Signed/unsigned is pretty easy. A signed 32-bit integer is actually just an unsigned 31-bit integer with the extra bit storing whether it's positive or negative. Therefore, the maximum value of a signed 32-bit is (2^31) - 1. An unsigned integer uses the full range for the numeric value, so its maximum value is (2^32) - 1.

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    RohaqRohaq UKRegistered User regular
    edited November 2007
    Oh, I know that, I meant on how to even use an unsigned integer. Although it couldn't hurt to learn, I'm sure :)

    Thanks for the advice everybody, I'll see what they send back :)

    EDIT:- We've learned all about that in our Computer Tech module, but I do need to brush up for the exam in two weeks, thanks Janin ;)

    Rohaq on
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    mausmalone wrote: »
    (snip)
    hope that helps!

    Thanks for the example. One question though: What does the following mean: "Using structs here requires that you link with packed structures." ?

    LoneIgadzra on
  • Options
    DrFrylockDrFrylock Registered User regular
    edited November 2007
    Janin wrote: »
    Signed/unsigned is pretty easy. A signed 32-bit integer is actually just an unsigned 31-bit integer with the extra bit storing whether it's positive or negative.

    Er, well, not usually. Mostly negatives are stored with Two's Complement where one bit is indeed used for the sign but the remainder isn't interpreted as an unsigned when the value is negative. This also increases the range of an integer value by one whole value, since it avoids the encoding for "negative zero."

    Integer types in C/++ are confusing. Shorts are at least 16 bits, longs are at least 32, and long longs are at least 64. An int is supposed to be the natural word size of the processor although it also depends on the compiler, the OS and the memory model in use. I believe if you were writing in Turbo C++ circa 1991 on a 486 with the small memory model, int was 16 bits. On most OSes today, int is 32 bits but I suppose on 64-bit OSes it is probably 64.

    DrFrylock on
  • Options
    mausmalonemausmalone Registered User regular
    edited November 2007
    mausmalone wrote: »
    (snip)
    hope that helps!

    Thanks for the example. One question though: What does the following mean: "Using structs here requires that you link with packed structures." ?

    Some C++ compilers will align structures so that each member of the structure aligns with a word of memory (a word being the standard unit for a memory read on that machine ... 32bits on an average PC). So if you had:
    struct example_struct {
       int my_int;
       float my_float;
       char my_char;
       unsigned short my_short;
    }
    

    That structure would take up 16bytes in Win32 if the compiler aligns the members to word boundaries. You'd have:

    int: 4 bytes
    float: 4 bytes
    char: 1 byte
    *** 3 empty bytes
    unsigned short: 2 bytes
    *** 2 empty bytes

    This theoretically can speed things up, but the difference is usually minimal. If you told the compiler to pack the structures you'd get the same except with none of the empty bytes. The benefit to this is that if you had:
    struct Vertex_S {
       float x,y,z;
    }
    
    int numVertices = 100;
    Vertex_S *vtx_array = new Vertex_S[numVertices];
    

    it would be exactly the same as:
    int numVertices = 100;
    float *vtx_array = new float[numVertices*3];
    

    Except that you can access the parts of a vertex using .x, .y, and .z. But since the memory layout is the same, you can still pass it to OpenGL as an array of floats.

    To pack structures in Visual C++ I think you just need to put this line at the top of your file:
    #pragma pack (1)
    

    EDIT: Would it be helpful if I put an example of how to make a sphere or something using a vertex array?

    mausmalone on
    266.jpg
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    No thanks on the extra examples; I think the packing is why mine wouldn't work before. I was just following some example that used structs like that, and I assumed the structs were packed automatically since the example worked. (Or I didn't know it was called packing at the time, but the concept had occurred to me.)

    LoneIgadzra on
  • Options
    JaninJanin Registered User regular
    edited November 2007
    DrFrylock wrote: »
    Janin wrote: »
    Signed/unsigned is pretty easy. A signed 32-bit integer is actually just an unsigned 31-bit integer with the extra bit storing whether it's positive or negative.

    Er, well, not usually. Mostly negatives are stored with Two's Complement where one bit is indeed used for the sign but the remainder isn't interpreted as an unsigned when the value is negative. This also increases the range of an integer value by one whole value, since it avoids the encoding for "negative zero."

    Integer types in C/++ are confusing. Shorts are at least 16 bits, longs are at least 32, and long longs are at least 64. An int is supposed to be the natural word size of the processor although it also depends on the compiler, the OS and the memory model in use. I believe if you were writing in Turbo C++ circa 1991 on a 486 with the small memory model, int was 16 bits. On most OSes today, int is 32 bits but I suppose on 64-bit OSes it is probably 64.

    2's complement is something that would be important to learn for actually implementing an adder circuit, but from the programming language side treating it as "31+1" bits works well enough. If a programmer wants to get down and dirty with bitfields where the detail matters, they'd already know how signed/unsigned works.

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    Godammit, what the hell is wrong with Perl documentation? I just want to know how its strings work! How can I index out a single character? What the hell does this mean?
    Error message: Use of uninitialized value in pattern match (m//) at MessageFinder.pl line 36.
    
    Line 36: my $pattern = /d/;
    

    LoneIgadzra on
  • Options
    JaninJanin Registered User regular
    edited November 2007
    try: my $pattern = /\d/;

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    jackaljackal Fuck Yes. That is an orderly anal warehouse. Registered User regular
    edited November 2007
    Godammit, what the hell is wrong with Perl?

    Fixed.

    jackal on
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    Janin wrote: »
    try: my $pattern = /\d/;

    I'm not trying to match a digit, just the letter d. :(

    LoneIgadzra on
  • Options
    ASimPersonASimPerson Cold... and hard.Registered User regular
    edited November 2007
    DrFrylock wrote: »
    Integer types in C/++ are confusing. Shorts are at least 16 bits, longs are at least 32, and long longs are at least 64. An int is supposed to be the natural word size of the processor although it also depends on the compiler, the OS and the memory model in use. I believe if you were writing in Turbo C++ circa 1991 on a 486 with the small memory model, int was 16 bits. On most OSes today, int is 32 bits but I suppose on 64-bit OSes it is probably 64.

    Indeed, 64-bit machines mix it up a little. Here's what I got on a x86 machine:
    sizeof int 4
    sizeof long: 4
    sizeof longlong 8

    And here's what I got on a both Solaris and a x86-64 box:
    sizeof int 4
    sizeof long: 8
    sizeof longlong 8

    So int stays 32-bit, but longs are no longer the same size as ints. This should be fun in the years ahead....

    ASimPerson on
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    I've never understood why a long was the same as an int...

    LoneIgadzra on
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    jackal wrote: »
    Godammit, what the hell is wrong with Perl?

    Fixed.

    Agreed. But still, you'd think in one of this fucking tutorials I'm reading someone would mention the way to assign a pattern to a variable and use it in a match expression without popping a warning? Because that seems like an obvious thing to me...

    Edit: Sorry about dual post, was in a hurry.

    LoneIgadzra on
  • Options
    SmasherSmasher Starting to get dizzy Registered User regular
    edited November 2007
    I've never understood why a long was the same as an int...

    This is completely a guess, but it might be an artifact from when ints were usually 16 bits.

    Smasher on
  • Options
    JaninJanin Registered User regular
    edited November 2007
    It's been a long time since I used any Perl, and that was a few weeks in one class, but this seems to work as expected:
    use strict;
    my($string) = "test";
    print ($string =~ m/t/);
    print "\n";
    print ($string =~ m/d/);
    print "\n";
    

    If it doesn't, could you post your full code?

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    Janin wrote: »
    It's been a long time since I used any Perl, and that was a few weeks in one class, but this seems to work as expected:
    use strict;
    my($string) = "test";
    print ($string =~ m/t/);
    print "\n";
    print ($string =~ m/d/);
    print "\n";
    

    If it doesn't, could you post your full code?

    I actually fixed the problem by commenting out this line:
    use warnings;
    

    LoneIgadzra on
  • Options
    JaninJanin Registered User regular
    edited November 2007
    Janin wrote: »
    It's been a long time since I used any Perl, and that was a few weeks in one class, but this seems to work as expected:
    use strict;
    my($string) = "test";
    print ($string =~ m/t/);
    print "\n";
    print ($string =~ m/d/);
    print "\n";
    

    If it doesn't, could you post your full code?

    I actually fixed the problem by commenting out this line:
    use warnings;
    

    D:D:D:D:D:

    You did not fix the problem, you just told the computer to shut up. From the very first post in this thread:
    Janin wrote: »
    Stuff that doesn't go anywhere specific

    Always turn on compiler or interpreter warnings, and set them to maximum. If possible, make them errors rather than warnings. Compiler writers are very smart, and probably know more than you do. Read the documentation to your compiler or interpreter to determine how to set warning and error levels.

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    LewishamLewisham Registered User regular
    edited November 2007
    Oh man, I just lol'd so hard.

    I wish programming really was about just making all errors go away! A suppressed error is an error that doesn't exist!

    Lewisham on
  • Options
    MonoxideMonoxide Registered User, ClubPA regular
    edited November 2007
    Lewisham wrote: »
    Oh man, I just lol'd so hard.

    I wish programming really was about just making all errors go away! A suppressed error is an error that doesn't exist!

    gcc -w source.cpp

    Shut the fuck up gcc, my code was righter than you'll ever know.

    Monoxide on
  • Options
    LewishamLewisham Registered User regular
    edited November 2007
    Monoxide wrote: »
    Lewisham wrote: »
    Oh man, I just lol'd so hard.

    I wish programming really was about just making all errors go away! A suppressed error is an error that doesn't exist!

    gcc -w source.cpp

    Shut the fuck up gcc, my code was righter than you'll ever know.

    -wall for EXTRA LIES.

    Pro Java tip:
    try
    {
    ...stuff...
    }
    catch (Exception e)
    {
    // I'll code this later. Promise!
    }

    AND THEN SHIP

    Lewisham on
  • Options
    ASimPersonASimPerson Cold... and hard.Registered User regular
    edited November 2007
    For maximum gcc fun, we (I used to be a TA) made our students compile with this:
    gcc -Wall -Werror -pedantic -ansi -O2

    The ansi isn't really strictly necessary (as far I could tell, it just prevents C++ style comments), but the rest is great, especially for people who are starting out. pedantic catches a ton of mistakes that newbie C coders are prone to make.

    ASimPerson on
  • Options
    jackaljackal Fuck Yes. That is an orderly anal warehouse. Registered User regular
    edited November 2007
    Option Explicit... OFF! HAHA

    On Error Goto... NEXT! HEEHEE! I AM BEST PROGRAMMER!

    The boss of my boss actually did that. She was so bad at programming they moved her into management, so that she couldn't hurt anything.

    The other people I work with are better, but not what I would call good. If I had a dollar for every time I've seen one of them put a database query directly in a control's event handler I would be in a higher tax bracket. I need a new job.

    jackal on
  • Options
    JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited November 2007
    That sound sounds horrible.

    Jasconius on
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    Janin wrote: »
    Janin wrote: »
    It's been a long time since I used any Perl, and that was a few weeks in one class, but this seems to work as expected:
    use strict;
    my($string) = "test";
    print ($string =~ m/t/);
    print "\n";
    print ($string =~ m/d/);
    print "\n";
    

    If it doesn't, could you post your full code?

    I actually fixed the problem by commenting out this line:
    use warnings;
    

    D:D:D:D:D:

    You did not fix the problem, you just told the computer to shut up. From the very first post in this thread:
    Janin wrote: »
    Stuff that doesn't go anywhere specific

    Always turn on compiler or interpreter warnings, and set them to maximum. If possible, make them errors rather than warnings. Compiler writers are very smart, and probably know more than you do. Read the documentation to your compiler or interpreter to determine how to set warning and error levels.

    I know that, that was mainly a joke and it turns out the script didn't work anyway, so I changed the way I was building my search patterns. (I am still pissed that no documentation immediately discusses the "right" way to do that.) Here's the entire code that now works:
    #!/usr/bin/env perl
    
    use strict;
    #use warnings; # pops completely unexplained warnings about my pattern variables
    
    my $debug = 0;
    my $minPeriod = 2;
    my $maxPeriod = 25;
    my $message;
    my @messageChars;
    my $text;
    
    my $argc = $#ARGV + 1;
    
    if ($argc < 2) {
    	print "Usage: MessageFinder [Text to search in] [Message to search for]\n";
    	exit();
    }
    
    my $fileName = $ARGV[0];
    
    # pack all message arguments into one word
    $message = $ARGV[1];
    for (my $i = 2; $i < $argc; $i++) {
    	$message = $message . $ARGV[$i];
    }
    chomp($message);
    if ($debug) {
    	print("message = $message\n");
    }
    
    # turn string into array of characters
    for (my $i = 0; $i < length($message); $i++)
    {
    	push(@messageChars, substr($message, $i, 1));
    }
    if ($debug) {
    	print "messageChars = @messageChars\n";
    }
    
    # read in file to a single string and remove whitespace and punctuation
    open (FILE, $fileName) or die ("Error opening $fileName\n");
    $/ = undef;
    $text = <FILE>;
    close (FILE);
    
    $text =~ s/[\s]+//g; 	   # get rid of whitespace
    $text =~ s/[^a-zA-Z0-9]+//g;  # get rid of punctuation
    
    if ($debug) {
    	print "text = $text\n";
    }
    
    for (my $period = $minPeriod; $period <= $maxPeriod; $period++)
    {
    	my $pattern = "";
    	my $tweenChars = $period - 1;
    	# create regex
    	for (my $i = 0; $i <= $#messageChars; $i++)
    	{	
    		# we don't want an extra quantifier after the last letter
    		if ($i == $#messageChars) {
    			$pattern = $pattern."$messageChars[$i]";
    		}
    		else {
    			$pattern = $pattern."$messageChars[$i].{$tweenChars}";
    		}
    	}
    	if ($debug) {
    		#print "pattern = $pattern\n";
    	}
    	# scan text for match
    	if ($text =~ qr/$pattern/i) {
    		print "Found message at period $period!\n";
    	}
    }
    
    # this I wrote to test regular expressions until I found how to form one that did what I want
    if ($debug) {
    	my $testtext = "eDenseereverlioncLaw";
    	my $testpattern = "d.{3}e.{3}v.{3}i.{3}l";
    	if ($testtext =~ qr/$testpattern/i) {
    		print "in hardcoded test: $testtext matches $testpattern\n";
    	}
    }
    

    I uncommented that line, and now that I use pattern variables as above I don't get warnings. (Though my understanding of how to work with regular expressions in Perl is still not concrete, so I'm not 100% sure of anything I did with them in that script. There are probably other mistakes since I suck at Perl, and I still don't know why the searches were case-insensitive even before I added some 'i's here and there.)

    LoneIgadzra on
  • Options
    JasconiusJasconius sword criminal mad onlineRegistered User regular
    edited November 2007
    I guess I can ask you crazy people since one of you has probably touched PHP. I am currently taking a class that touches on a lot of PHP. A couple of nights ago the professor and I were trying to figure out the object oriented features of PHP5, since neither of us had touched them.

    We can't figure out a few things, and the php.net documentation is terrible.

    1) Is it possible to have private variables within a class? We tried, couldn't pull it off.

    2) Is there some unwritten way to share classes with the rest of your application outside of just including them?

    3) When you create a class constructor, how do you set them up to take arguments, I just couldn't find the exact syntax.

    I'm just completely hazy on things regarding scope in PHP, and like I said, php.net documentation leaves something to be desired.

    Jasconius on
  • Options
    JaninJanin Registered User regular
    edited November 2007
    Jasconius wrote: »
    I guess I can ask you crazy people since one of you has probably touched PHP. I am currently taking a class that touches on a lot of PHP. A couple of nights ago the professor and I were trying to figure out the object oriented features of PHP5, since neither of us had touched them.

    We can't figure out a few things, and the php.net documentation is terrible.

    1) Is it possible to have private variables within a class? We tried, couldn't pull it off.

    2) Is there some unwritten way to share classes with the rest of your application outside of just including them?

    3) When you create a class constructor, how do you set them up to take arguments, I just couldn't find the exact syntax.

    I'm just completely hazy on things regarding scope in PHP, and like I said, php.net documentation leaves something to be desired.
    1. Put "private" before the variable declarations (http://us2.php.net/manual/en/language.oop5.visibility.php).
    2. No
    3. Define a __construct function (http://us2.php.net/manual/en/language.oop5.decon.php).

    More documentation on PHP5 classes at http://us2.php.net/zend-engine-2.php

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    Typhus733Typhus733 Yip! Registered User regular
    edited November 2007
    Odd question, a friend/classmate of mine wants to "disable" certain keys in a C++ program. He wants to disable the alt, ctrl, and windows key within a program and I have no clue how that would work and am kinda interested myself now. Any possible way to do this?

    Typhus733 on
  • Options
    LewishamLewisham Registered User regular
    edited November 2007
    #!/usr/bin/env perl
    
    use strict;
    #use warnings; # pops completely unexplained warnings about my pattern variables
    
    my $debug = 0;
    my $minPeriod = 2;
    my $maxPeriod = 25;
    my $message;
    my @messageChars;
    my $text;
    
    my $argc = $#ARGV + 1;
    
    if ($argc < 2) {
    	print "Usage: MessageFinder [Text to search in] [Message to search for]\n";
    	exit();
    }
    
    my $fileName = $ARGV[0];
    
    # pack all message arguments into one word
    $message = $ARGV[1];
    for (my $i = 2; $i < $argc; $i++) {
    	$message = $message . $ARGV[$i];
    }
    chomp($message);
    if ($debug) {
    	print("message = $message\n");
    }
    
    # turn string into array of characters
    for (my $i = 0; $i < length($message); $i++)
    {
    	push(@messageChars, substr($message, $i, 1));
    }
    if ($debug) {
    	print "messageChars = @messageChars\n";
    }
    
    # read in file to a single string and remove whitespace and punctuation
    open (FILE, $fileName) or die ("Error opening $fileName\n");
    $/ = undef;
    $text = <FILE>;
    close (FILE);
    
    $text =~ s/[\s]+//g; 	   # get rid of whitespace
    $text =~ s/[^a-zA-Z0-9]+//g;  # get rid of punctuation
    
    if ($debug) {
    	print "text = $text\n";
    }
    
    for (my $period = $minPeriod; $period <= $maxPeriod; $period++)
    {
    	my $pattern = "";
    	my $tweenChars = $period - 1;
    	# create regex
    	for (my $i = 0; $i <= $#messageChars; $i++)
    	{	
    		# we don't want an extra quantifier after the last letter
    		if ($i == $#messageChars) {
    			$pattern = $pattern."$messageChars[$i]";
    		}
    		else {
    			$pattern = $pattern."$messageChars[$i].{$tweenChars}";
    		}
    	}
    	if ($debug) {
    		#print "pattern = $pattern\n";
    	}
    	# scan text for match
    	if ($text =~ qr/$pattern/i) {
    		print "Found message at period $period!\n";
    	}
    }
    
    # this I wrote to test regular expressions until I found how to form one that did what I want
    if ($debug) {
    	my $testtext = "eDenseereverlioncLaw";
    	my $testpattern = "d.{3}e.{3}v.{3}i.{3}l";
    	if ($testtext =~ qr/$testpattern/i) {
    		print "in hardcoded test: $testtext matches $testpattern\n";
    	}
    }
    

    I uncommented that line, and now that I use pattern variables as above I don't get warnings. (Though my understanding of how to work with regular expressions in Perl is still not concrete, so I'm not 100% sure of anything I did with them in that script. There are probably other mistakes since I suck at Perl, and I still don't know why the searches were case-insensitive even before I added some 'i's here and there.)

    Put the line back in, for goodness sakes. You don't get any warnings because you suppressed them. Note I use the word suppress rather than fix, just because it doesn't complain about the bugs doesn't mean they aren't there. What is the warning? What you are doing seems OK, but I'm not going to sit here and debug the script. This is for a coursework assignment, right? If you get warnings, you will be marked down.

    Start using foreach rather than for, it's a simpler syntax and guarantees you can't do anything silly and overrun the array.

    Lewisham on
  • Options
    SmasherSmasher Starting to get dizzy Registered User regular
    edited November 2007
    Typhus733 wrote: »
    Odd question, a friend/classmate of mine wants to "disable" certain keys in a C++ program. He wants to disable the alt, ctrl, and windows key within a program and I have no clue how that would work and am kinda interested myself now. Any possible way to do this?

    So he wants to make it impossible to alt tab out or force quit? That doesn't seem like a very good idea.

    Smasher on
  • Options
    Typhus733Typhus733 Yip! Registered User regular
    edited November 2007
    I know but it isn't my problem what he does with it and what happens as a consequence(I'm such a great friend, I know).

    Typhus733 on
  • Options
    SmasherSmasher Starting to get dizzy Registered User regular
    edited November 2007
    I don't know much about Windows programming, but I suspect it's not possible. The operating system likely handles certain key combinations such as those before handing input over to the application, precisely so the user can always force quit if necessary.

    At least I hope that's the case.

    Smasher on
  • Options
    JaninJanin Registered User regular
    edited November 2007
    Typhus733 wrote: »
    Odd question, a friend/classmate of mine wants to "disable" certain keys in a C++ program. He wants to disable the alt, ctrl, and windows key within a program and I have no clue how that would work and am kinda interested myself now. Any possible way to do this?

    If it's in a command-line program and you just don't want them to display, you can use a library like readline or ncurses to do fancy terminal manipulation. I wouldn't advise doing it without a library, because terminal interfacing is crazy.

    If he wants to do something like disable alt-tab, your friend is a dick. Luckily, there is no way (that I know of) to do this.

    Janin on
    [SIGPIC][/SIGPIC]
  • Options
    LoneIgadzraLoneIgadzra Registered User regular
    edited November 2007
    Lewisham wrote: »
    #!/usr/bin/env perl
    
    use strict;
    #use warnings; # pops completely unexplained warnings about my pattern variables
    
    my $debug = 0;
    my $minPeriod = 2;
    my $maxPeriod = 25;
    my $message;
    my @messageChars;
    my $text;
    
    my $argc = $#ARGV + 1;
    
    if ($argc < 2) {
    	print "Usage: MessageFinder [Text to search in] [Message to search for]\n";
    	exit();
    }
    
    my $fileName = $ARGV[0];
    
    # pack all message arguments into one word
    $message = $ARGV[1];
    for (my $i = 2; $i < $argc; $i++) {
    	$message = $message . $ARGV[$i];
    }
    chomp($message);
    if ($debug) {
    	print("message = $message\n");
    }
    
    # turn string into array of characters
    for (my $i = 0; $i < length($message); $i++)
    {
    	push(@messageChars, substr($message, $i, 1));
    }
    if ($debug) {
    	print "messageChars = @messageChars\n";
    }
    
    # read in file to a single string and remove whitespace and punctuation
    open (FILE, $fileName) or die ("Error opening $fileName\n");
    $/ = undef;
    $text = <FILE>;
    close (FILE);
    
    $text =~ s/[\s]+//g; 	   # get rid of whitespace
    $text =~ s/[^a-zA-Z0-9]+//g;  # get rid of punctuation
    
    if ($debug) {
    	print "text = $text\n";
    }
    
    for (my $period = $minPeriod; $period <= $maxPeriod; $period++)
    {
    	my $pattern = "";
    	my $tweenChars = $period - 1;
    	# create regex
    	for (my $i = 0; $i <= $#messageChars; $i++)
    	{	
    		# we don't want an extra quantifier after the last letter
    		if ($i == $#messageChars) {
    			$pattern = $pattern."$messageChars[$i]";
    		}
    		else {
    			$pattern = $pattern."$messageChars[$i].{$tweenChars}";
    		}
    	}
    	if ($debug) {
    		#print "pattern = $pattern\n";
    	}
    	# scan text for match
    	if ($text =~ qr/$pattern/i) {
    		print "Found message at period $period!\n";
    	}
    }
    
    # this I wrote to test regular expressions until I found how to form one that did what I want
    if ($debug) {
    	my $testtext = "eDenseereverlioncLaw";
    	my $testpattern = "d.{3}e.{3}v.{3}i.{3}l";
    	if ($testtext =~ qr/$testpattern/i) {
    		print "in hardcoded test: $testtext matches $testpattern\n";
    	}
    }
    

    I uncommented that line, and now that I use pattern variables as above I don't get warnings. (Though my understanding of how to work with regular expressions in Perl is still not concrete, so I'm not 100% sure of anything I did with them in that script. There are probably other mistakes since I suck at Perl, and I still don't know why the searches were case-insensitive even before I added some 'i's here and there.)

    Put the line back in, for goodness sakes. You don't get any warnings because you suppressed them. Note I use the word suppress rather than fix, just because it doesn't complain about the bugs doesn't mean they aren't there. What is the warning? What you are doing seems OK, but I'm not going to sit here and debug the script. This is for a coursework assignment, right? If you get warnings, you will be marked down.

    Start using foreach rather than for, it's a simpler syntax and guarantees you can't do anything silly and overrun the array.

    It was a joke. I'm glad you laughed earlier because that was what I was going for. The script didn't work while that warning was popping up. I fixed it. It now pops no warnings with warnings enabled. I paid careful attention to the grading criteria when finishing this script, which in this case was that it work.

    I also know about foreach, but it's hard to change habits, and I was in a huge hurry with this. I just noticed that the "foreach" syntax wasn't the same as Python, and decided not to spend 30 seconds looking it up when I could write a C-style for loop in that time (which, if I'm not mistaken, is fairly safe here anyway since I use the array sizes as a conditional check).

    Edit: Okay, looked up the syntax and replaced a few loops for posterity. But something like this I don't even know how I'd do with foreach:
    # turn string into array of characters
    for (my $i = 0; $i < length($message); $i++)
    {
    	push(@messageChars, substr($message, $i, 1));
    }
    

    And this I wouldn't want to do with foreach:
    for (my $period = $minPeriod; $period <= $maxPeriod; $period++)
    

    LoneIgadzra on
  • Options
    Typhus733Typhus733 Yip! Registered User regular
    edited November 2007
    Well sucks for him, either way thanks for the answer.

    Typhus733 on
  • Options
    AndorienAndorien Registered User regular
    edited November 2007
    Janin wrote: »
    Typhus733 wrote: »
    Odd question, a friend/classmate of mine wants to "disable" certain keys in a C++ program. He wants to disable the alt, ctrl, and windows key within a program and I have no clue how that would work and am kinda interested myself now. Any possible way to do this?

    If it's in a command-line program and you just don't want them to display, you can use a library like readline or ncurses to do fancy terminal manipulation. I wouldn't advise doing it without a library, because terminal interfacing is crazy.

    If he wants to do something like disable alt-tab, your friend is a dick. Luckily, there is no way (that I know of) to do this.

    There IS a way I believe. IIRC, if you simply don't have the Win32 message pump, it can't receive the alt-tab message, along with any other message. This, of course, is retarded and will prevent your program from doing a lot of stuff.

    If he wants to disable them just in his program, he could simply ignore their input. If he wants to disable their general use in the entire system while the program is running, then yes, he's a dick, and you shouldn't tell him how even if you find out. Let him find out how to be an ass on his own time.

    Andorien on
  • Options
    LewishamLewisham Registered User regular
    edited November 2007
    But something like this I don't even know how I'd do with foreach:
    # turn string into array of characters
    for (my $i = 0; $i < length($message); $i++)
    {
    	push(@messageChars, substr($message, $i, 1));
    }
    

    And this I wouldn't want to do with foreach:
    for (my $period = $minPeriod; $period <= $maxPeriod; $period++)
    

    Oh, fair enough. I had forgotten that Perl doesn't treat strings as arrays. My bad :x

    But do remember them! They're good ;)

    Lewisham on
This discussion has been closed.