Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • i usually write that as

    perl -le 'print chr(65+rand(26))'

    your version is prettier, though :)

    (sorry, i'm not a java person)
    --
    bgp is for those who can't keep it static long enough
    • Damn. I thought my version is crystal clear, but yours is just perfect and obvious -- if you know ASCII.

      And doing that in Java:

      import java.io.*;

      class Pick {
          public static void main(String[] args) {
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
              while (true) {
                  try {
                      br.readLine();
               

      • I Don't Speak Java, But:

        If this is for a party game, isn't the exception handling gratuitous? Whether it fails with a stack track or a nice error message, it's screwed up. Can't you just omit that?

        Why do you need the reader at all? Is there some requirement that you must read from stdin if you want to send to stdout?
        --
        rjbs
        • The reason for the reader is that the game were were playing required we think of words for categories but each word had to start with a particular letter. The reader was there to ensure we could just hit "Enter" and get the next letter. So if we just rerun java Pick every time, we can get it to this:

          class Pick {
              public static void main(String[] args) {
                  System.out.println((char)( 65 + (int)(Math.random() * 26)));
              }
          }

          This doesn't replicate the exact functionality of the Perl, though, which is really what the post was about. If I want the 'hit enter for a new letter' functionality, the minimum I can write seems to be this:

          import java.io.*;

          class Pick {
              public static void main(String[] args) {
                  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                  while (true) {
                      try {
                          br.readLine();
                      }
                      catch (Exception e) {}
                      System.out.println((char)( 65 + (int)(Math.random() * 26)));
                  }
              }
          }

          I've stubbed out the exception (a notorious but common practice) because that's a checked exception and failure to catch it is a compile time error. Checked exceptions are so common that you wind up with fun boilerplate like this [google.com]:

          public ValidationConsumer (EventConsumer next) {
              super (next);
              setContentHandler (this);
              setDTDHandler (this);
              try { setProperty (DECL_HANDLER, this); }
              catch (Exception e) { /* "can't happen" */ }
              try { setProperty (LEXICAL_HANDLER, this); }
              catch (Exception e) { /* "can't happen" */ }
          }