> Programming Languages > Perl
Various Topics Home | Disclaimer | Report Adult Posts

Various Topics on Perl



Perl - "the antichomp" in Programming Languages


Old 11-17-2004   #11
.. ..n.. ....
 
Default Re: the antichomp

Uri Guttman <uguttman@athenahealth.com> wrote in
news:m38y9071n5.fsf@lap.athenahealth.com:

>>>>>> "ASU" == A Sinan Unur <1usa@llenroc.ude.invalid> writes:

>
> ASU> Uri Guttman <uri@stemsystems.com> wrote in
> ASU> news:x7is845vlh.fsf@mail.sysarch.com:
>
> >>>>>>> "w" == wana <ioneabu@yahoo.com> 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.
 
Old 11-17-2004   #12
..e ..i..
 
Default Re: the antichomp

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 ***uming that the "total number of characters added" is
not needed.
-Joe

 
Old 11-18-2004   #13
..m.. ..llmo..
 
Default Re: the antichomp

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
 
Old 11-18-2004   #14
.... ..l..
 
Default Re: the antichomp

"James Willmore" <jwillmore@fastmail.us> wrote in message
newsan.2004.11.18.04.29.35.485112@fastmail.us...
> 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

 
Old 11-18-2004   #15
..e-Bo..
 
Default Re: the antichomp

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;


--
Hue-Bond
 
Old 11-18-2004   #16
..d ..Clell..
 
Default Re: the antichomp

Hue-Bond <responder_solo_en_el_grupo@yahoo.es> 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
tadmc@augustmail.com Perl programming
Fort Worth, Texas
 
Old 11-19-2004   #17
.... ..grav..
 
Default Re: the antichomp

"Tad McClellan" <tadmc@augustmail.com> wrote in message
news:slrncppcip.4ca.tadmc@magna.augustmail.com...
<snip>
> Neither of which is an "unchomp" though, since they do not
> take the value of $/ into account, so this is probably better:
>
> $_ .= $/ for @ARGV;


ISTM the OP did not state his objective clearly. If his intent was to alter
the contents of @ARGV, then the above statement does what he intended. The
OP might consult the last paragraph in Chapter 4 (pp. 64-65) of LP2ed (Camel
Book) for an explanation.

OTOH, if the OP's intent was to *not* alter @ARGV, but rather, was to simply
add newlines when (later) printing the elements of @ARGV, perhaps the
following would suffice (Thanks to Damian Conway):

use Perl6::Say;
say $_ for @ARGV;

Cheers.
--
Bill Segraves


 
Old 11-19-2004   #18
....
 
Default Re: the antichomp

Uri Guttman <uguttman@athenahealth.com> wrote in message news:<m3d5yc71q8.fsf@lap.athenahealth.com>...
> >>>>> "ASU" == A Sinan Unur <1usa@llenroc.ude.invalid> writes:

>
> ASU> Uri Guttman <uri@stemsystems.com> wrote in
> ASU> news:x765445uy1.fsf@mail.sysarch.com:
>
> >>>>>>> "ASU" == A Sinan Unur <1usa@llenroc.ude.invalid> 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
 

Thread Tools
Display Modes





Powered by vBulletin®
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0