- #1
- 19,557
- 10,338
Physics Forums now supports GeSHi syntax highlighting for nearly 200 programming languages. Simply hit the </> button on the editor. Select the code option, use general code, type in your code. Then in the first bbcode tag put in the language you want to use. For example:
[code=perl]
[code=ruby]
[code=java]
[code=javascript]
Enjoy :)
[code=perl]
Perl:
use strict;
# Read some lines, concat them together (separated by a space), and print them.
# How exciting. Stops when it reaches the indicated count, the
# end of file, or the line STOP!
# Read the number.
print "How many lines? ";
my $num = <STDIN>;
$num > 0 or die "Num must be positive. You entered $num";
# Read in the strings.
my $accum = "";
my $sep = "";
while(my $line = <STDIN>) {
chomp $line;
if($line eq "STOP!") { last; }
$accum = "$accum$sep$line";
if(--$num == 0) { last; }
$sep = " ";
}
# Print what we got.
print "$accum\n";
[code=ruby]
Ruby:
# Double-quoted strings can substitute variables.
a = 17
print "a = #{a}\n";
print 'a = #{a}\n';
print "\n";
# If you're verbose, you can create a multi-line string like this.
b = <<ENDER
This is a longer string,
perhaps some instructions or agreement
goes here. By the way,
a = #{a}.
ENDER
print "\n[[[" + b + "]]]\n";
print "\nActually, any string
can span lines. The line\nbreaks just become
part of the string.
"
print %Q=\nThe highly intuitive "%Q" prefix allows alternative delimiters.\n=
print %Q[Bracket symbols match their mates, not themselves.\n]
[code=java]
Java:
//sample code to write 100 random ints to a file, 1 per line
import java.io.PrintStream;
import java.io.IOException;
import java.io.File;
import java.util.Random;
public class WriteToFile
{ public static void main(String[] args)
{ try
{ PrintStream writer = new PrintStream( new File("randInts.txt"));
Random r = new Random();
final int LIMIT = 100;
for(int i = 0; i < LIMIT; i++)
{ writer.println( r.nextInt() );
}
writer.close();
}
catch(IOException e)
{ System.out.println("An error occurred while trying to write to the file");
}
}
}
[code=javascript]
JavaScript:
function calculate(fld, price) {
var dir = fld.name.[B]charAt[/B](1);
var num = fld.name.[B]charAt[/B](2);
var quant = fld.[B]options[/B][fld.[B]selectedIndex[/B]].[B]value[/B];
var subtotal = [B]eval[/B](quant * price);
var total = 0;
[B]eval[/B]('document.order.t' + dir + num).value = [B]fix[/B](subtotal);
for (i = 1; i < 8; i++) {
var itemTotal = [B]eval[/B]('document.order.t' + dir + i).value;
if ([B]parseFloat[/B](itemTotal) > 0) total += [B]parseFloat[/B](itemTotal);
}
[B]eval[/B]('document.order.total' + dir).value = total;
}
function fix(total) {
// var dollars = [B]parseInt[/B](total);
var dollars = Math[B].floor[/B](total);
// var cents = total - dollars;
var cents = (total * 100) - (dollars * 100); [B]//[/B] browsers sometimes have rounding errors
cents = Math[B].round[/B]( cents);
if (cents < 10) cents = "0" + cents;
if (cents == 100) dollars++;
if (dollars == total || dollars == Math.[B]floor[/B](total) + 1) cents = "00";
total = dollars + "." + cents;
[B]return[/B] total;
}
Enjoy :)
Last edited by a moderator: