Re: Perl Pro but Java Newbie: Need nudge in proper direction for my
favorite Perl routine in Java
On Sep 14, 5:23 pm, Mark Space <marksp...@sbcglobal.net> wrote:
/usr/ceo wrote:
It's not just Java, but all languages that don't allow instring
substitution that I've never really liked. It drives me buts to have
OK, re-read this and had a thought. What is "in-string substitution"?
You mean like printf?
Geeze, if that's what you wanted, you should have asked:
<http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html>
// Writes a formatted string to System.out.
System.out.format("Local time: %tT", Calendar.getInstance());
// -> "Local time: 13:34:18"
// Writes formatted output to System.err.
System.err.printf("Unable to open file '%1$s': %2$s",
fileName, exception.getMessag=
e());
// -> "Unable to open file 'food': No such file or directory"
Instring substitution meaning this:
Ruby:
what="dog"
puts "The #{what} days of summer"
Perl:
$what = 'dog';
print "The $what days of summer\n";
Also known as string interpolation. Love it. Taking an example
someone else left where they spoke rather convincingly that I was
merely substituting commas (,) for pluses (+):
class TestOut {
public static void main ( Strings ... args ) {
int i = 5;
Object o = new Object();
javax.swing.JLabel label = new javax.swing.JLabel( "Hi" );
System.out.println( "I have " + i + " ints and I have " +
"\nthis " + o + " thing here and " +
"\nI have a JLabel " + label + "." +
"\nHave a nice day!"
);
}
}
Perl:
#!/usr/bin/perl
$|++;
use strict;
use warnings qw( all );
use MyOrg::SimpleObject;
my $int = 5;
my $dog = "Fido";
my $o = MyOrg::SimpleObject->new( name => 'Java Crammer' );
$o->name( '/usr/ceo' );
print "I have an int as $int, and my dog's name is $dog, and my name
is ${\ $o->name }...\n";
I have an int as 5, and my dog's name is Fido, and my name is /usr/ceo.=
...
Like that... Not using format or printf... I don't expect to be able
to do this in Java. That's fine.
/usr/ceo