10,20,30,40

D

Dan Jacobson

Is this how the pros would do this?
$ perl -we 'print join ",", map($_*10,(1..10))'
10,20,30,40,50,60,70,80,90,100
Simpler ways would get an extra comma, or be meant for non gappy lists.
 
F

Fabian Pilkowski

* Dan Jacobson said:
Is this how the pros would do this?
$ perl -we 'print join ",", map($_*10,(1..10))'
10,20,30,40,50,60,70,80,90,100
Simpler ways would get an extra comma, or be meant for non gappy lists.

You could get rid of all those needless parentheses to shorten your line
a little bit.

$ perl -we'print+join",",map$_*10,1..10'

Alternatively, you could use grep() instead of map(). Sure, You need the
same number of characters to type -- it's just another way to doing the
same thing (remember, TIMTOWTDI ;-).

$ perl -we'print+join",",grep/0/,1..100'

But I think this is not as efficient as your first thought. My last idea
is to set $OUTPUT_FIELD_SEPARATOR (»$,«, look into `perldoc perlvar` for
details) to a suitable value, to save one more character ;-)

$ perl -we'$,=",";print+map$_*10,1..10'
$ perl -we'$,=",";print+grep/0/,1..100'

regards,
fabian
 
S

Steven Kuo

Is this how the pros would do this?
$ perl -we 'print join ",", map($_*10,(1..10))'
10,20,30,40,50,60,70,80,90,100
Simpler ways would get an extra comma, or be meant for non gappy lists.


If you're inquring about "perl golf", then the best I could do was:

$ perl -e '$,="0,";print 1..10,"\b"'
 
F

Fabian Pilkowski

* Steven Kuo said:
If you're inquring about "perl golf", then the best I could do was:

$ perl -e '$,="0,";print 1..10,"\b"'

Just because my windows shell doesn't like the deleting "\b" so much and
there is no need to print out more characters than really desired:

$ perl -e 'print+join("0,",1..10),0'

But I like your idea.

regards,
fabian
 
B

Bob Walton

Michael said:
....
Q:... when I tried to cut-n-paste it into my WIN: ActiveState I got the
following Error Message, & don't know why ???

D:\study\perl\WIP>perl -we 'print join ",", map($_*10,(1..10))'
Can't find string terminator "'" anywhere before EOF at -e line 1.

That's because the Windoze command line processor doesn't
recognize ' as a quoting character, but does recognize ". Try:

perl -we "print join ',',map $_*10,1..10"

That is a Windoze issue, not a Perl issue, except for how it
botches up Perl one-liners.
- thanks & take care M.D.
....
 

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
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top