ellem has asked for the wisdom of the Perl Monks concerning the following question:

#! /usr/local/bin/perl use warnings ; use strict ; my $one = 1 ; until ($one == 100000) { print $one++ ; print "\n" ; }
You'll notice that I have the newline on a separate line from the orginal print. I tried several versions of getting those two print lines on one line but all failed. Is this the way to do newlines with a varying variable?
--
ellem@optonline.net
There's more than one way to do it, but only some of them actually work.

Replies are listed 'Best First'.
Re: A better way to print
by cjf (Parson) on Mar 18, 2002 at 15:09 UTC

    Do you mean like so?

    #!/usr/local/bin/perl use warnings; use strict; my $one = 1; until ($one == 100000) { print $one++, "\n"; }

     

      Oh well if you're going to use a comma....
      <hangs_head_in_shame>ellem
      note me not closing the shame tag...
      --
      ellem@optonline.net
      There's more than one way to do it, but only some of them actually work.
Re: A better way to print
by tachyon (Chancellor) on Mar 18, 2002 at 15:25 UTC

    One liners:

    print ++$one, "\n" until $one == 10; print "$_\n" for 1..10; print join "\n", 1..10; # or even the somewhat less clear: s.$.$"=$/,@_=1\.\.10;qq<@_>.e&&print

    The useful things to note are the , and range .. operators perlman:perlop, the aliasing of stuff to $_, the limited need for parentheses () and the fact you can add modifier functions like for, if, unless, until and while to the end of statements saving a lot of code perlman:perlfunc. You can also write totally incomprehensible Perl just for fun :-)

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: A better way to print
by buckaduck (Chaplain) on Mar 18, 2002 at 16:28 UTC
    For the daring; change your output record separator. This allows you to automatically send a "\n" after every print statement. In fact, this is done for you automatically if you use the -l flag on Perl's command line:

    #!/usr/local/bin/perl -l use warnings ; use strict ; my $one = 1 ; until ($one == 100000) { print $one++ ; }

    buckaduck

Re: A better way to print
by YuckFoo (Abbot) on Mar 18, 2002 at 15:56 UTC
    Or this: print "${\$one++}\n";

    YuckFoo

      Or even: map{print${\[/(.*)/]->[0]++},$/}1..10

      cheers

      tachyon

Re: A better way to print
by Rex(Wrecks) (Curate) on Mar 18, 2002 at 18:02 UTC
    print $one++ . "\n" ;

    this will also work.

    "Nothing is sure but death and taxes" I say combine the two and its death to all taxes!
Re: A better way to print
by grummerX (Pilgrim) on Mar 18, 2002 at 18:51 UTC
    Yet another:
    printf "%d\n", $one++;

    -- grummerX

Re: A better way to print
by Silicon Cactus (Scribe) on Mar 18, 2002 at 21:30 UTC

    Not to be rude, and I honestly don't mean to be, but having just read the node about "How far do I go," referring to pointing in the right direction as opposed to writing code for, would this not fall under the category of RTFM?

    Not for nothing, I need help often, and would never try to block someone from receiving help, but this is basic stuff that can be found in many sources. But how productive is it to show someone how to do something perl is Known for?

      You may be right here, but the OPs code is _not_ broken and the OP does show a genuine attempt at getting "it right"

      So I say give the OP a break.

      Besides it did lead to a lively thread
        Honestly, this was not an attempt to knock OP, but more of a "why" deal, considering this is very very much covered in manuals. The idea was not to make an example of OP, god knows I have been told to RTFM before, but to make an example of the question... does this make sense?