Printing Array's content with carriage return (\n)

Discussion in 'Perl Misc' started by John Bokma, May 9, 2004.

  1. John Bokma

    John Bokma Guest

    Edward Wijaya wrote:

    > Hi,
    >
    > If I have an array: @myarray = (ab, bc, cd, ...)
    >
    > currently if I use the print command directly like this:
    >
    > print "@myarray\n";
    >
    > it returns:
    > ab bc cd ....
    >
    > is there anyway I can tweak the print command?


    yes, but read on...

    > so that it gives
    >
    > ab
    > bc
    > cd
    > ...
    >
    > Namely, it print out in vertical forms.


    try: print join("\n", @array), "\n";

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
     
    John Bokma, May 9, 2004
    #1
    1. Advertising

  2. John Bokma

    Jim Cochrane Guest

    In article <>, Edward Wijaya wrote:
    > Hi,
    >
    > If I have an array: @myarray = (ab, bc, cd, ...)
    >
    > currently if I use the print command directly like this:
    >
    > print "@myarray\n";
    >
    > it returns:
    > ab bc cd ....
    >
    > is there anyway I can tweak the print command?
    > so that it gives
    >
    > ab
    > bc
    > cd
    > ...


    If by tweak you're implying overriding print, it's probably possible, but
    might not be a good idea. Altenratively, you could do:

    print join("\n", @array), "\n";

    >
    > Namely, it print out in vertical forms.
    >
    > Thanks so much for your time
    >
    > Regards
    > Edward WIJAYA
    > SINGAPORE
    >



    --
    Jim Cochrane;
    [When responding by email, include the term non-spam in the subject line to
    get through my spam filter.]
     
    Jim Cochrane, May 9, 2004
    #2
    1. Advertising

  3. John Bokma

    John Bokma Guest

    Jim Cochrane wrote:

    > In article <>, Edward Wijaya wrote:


    >>is there anyway I can tweak the print command?
    >>so that it gives
    >>
    >>ab
    >>bc
    >>cd
    >>...

    >
    > If by tweak you're implying overriding print, it's probably possible, but


    jbokma:~$ perl -e '@array = qw(aa bb cc dd);$"="\n"; print "@array\n"'
    aa
    bb
    cc
    dd
    jbokma:~$

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
     
    John Bokma, May 9, 2004
    #3
  4. Hi,

    If I have an array: @myarray = (ab, bc, cd, ...)

    currently if I use the print command directly like this:

    print "@myarray\n";

    it returns:
    ab bc cd ....

    is there anyway I can tweak the print command?
    so that it gives

    ab
    bc
    cd
    ....

    Namely, it print out in vertical forms.

    Thanks so much for your time

    Regards
    Edward WIJAYA
    SINGAPORE

    --
    Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
     
    Edward Wijaya, May 9, 2004
    #4
  5. Edward Wijaya <> wrote:

    > Subject: Printing Array's content with carriage return (\n)



    A "carriage return" is the ASCII CR character.

    A "line feed" is the ASCII LF character.

    A "newline" (\n) is a *logical* end-of-line, and it is different
    on different operating systems:

    LF - *nix
    CR - mac
    CR+LF - windows (and many of the common protocols)


    So your Subject is only accurate on a Mac. :)

    I think you meant this instead:

    Subject: Printing Array's content with newline (\n)


    > currently if I use the print command directly like this:
    >
    > print "@myarray\n";


    [snip]

    > is there anyway I can tweak the print command?



    I dunno, but there are several ways to get what you want without
    tweaking the print function/operator (not a "command").


    > Namely, it print out in vertical forms.



    # tweek how arrays are interpolated, rather than print() itself
    { local $" = "\n"; # see perlvar.pod
    print "@myarray\n";
    }

    or

    # _say_ that you want them joined with newlines
    print join("\n", @myarray), "\n";



    I'd prefer the second one, it is more self-documenting.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 9, 2004
    #5
  6. Tad McClellan wrote:
    >
    > Edward Wijaya <> wrote:
    > >
    > > is there anyway I can tweak the print command?

    >
    > I dunno, but there are several ways to get what you want without
    > tweaking the print function/operator (not a "command").
    >
    > > Namely, it print out in vertical forms.

    >
    > # tweek how arrays are interpolated, rather than print() itself
    > { local $" = "\n"; # see perlvar.pod
    > print "@myarray\n";
    > }


    TMTOWTDI :)

    { local $, = local $\ = "\n"; # see perlvar.pod
    print @myarray;
    }



    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, May 9, 2004
    #6
  7. On Sun, 09 May 2004, Edward Wijaya <> wrote:
    > If I have an array: @myarray = (ab, bc, cd, ...)
    >
    > currently if I use the print command directly like this:
    >
    > print "@myarray\n";
    >
    > it returns:
    > ab bc cd ....
    >
    > is there anyway I can tweak the print command?
    > so that it gives
    >
    > ab
    > bc
    > cd
    > ...
    >
    > Namely, it print out in vertical forms.


    If you want to print like that, but NOT insert newlines in your data, it
    may be better to simply:

    foreach (@myarray) { print "$_\n"; }

    --
    David Efflandt - All spam ignored http://www.de-srv.com/
     
    David Efflandt, May 9, 2004
    #7
  8. John Bokma

    Uri Guttman Guest

    >>>>> "lv" == l v <> writes:

    lv> I usually use with preference on #2:

    lv> print map {"$_\n"} @myarray;

    often i do that with some common variations

    lv> or

    lv> map {print "$_\n"} @myarray;

    alert! map in void context! (hush abigail!).

    anyhow try benchmarking those two. i will wager that the former is much
    faster than the latter. this segues right into my rule about printing:

    print rarely, print late.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, May 10, 2004
    #8
  9. l v <> wrote:

    > I usually use with preference on #2:
    >
    > print map {"$_\n"} @myarray;
    >
    > or
    >
    > map {print "$_\n"} @myarray;



    _Why_ do you prefer #2?


    I think most people would prefer #1 because it says "print bunch 'o stuff"
    while #2 says "bunch 'o stuff print".


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 10, 2004
    #9
  10. John Bokma

    Matija Papec Guest

    X-Ftn-To: David Efflandt

    (David Efflandt) wrote:
    >> Namely, it print out in vertical forms.

    >
    >If you want to print like that, but NOT insert newlines in your data, it
    >may be better to simply:
    >
    >foreach (@myarray) { print "$_\n"; }


    Second that, I would only drop braces and use modifier,

    print "$_\n" for @myarray;



    --
    Matija
     
    Matija Papec, May 10, 2004
    #10
  11. l v <> wrote:
    > Tad McClellan wrote:
    >> l v <> wrote:
    >>
    >>>I usually use with preference on #2:
    >>>
    >>>print map {"$_\n"} @myarray;
    >>>or
    >>>map {print "$_\n"} @myarray;

    >>
    >>
    >> _Why_ do you prefer #2?
    >>
    >> I think most people would prefer #1 because it says "print bunch 'o stuff"
    >> while #2 says "bunch 'o stuff print".
    >>

    >
    > "print bunch 'o stuff" -- I like it!



    It is just an uneconomical way of saying "list". :)


    > #2 purely for comprehension by those in my work area which are VERY
    > inexperienced with Perl.



    map() is a foreach() in disguise.


    > They see the print block and understand what
    > is to occur.



    These inexperienced people are expected to know that map is
    a foreach in disguise?

    Why not just use a literal foreach?

    foreach ( @myarray )
    {print "$_\n"}

    or

    print "$_\n" foreach @myarray;


    Seems that would be even easier to understand...


    > I've never really studied which is faster, 'print map ...'
    > or 'map {print ...' or for. I guess I now have something to do tonight
    > after all.



    Don't do anything tonight.

    You should be optimizing for labor (comprehension) before speed,
    so choose whichever is easier to read and understand without
    regard to how many microseconds can be saved.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, May 11, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ferdi
    Replies:
    5
    Views:
    4,686
    Jacob Yang [MSFT]
    Aug 24, 2003
  2. Kevin Spencer
    Replies:
    3
    Views:
    3,224
    Doc Wally
    Oct 11, 2003
  3. Jeff Clark
    Replies:
    0
    Views:
    1,603
    Jeff Clark
    Oct 17, 2003
  4. Xeno Campanoli
    Replies:
    0
    Views:
    228
    Xeno Campanoli
    Feb 13, 2006
  5. Steve Anderson
    Replies:
    3
    Views:
    254
    Steve Anderson
    Jun 21, 2004
Loading...

Share This Page