the antichomp

A

A. Sinan Unur

(e-mail address removed) (wana) wrote in @posting.google.com:
Is there a better way to it than this?

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

Of course. Read perldoc -f chomp.

Sinan
 
U

Uri Guttman

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

s/for/x/ ;

uri
 
U

Uri Guttman

ASU> (e-mail address removed) (wana) wrote in ASU> @posting.google.com:

ASU> Of course. Read perldoc -f chomp.

and how would chomp append newlines?

uri
 
A

A. Sinan Unur

ASU> (e-mail address removed) (wana) wrote in
ASU> @posting.google.com:


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

A. Sinan Unur

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.
 
U

Uri Guttman

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
 
U

Uri Guttman

w> Is there a better way to it than this?
w> $_ .= "\n" for @ARGV;
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
 
J

Janek Schleicher

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
 
A

A. Sinan Unur

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

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.
 
J

Joe Smith

A. Sinan Unur said:
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
 
J

James Willmore

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
 
P

Paul Lalli

James Willmore said:
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
 
T

Tad McClellan

Hue-Bond said:
Paul Lalli, Thu20041118@14:07:40(CET):

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;
 
W

wana

Uri Guttman said:
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
 
U

Uri Guttman

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
 
U

Uri Guttman

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
 
W

wana

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
 
T

Tad McClellan

wana said:
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?
 
T

Tad McClellan

wana said:
Uri Guttman wrote:


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().
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top