the antichomp

Discussion in 'Perl Misc' started by wana, Nov 17, 2004.

  1. wana

    wana Guest

    Is there a better way to it than this?

    $_ .= "\n" for @ARGV;

    wana
     
    wana, Nov 17, 2004
    #1
    1. Advertising

  2. (wana) wrote in news:bf0b47ca.0411170715.2aa1d853
    @posting.google.com:

    > Is there a better way to it than this?
    >
    > $_ .= "\n" for @ARGV;


    Of course. Read perldoc -f chomp.

    Sinan
     
    A. Sinan Unur, Nov 17, 2004
    #2
    1. Advertising

  3. wana

    Uri Guttman Guest

    >>>>> "w" == wana <> writes:

    w> Is there a better way to it than this?
    w> $_ .= "\n" for @ARGV;

    s/for/x/ ;

    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, Nov 17, 2004
    #3
  4. wana

    Uri Guttman Guest

    >>>>> "ASU" == A Sinan Unur <> writes:

    ASU> (wana) wrote in news:bf0b47ca.0411170715.2aa1d853
    ASU> @posting.google.com:

    >> Is there a better way to it than this?
    >>
    >> $_ .= "\n" for @ARGV;


    ASU> Of course. Read perldoc -f chomp.

    and how would chomp append newlines?

    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, Nov 17, 2004
    #4
  5. Uri Guttman <> wrote in
    news::

    >>>>>> "ASU" == A Sinan Unur <> writes:

    >
    > ASU> (wana) wrote in
    > news:bf0b47ca.0411170715.2aa1d853 ASU> @posting.google.com:
    >
    > >> Is there a better way to it than this?
    > >>
    > >> $_ .= "\n" for @ARGV;

    >
    > ASU> Of course. Read perldoc -f chomp.
    >
    > and how would chomp append newlines?


    It would not. But it would explain what one needs to do to write an
    'antichomp'.

    chomp VARIABLE
    chomp( LIST )
    chomp

    This safer version of "chop" removes any trailing string
    that corresponds to the current value of $/ (also known
    as $INPUT_RECORD_SEPARATOR in the "English" module). It
    returns the total number of characters removed from all
    its arguments.

    So, "the antichomp" would have to do the opposite of what chomp does,
    wouldn't it?

    Sinan.
     
    A. Sinan Unur, Nov 17, 2004
    #5
  6. Uri Guttman <> wrote in
    news::

    >>>>>> "w" == wana <> writes:

    >
    > w> Is there a better way to it than this?
    > w> $_ .= "\n" for @ARGV;
    >
    > s/for/x/ ;
    >
    > uri


    I am confused.

    use strict;
    use warnings;

    $_ .= "\n" x @ARGV;

    print @ARGV;
    __END__

    D:\Home> perl t.pl 1 2 3 4 5
    12345

    OTOH:

    use strict;
    use warnings;

    $_ .= "\n" for @ARGV;

    print @ARGV;
    __END__

    D:\Home> perl t.pl 1 2 3 4 5
    1
    2
    3
    4
    5

    What am I missing?

    Sinan.
     
    A. Sinan Unur, Nov 17, 2004
    #6
  7. wana

    Uri Guttman Guest

    >>>>> "ASU" == A Sinan Unur <> writes:

    ASU> Uri Guttman <> wrote in
    ASU> news::

    >>>>>>> "ASU" == A Sinan Unur <> writes:

    >>
    >> >> $_ .= "\n" for @ARGV;

    ASU> Of course. Read perldoc -f chomp.
    >>
    >> and how would chomp append newlines?


    ASU> It would not. But it would explain what one needs to do to write an
    ASU> 'antichomp'.

    ASU> So, "the antichomp" would have to do the opposite of what chomp does,
    ASU> wouldn't it?

    but that isn't an antichomp. it is appending a newline it over and over
    to a single string. and writing an antichomp for such a simple op makes
    little sense. see my other post for a better solution.

    uri
     
    Uri Guttman, Nov 17, 2004
    #7
  8. wana

    Uri Guttman Guest

    >>>>> "ASU" == A Sinan Unur <> writes:

    ASU> Uri Guttman <> wrote in
    ASU> news::

    >>>>>>> "w" == wana <> writes:

    >>

    w> Is there a better way to it than this?
    w> $_ .= "\n" for @ARGV;
    >>
    >> s/for/x/ ;
    >>
    >> uri


    ASU> I am confused.

    untested as always. must have been too late at night. i didn't see the
    $_ being aliased to the elements of @ARGV.

    uri
     
    Uri Guttman, Nov 17, 2004
    #8
  9. On Wed, 17 Nov 2004 07:15:55 -0800, wana wrote:

    > Is there a better way to it than this?
    >
    > $_ .= "\n" for @ARGV;


    Well a bit shorter and perhaps more in a sense of antichomp might be
    $_ .= $/ for @ARGV;

    However, that fails to do something sensful if $/ contains e.g. a
    reference to a number, but whenever $/ contains a simple line ending
    string, that antichomp variation would add what chomp would have removed
    perhaps before.


    Greetings,
    Janek
     
    Janek Schleicher, Nov 17, 2004
    #9
  10. Uri Guttman <> wrote in
    news::

    >>>>>> "ASU" == A Sinan Unur <> writes:

    >
    > ASU> Uri Guttman <> wrote in
    > ASU> news::
    >
    > >>>>>>> "w" == wana <> writes:
    > >>

    > w> Is there a better way to it than this?
    > w> $_ .= "\n" for @ARGV;
    > >>
    > >> s/for/x/ ;
    > >>
    > >> uri

    >
    > ASU> I am confused.
    >
    > untested as always. must have been too late at night. i didn't see the
    > $_ being aliased to the elements of @ARGV.


    It's OK. I was beginning to think I was missing something extremely
    fundamental :)

    Sinan.
     
    A. Sinan Unur, Nov 17, 2004
    #10
  11. wana

    Joe Smith Guest

    A. Sinan Unur wrote:

    >>Is there a better way to it than this?
    >>
    >>$_ .= "\n" for @ARGV;

    >
    >
    > Of course. Read perldoc -f chomp.


    That's a rather cryptic way of saying that

    $_ .= $/ for @ARGV;

    is better. I would add logic to not append $/
    if the string already has the end-of-line character(s).

    @array1 = ("line1", "linefeed already present on second\n", "line3");

    @array = @array1;
    $_ .= "\n" for @array;
    print 'A: ',@array;

    @array = @array1;
    $_ .= (m{$/$} ? '' : $/) for @array;
    print 'B: ',@array;

    That's assuming that the "total number of characters added" is
    not needed.
    -Joe
     
    Joe Smith, Nov 17, 2004
    #11
  12. On Wed, 17 Nov 2004 07:15:55 -0800, wana wrote:

    > Is there a better way to it than this?
    >
    > $_ .= "\n" for @ARGV;


    Maybe ...

    #append a newline to each element in @ARGV
    $_ = join("\n", @ARGV);

    (one liner)
    perl -e '$_ = join("\n", @ARGV);print "$_\n";' 1 2 3

    If all you want to do is append a newline to each element of @ARGV, then
    'join' is probably the best way to do it ...but, of course, TMTOWTDI :)

    HTH

    Jim
     
    James Willmore, Nov 18, 2004
    #12
  13. wana

    Paul Lalli Guest

    "James Willmore" <> wrote in message
    news:p...
    > On Wed, 17 Nov 2004 07:15:55 -0800, wana wrote:
    >
    > > Is there a better way to it than this?
    > >
    > > $_ .= "\n" for @ARGV;

    >
    > Maybe ...
    >
    > #append a newline to each element in @ARGV
    > $_ = join("\n", @ARGV);
    >
    > (one liner)
    > perl -e '$_ = join("\n", @ARGV);print "$_\n";' 1 2 3
    >
    > If all you want to do is append a newline to each element of @ARGV,

    then
    > 'join' is probably the best way to do it ...but, of course, TMTOWTDI

    :)

    This doesn't at all do what the OP asked for. Your join method creates
    a string comprised of all of the elements in @ARGV seperated by
    newlines. The OP wanted a way to 'unchomp' the array @ARGV - that is to
    modify @ARGV so that each element has a newline on the end. Your method
    creates a single new string and leaves @ARGV unmodified.

    Paul Lalli
     
    Paul Lalli, Nov 18, 2004
    #13
  14. Hue-Bond <> wrote:
    > Paul Lalli, Thu20041118@14:07:40(CET):
    >>
    >> The OP wanted a way to 'unchomp' the array @ARGV - that is to
    >> modify @ARGV so that each element has a newline on the end.

    >
    > Then what about:
    >
    > @ARGV = map { $_ . "\n" } @ARGV;



    or

    $_ .= "\n" for @ARGV;


    Neither of which is an "unchomp" though, since they do not
    take the value of $/ into account, so this is probably better:

    $_ .= $/ for @ARGV;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 18, 2004
    #14
  15. wana

    wana Guest

    Uri Guttman <> wrote in message news:<>...
    > >>>>> "ASU" == A Sinan Unur <> writes:

    >
    > ASU> Uri Guttman <> wrote in
    > ASU> news::
    >
    > >>>>>>> "ASU" == A Sinan Unur <> writes:

    >
    > >> >> $_ .= "\n" for @ARGV;

    > ASU> Of course. Read perldoc -f chomp.
    > >>
    > >> and how would chomp append newlines?

    >
    > ASU> It would not. But it would explain what one needs to do to write an
    > ASU> 'antichomp'.
    >
    > ASU> So, "the antichomp" would have to do the opposite of what chomp does,
    > ASU> wouldn't it?
    >
    > but that isn't an antichomp. it is appending a newline it over and over
    > to a single string. and writing an antichomp for such a simple op makes
    > little sense. see my other post for a better solution.
    >
    > uri


    The question relates to my use of my own function SaveToFile which I
    realize is a poor choice of sub names in Perl but I copied it from
    Borland C++ which is where I used it all the time. The old Borland
    version will save an array of strings to a file with each string on a
    separate line. I used my version and found that the strings where all
    stuck together on a single line. I then tried out File::Slurp which
    did the same. I was not sure if I wanted to change my function's
    default action without further thought, so I did it in my program
    before the function call: $_ .= "\n" for @ARGV;

    By the way, I am going to start using File::Slurp. I was under the
    mistaken impression that it was hard to use. It is actually pretty
    easy. To install, I had to copy the File directory from lib to use it
    because I am on a shared server where I don't have root access, so
    make install won't work for me. This has been true for all modules I
    have installed. Fortunately, my provider has pre-installed many many
    modules already.

    wana
     
    wana, Nov 18, 2004
    #15
  16. wana

    Uri Guttman Guest

    >>>>> "w" == wana <> writes:

    w> The question relates to my use of my own function SaveToFile which I
    w> realize is a poor choice of sub names in Perl but I copied it from
    w> Borland C++ which is where I used it all the time. The old Borland
    w> version will save an array of strings to a file with each string on a
    w> separate line. I used my version and found that the strings where all
    w> stuck together on a single line. I then tried out File::Slurp which
    w> did the same. I was not sure if I wanted to change my function's
    w> default action without further thought, so I did it in my program
    w> before the function call: $_ .= "\n" for @ARGV;

    but you said the lines are in an array. @ARGV is not a normal array so
    you shouldn't be modifying it in place in general. map would be a better
    choice for this as you would want to assign the results to another
    array.

    w> By the way, I am going to start using File::Slurp. I was under the
    w> mistaken impression that it was hard to use. It is actually pretty
    w> easy. To install, I had to copy the File directory from lib to use
    w> it because I am on a shared server where I don't have root access,
    w> so make install won't work for me. This has been true for all
    w> modules I have installed. Fortunately, my provider has
    w> pre-installed many many modules already.

    where did you get the impression that it was hard to use? how would
    making a hard to use module be useful? one thing i strive for in all my
    work is making stuff easy to use without sacrificing speed and
    flexibility and file::slurp does all that.

    and if you have your lines in an array then this is how you would do it:

    use File::Slurp ;

    write_file( 'my_file_with_newlines', map "$_\n", @array );

    really hard to use!! :)

    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, Nov 18, 2004
    #16
  17. wana

    Uri Guttman Guest

    >>>>> "w" == wana <> writes:

    w> I would not thought of it myself (the use of map), but now I will.
    w> Thanks :)

    map is easy but for some reason freaks out many perl newbies.

    w> Very cool. Thanks for making File::Slurp available. It's my kind
    w> of module. You might want to add to the documentation when you
    w> update it that you can just copy the File directory from the lib
    w> directory and use it as is without the install process for non-root
    w> users.

    i didn't create the module but i rewrote it and took it over. there is
    no need for that doc note as it is true for any pure perl module. and
    the FAQ tells you how to install modules into your own space without
    needing root.

    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, Nov 19, 2004
    #17
  18. wana

    wana Guest

    Uri Guttman wrote:

    [snip]
    >
    > where did you get the impression that it was hard to use? how would
    > making a hard to use module be useful? one thing i strive for in all my
    > work is making stuff easy to use without sacrificing speed and
    > flexibility and file::slurp does all that.
    >
    > and if you have your lines in an array then this is how you would do it:
    >
    > use File::Slurp ;
    >
    > write_file( 'my_file_with_newlines', map "$_\n", @array );
    >
    > really hard to use!! :)
    >
    > uri
    >


    I would not thought of it myself (the use of map), but now I will.
    Thanks :)

    My previous language experience is with (in order) Java, Borland C++,
    Microsoft C++, and, briefly, PHP. Compared to Perl, they might as well all
    be the same. I have always been used to carrying over programming
    practices from one language/platform to the next with little change. I am
    not used to so much power and expression being built into the language
    itself.

    Very cool. Thanks for making File::Slurp available. It's my kind of
    module. You might want to add to the documentation when you update it that
    you can just copy the File directory from the lib directory and use it as
    is without the install process for non-root users.

    wana
     
    wana, Nov 19, 2004
    #18
  19. wana <> wrote:

    > To install, I had to copy the File directory from lib to use it
    > because I am on a shared server where I don't have root access, so
    > make install won't work for me.



    You do not need to be root to install modules.


    perldoc -q module

    How do I keep my own module/library directory?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 19, 2004
    #19
  20. wana <> wrote:
    > Uri Guttman wrote:



    >> and if you have your lines in an array then this is how you would do it:


    >> write_file( 'my_file_with_newlines', map "$_\n", @array );



    > I would not thought of it myself (the use of map), but now I will.



    If you want to "transform a list", use map().

    If you want to "filter a list", use grep().


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 19, 2004
    #20
    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.

Share This Page