• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

$file =~ /(\w+)\-(\w+)\.(\w+)/; <--This is why PERL makes god cry.

Status
Not open for further replies.

Nerevar

they call me "Man Gravy".
wait, you're staying I'm going to have to actually use that regexp crap I learned in school?

Oh God, I'm not gonna last five minutes on my job.
 

Bog

Junior Ace
Knowing regular expressions made me look like a programming genius at my first internship.
 

maharg

idspispopd
Perl:
($@,$?)=(@ARGV,9,9);
$: = 1 - $? * $@;
print "# ", '##' x ($@-2), "# #\n";
$_[0][$_] = $_[$? + 1][$_] = 8 for (0 .. $@);
$_[$_][0] = $_[$_][$@ + 1] = 8 for (0 .. $?);
$/ = 1 + rand $?;
$. = 1 + rand $@;
@@ = [$., $/];
$_[$/][$.] = $^ = 4;
while ($:) {
    $" = ($^-- > 3) ? rand 4
                    : ++$" % 4;
    (1 > $") ? $.-- 
             : ((2 > $") ? $/--
                         : (2 ^ $") ? ++$/
                                : $.++);
    if ($_[$/][$.] && 1) {
        pop @@, $^ = 4 if (!$^);
        ($., $/) = @{ $@[-1] };
        next;
    }
    $_[$/][$.] = $^ = 4;
    ($" < 2) ? $_[$/][$.] |= 1 << $"
             : ($_[$@[-1][1]][$@[-1][0]] |= 1 << $" - 2);
    push @@, [$., $/];
    ++$:
}
print "#", map( { ($_ % 2) ? '  '
                           : ' #'
                } @$_[1..$@]),
           "\n#",
           map( { (2 & $_) ? ' #'
                           : '##'
                } @$_[1..$@]),
           "\n" for (@_[1 .. $?]);
 

Ferrio

Banned
I swear, Perl's a neat language and all. But once you write the code hope you never have to alter it again. Or even worse, someone elses.
 

explodet

Member
Yeah, I did this all the time at my job - when I hit regex, my mind went BLEAH for an hour or so trying to figure it out, then I did and wondered how I could be so dim not seeing it in the first place.
 

aoi tsuki

Member
Having never looked at much Perl, i've gotta say that it's cryptic nature makes it look extremely cool. If i weren't for the fact that i'm reading so many books right now, i'd pick up one on Perl just to try and understand it.
 

emerge

Member
Code:
* factor an arbitrarily large positive integer
*
* Copyright (C) 1999 by Brian Raiter
* under the GNU General Public License

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>-
<<<<<<<<<+
[-[>>>>>>>>>>][-]<<<<<<<<<<[[->>>>>>>>>>+<<<<<<<<<<]<<<<<<<<<<]
  >>>>>>>>>>,----------]
>>>>>>>>>>[------------------------------------->>>>>>>>>->]
<[+>[>>>>>>>>>+>]<-<<<<<<<<<<]-
[>++++++++++++++++++++++++++++++++++++++++++++++++.
  ------------------------------------------------<<<<<<<<<<<]
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.
--------------------------.[-]
>>>>>>>>>>>>++<<<<+
[ [-]>>
  [>>>>[-]>[-]>[-]>[-]
    >[-]>[-]
    <<<<<<<[->>>+>+<<<<]>>>>>>>>]
  <<<<<<<<<<[>>>>>>[-<<<<+>>>>]<<<<<<<<<<<<<<<<]>>>>>>>>>>
  [>[->>>+>>+<<<<<]>>>>>>>>>]
  <<<<<<<<<<[>>>>>>[-<<<<<+>>>>>]<<<<<<<<<<<<<<<<]>>>>>>>>>>
  [>>>[-]>>>[-]>[-]>>>] 
  <<<<<<<<<<[<<<<<<<<<<]
  >>>>>>>>>[-]>>>>>>>+<<<<<<<<[+]+
  [ ->> 
    [>>>>>>[->++<]>>>>]<<<<<<<<<<
    [>>>>>>>>[-]>[-]
       <<<<[->>>++<<<]<<<<<<<<<<<<<<<]>>>>>>>>>>
    [>>>>>>>>[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<
            [->--------->>>>>>>>>+<<<<<<<<<<[->+<]]]]]]]]]]]>>]
<<<<<<<<<<[>>>>>>>>>[-<+<<<+>>>>]<<<<<<<<<<<<<<<<<<<]>>>>>>>>>>
    [>>>>>>>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>[-<+>
            [-<--------->>>>>>>>>>>+<<<<<<<<<<[-<+>]]]]]]]]]]]>>>]
    <<<<<<<<<<
    [>>>>[->>>+>>+<<<<<]<<<<<<<<<<<<<<]
    >>>>>>>>>>[>>>>>>>[-<<<+>>>]>>>]<<<<<<<<<<
    [>>>>>>>>[->-<]>
      [<<<<<<<<<[<[-]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<<<<<<<<<<]
        >>>>>>>>>>>>>>>>>>>]
      <<<<<<<<<<<<<<<<<<<]
    >>>>>>>>>[+[+[+[+[+[+[+[+[+[+[[-]<+>]]]]]]]]]]]<
  ]
  >>>>>>>>
  [                                   
    <<<<<<
    [>>>>>>>>[-]>[-]<<<<<[->>>+>+<<<<]>>>>>>]<<<<<<<<<<
    [>>>>>>>>[-<<<<+>>>>]<<<[->>>+>+<<<<]<<<<<<<<<<<<<<<]>>>>>>>>>>
    [>>>>>>>>>[-<<<<+>>>>]>]<<<<<<<<<<
    [>>>>>>>>[-<->]<<<<<<<<<<<<<<<<<<]>>>>>>>>>>
    [>>>>>>>[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<[->+<
            [++++++++++[+>-<]>>>>>>>>>>-<<<<<<<<<<]]]]]]]]]]]>>>]
    >>>>>>>+
    [  
      [-]<<<<<<<<<<<<<<<<< 
[>>>>[-]>>>>[-<<<<+>>>>]<<[->>+<<]<<<<<<<<<<<<<<<<]>>>>>>>>>>
      [>>>>>>>>[->+<<<+>>]>>]<<<<<<<<<<
      [>>>[->>>>>>+<<<<<<]<<<<<<<<<<<<<]>>>>>>>>>>
      [>>>>>>>>>[-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
                [-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
                [-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
                [-<<<<<<+>>>>>>[-<<<<<<+>>>>>>
[-<<<<<<+>>>>>>[-<<<<<<--------->>>>>>>>>>>>>>>>+<<<<<<<<<<
                [-<<<<<<+>>>>>>]]]]]]]]]]]>]
      >>>>>>>
    ]                               
    <<<<<<<<<<<<<<<<<[<<<<<<<<<<]>>>>>>>>>>
    [>>>>>>>>[-]<<[->+<]<[->>>+<<<]>>>>>]<<<<<<<<<<
    [+>>>>>>>[-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
             [-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
             [-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
             [-<<<<<<<+>>>>>>>[-<<<<<<<->>>>>>+>
             [-<<<<<<<+>>>>>>>]]]]]]]]]<<<<<<<
             [->>>>>>>+<<<<<<<]-<<<<<<<<<<]
    >>>>>>>
    [-<<<<<<<<<<<+>>>>>>>>>>>]
      >>>[>>>>>>>[-<<<<<<<<<<<+++++>>>>>>>>>>>]>>>]<<<<<<<<<<
    [+>>>>>>>>[-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
              [-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
              [-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
              [-<<<<<<<<+>>>>>>>>[-<<<<<<<<->>>>>+>>>
              [-<<<<<<<<+>>>>>>>>]]]]]]]]]<<<<<<<<
              [->>>>>>>>+<<<<<<<<]-<<<<<<<<<<]
    >>>>>>>>[-<<<<<<<<<<<<<+>>>>>>>>>>>>>]>>
    [>>>>>>>>[-<<<<<<<<<<<<<+++++>>>>>>>>>>>>>]>>]<<<<<<<<<<
    [<<<<<<<<<<]>>>>>>>>>>
    >>>>>>
  ]
  <<<<<<
  [>>>[->>>>+>+<<<<<]>>>>>>>]
  <<<<<<<<<<
  [>>>>>>>[-<<<<+>>>>]<<<<<[->>>>>+>>+<<<<<<<]<<<<<<<<<<<<]
  >>>>>>>>>>[>>>>>>>[-<<<<<+>>>>>]>>>]<<<<<<<<<<
  [>>>>>>>>>[-<->]<
    [<<<<<<<<
      [<<[-]>>>>>>>>>>[-<<<<<<<<<<+>>>>>>>>>>]<<<<<<<<<<<<<<<<<<]
    >>>>>>>>>>>>>>>>>>]<<<<<<<<<<<<<<<<<<]
  >>>>>>>>[>-<[+[+[+[+[+[+[+[+[+[[-]>+<]]]]]]]]]]]>+
  [ [-]
    <[-]+>>>>+>>>>>>>>[>>>>>>>>>>]<<<<<<<<<<
    [<<<<<<[<<<<[<<<<<<<<<<]>>>>+<<<<<<<<<<]<<<<]
    >>>>>>>>>>>>>>>>>>>>[>>>>>>>>>>]<<<<<<<<<<[<<<<<<<<<<]
    >>>>-
    [ [+]
      >>>>>>>>-<<[>[-]>>[-<<+>>]>>>>>>>]<<<<<<<<<<
      [+>>[>>>>>>>>+>>]<<-<<<<<<<<<<]-
      [>>++++++++++++++++++++++++++++++++++++++++++++++++.
         ------------------------------------------------<<<<<<<<<<<<]
      ++++++++++++++++++++++++++++++++.[-]>>>>
    ]
    >>>>>>
    [>>[->>>>>+<<<<<[->>>>>+<<<<<
       [->>>>>+<<<<<[->>>>>+<<<<<
       [->>>>>+<<<<<[->>>>>+<<<<<
       [->>>>>+<<<<<[->>>>>+<<<<<
       [->>>>>+<<<<<[->>>>>--------->>>>>+<<<<<<<<<<
       [->>>>>+<<<<<]]]]]]]]]]]>>>>>>>>]
    <<<<<<<<<<[>>>>>>>[-<<<<<+>>>>>]<<<<<<<<<<<<<<<<<]
    >>>>>>>>>
  ]<
]>>
[>>>>>>>>>>]<<<<<<<<<<[+>[>>>>>>>>>+>]<-<<<<<<<<<<]-
[>++++++++++++++++++++++++++++++++++++++++++++++++.<<<<<<<<<<<]
++++++++++.

Brainfuck syntax highlighting needs to be implemented asap :) http://www.muppetlabs.com/~breadbox/bf/
 

iapetus

Scary Euro Man
Someone had to bring Brainfuck up, didn't they? Well, I'm afraid I'm about to win the battle of "most ridiculous programming language syntax".

http://compsoc.dur.ac.uk/whitespace/

What is Whitespace?

Most modern programming languages do not consider white space characters (spaces, tabs and newlines) syntax, ignoring them, as if they weren't there. We consider this to be a gross injustice to these perfectly friendly members of the character set. Should they be ignored, just because they are invisible? Whitespace is a language that seeks to redress the balance. Any non whitespace characters are ignored; only spaces, tabs and newlines are considered syntax.
What are the advantages of Whitespace?

Some things which are difficult in other languages are made much easier in Whitespace. For example, literate programming is simply a matter of writing your helpful comments in between program instructions. It's also easy to encrypt your programs. Simply write a misleading comment!

Whitespace is a particularly useful language for spies. Imagine you have a top secret program that you don't want anyone to see. What do you do? Simply print it out and delete the file, ready to type in at a later date. Nobody will know that your blank piece of paper is actually vital computer code!

What does a typical Whitespace program look like?

Below is an extract from a program which asks for a name then outputs it (see here for the full script.

Towers of Hanoi solver.

Can we get Whitespace syntax highlighting too? :)
 

SFA_AOK

Member
What is it they call Perl? Write once, read never? Because once you've written it, you've no chance of ever understanding what it does ever again.
 

Chrono

Banned
oh dammit what the hell...

I have a (beginners') java project due in 5 hours and I still have got nothing. I though I'd put this here since this thread is kinda about programming. :p If anyone feels like helping this is a beginner's course so the program is all in one class with only one main method... or somethin. :s

A polynomial in x of at most degree 2 is given by ax^2 + bx + c

Write a Java program that reads in values of a, b, and c and finds the roots of the polynomial

ax^2 + bx + c

The roots are real or complex numbers that solve the equation

ax^2 + bx + c = 0

When both a = 0 and b = 0, we consider the case "extremely degenerate" and leave it at that. When a = 0 and b != 0, we consider the case "degenerate." In this case the equation reduces to bx + c = 0 and has one root given by

x = - c/b. When a != 0 (the general case), the roots are given by

root1 = (1/2a) (-b + (b^2 - 4 a c)^1/2)
root1 = (1/2a) (-b - (b^2 - 4 a c)^1/2)

the expression under the square root sign is the discriminant. If the disc. is postive, then two real roots exist. If the disc. is zero, then the two roots are real and equal. In the case we say that the polynomial has multiple roots. If the disc. is negative, then the roots are complex. For each set of values, a, b, and c, your program should print the computed root (s) along with one of the following messages:

degenerate
extremely degenerate
multiple real roots
two real roots
two complex roots


For example, if the values 1, 2, and 3 are read in for a, b, and c, respectively, then

Two complex roots: root1 = -1.00 + i * 1.41
root2 = -1.00 - i * 1.41

Should be printed.

and that's the whole page... I'm gonna go now and actually write this before 9 AM.
 

Ferrio

Banned
Should be easy. Just need some if statements evaluating a,b,c to find out whether its extreme degenerate, regular degenerate, or has multiple roots. Then go on from there... Oh lemme just write some pseudocode, I don't know java but this should translate pretty easily.



if ( a == 0 && b==0) {
Its extreme degenerate. Quit program, or continue logic with else
}

if (a == 0) {
Degenerate
root1 = -c/b;
}
else {
if ( (b^2 - 4 a c) > 0 ) {
two real roots
root1 = (1/2a) (-b + (b^2 - 4 a c)^1/2);
root2 = (1/2a) (-b - (b^2 - 4 a c)^1/2) ;
}
else {
if ( (b^2 - 4 a c) < 0 ) {
complex roots,
root1 = whatever;
root2 = whatever;
}
else {
Multiple real roots
root1 = root2 = (1/2a) (-b + (b^2 - 4 a c)^1/2) ;
}
}
}

Ugly but whatever.
 

Chrono

Banned
heh, that does look easy. We took swtich statements in the class I missed just before this project was given so I thought I must use those... guess not. :)
 

Ferrio

Banned
Switch statements aren't used very much. Good use for them is menuing systems.

Switchs are just so you don't have to do this crap


if (a == 1)
else
if (a == 2)
else
if (a == 3)
else
if (a == 4)
....so on and so forth.
 

Baron Aloha

A Shining Example
Perl isn't a programming language. It's a scripting languange and its easy.

Many scripts have some weird characters in them but it's easy once you learn what they mean.

And regular expressions are a joke.

Edit: Upon further reflection I realized that my above comments make me look like a bit of a douche (I know what some of you are thinking... "so what else is new?" ^_^ )

Anyway, the real point I was trying to make is that you should spend some time learning the regular expressions since they are really the key to Perl (I'd say 75% of learning Perl is learning regular expressions and the other 25% is learning the functions). Take a few days to really learn those and I promise that things will suddenly get a lot easier.
 

iapetus

Scary Euro Man
Ferrio said:
Switch statements aren't used very much. Good use for them is menuing systems.

There is no good use for switch statements. They're a pustulent sore on the arse of C-style syntax at best.

Actually, I take that back. They're not bad for practical exercises on how to debug badly written code. :p
 

DaCocoBrova

Finally bought a new PSP, but then pushed the demon onto someone else. Jesus.
I just turned down a programming job. They offered to train me and everything (AMX/Crestron)... No regrets.

None. At. All.
 

Chrono

Banned
Did I screw this one up completely? I finished the code an hour and half ago and realize there are some things I can change, but for the past hour I've been doing nothing but looking at teh compilor erros: just 2. each teh same: it says ' ) ' expected at lines 78 and 131.... but I don't see anything wrong.. =\

I'm guessing the whole thing is wrong and I should drop this class. too late now though :(

Here's the code of anyone is feeling generous this morning:

lines 78 and 131 are in bold..

Code:
import javax.swing.*;


public class PolynomialRoots {


	public static void main(String[] args) {


// declare variables


		String a_input;
		String b_input;
		String c_input;
		double a;
		double b;
		double c;
		double root1;
		double root2;



// use JOptionPane object for input and then converting the strings to doubles



String a_input = JOptionPane.showInputDialog(null, "Enter the value of a", "Project 7" , JOptionPane.QUESTION_MESSAGE);


a = Double.parseDouble(a_input);


String b_input = JOptionPane.showInputDialog(null, "Enter the value of b", "Project 7" , JOptionPane.QUESTION_MESSAGE);


b = Double.parseDouble(b_input);


String c_input = JOptionPane.showInputDialog(null, "Enter the value of c", "Project 7" , JOptionPane.QUESTION_MESSAGE);


c = Double.parseDouble(c_input);




// using the control statement if.. else to determine if there are any roots, and if so what type



if   ( a == 0 && b == 0 )

System.out.println("Case is Extremely Degenerate");

else

{

if   ( a == 0 && b != 0 ) {

	root1 =  - c/b;

	System.out.printlin("There is only one root in this degenerate case, and it is:" + root1);	}


else

 {

[b] if   ( ( b^2 - 4 a c ) > 0 ) { [/b]

	root1 = (1/2a) (-b + (b^2 - 4 a c)^1/2);

	root2 = (1/2a) (-b - (b^2 - 4 a c)^1/2);


	System.out.println("Two real roots:" + "root1:" root1 + "root2:" + root2); }


else

  {

if   ( ( b^2 - 4 a c) == 0) {


	root1 = (1/2a) (-b + (b^2 - 4 a c)^1/2);

	root2 = (1/2a) (-b - (b^2 - 4 a c)^1/2);


	System.out.prinln("multiple real roots:" + "root1:" + root1 + "root2:" + root2);


	}

else

    {

if    ( ( b^2 - 4 a c) < 0) {


	root1 = (1/2a) (-b + (b^2 - 4 a c)^1/2);

	root2 = (1/2a) (-b - (b^2 - 4 a c)^1/2);


	System.out.println("Two complex roots:" + "root 1:" + root1 + "root2:" + root2);

	}

    }

   }




// exiting and ending the program


[b]System.exit(0);[/b]

  }


  }

 }

}
 
What's wrong with PERL regex? I think .NET's System.Text.RegularExpression namespace is a little bit snarlier, just because it's less concise.

Like someone pointed out, your problem isn't with PERL, it's with sed, which is where Larry and Co got the regex notation.
 

iapetus

Scary Euro Man
Chrono said:
if ( ( b^2 - 4 a c ) > 0 )

If you're wanting to multiply 4 by a by c (which you are) then you have to use the multiplication operator. So, for example, to fix this piece of code:

Code:
if ((b^2 - 4 * a * c) > 0)

There are a number of places where you've done the same thing.
 

CrunchyB

Member
iapetus said:
There is no good use for switch statements.

You're treading on thin ice here :)
A switch is evaluated only once, I'd image it could be more efficient than many sequential if statements. I realise that there is no assembler equivalent for a switch in most (all?) architectures, but with languages other than C/C++ there could be some benefits.


Back to perl, the infamous obfuscated DeCSS script:

Perl:
#!/usr/bin/perl
# 472-byte qrpff, Keith Winstein and Marc Horowitz <sipb-iap-dvd@mit.edu>
# MPEG 2 PS VOB file -> descrambled output on stdout.
# usage: perl -I <k1>:<k2>:<k3>:<k4>:<k5> qrpff
# where k1..k5 are the title key bytes in least to most-significant order

s''$/=\2048;while(<>){G=29;R=142;if((@a=unqT="C*",_)[20]&48){D=89;_=unqb24,qT,@b=map{ordqB8,unqb8,qT,_^$a[--D]}@INC;s/...$/1$&/;Q=unqV,qb25,_;H=73;O=$b[4]<<9|256|$b[3];Q=Q>>8^(P=(E=255)&(Q>>12^Q>>4^Q/8^Q))<<17,O=O>>8^(E&(F=(S=O>>14&7^O)^S*8^S<<6))<<9,_=(map{U=_%16orE^=R^=110&(S=(unqT,"\xb\ntd\xbz\x14d")[_/16%8]);E^=(72,@z=(64,72,G^=12*(U-2?0:S&17)),H^=_%64?12:0,@z)[_%8]}(16..271))[_]^((D>>=8)+=P+(~F&E))for@a[128..$#a]}print+qT,@a}';s/[D-HO-U_]/\$$&/g;s/q/pack+/g;eval
 

iapetus

Scary Euro Man
CrunchyB said:
A switch is evaluated only once, I'd image it could be more efficient than many sequential if statements.

And indeed, in terms of instructions generated it can be (though in theory a good compiler should generate the same code). The minute performance improvement is more than offset by the tendency of switch statements to be the source of coding errors IME, though.
 
Perl's useless for CGI these days, but no way in hell will it be anything BUT a vital tool for system administration and file/formatting scripts.
 
Status
Not open for further replies.
Top Bottom