in reply to Creating comma-delimited lists

You could use join ',',@list, but that wouldn't get you the "and" at the end...

Untested code:

@thingsicouldknow = ('this','that','the other','the last thing'); @iknowthat = (0,1,3); #this, that, and last thing # now print the things I know... @thingsiknow = @thingsicouldknow[@iknowthat]; print "I know about " print join(', ',@thingsiknow[0..@thingsiknow-2]); print ", and", @thingsiknow[-1] if @thingsiknow > 1; print ".\n".
It's probably not the best, but it might work. Update:Fixed some off-by-one errors caused by using $#foo

This version should not print the "and" if you only know one thing.

Replies are listed 'Best First'.
RE: Re: Creating comma-delimited lists
by myocom (Deacon) on Aug 24, 2000 at 01:09 UTC

    I'm not sure I realized, for some reason, that you could do @thingsiknow = @thingsicouldknow[@iknowthat]. Sure beats the heck out of doing a foreach...push deal as I was doing in my script beforehand.

    Thanks for the tip!