LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2007, 01:19 AM   #16
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
Question


Quote:
Originally Posted by ghostdog74 View Post
another way, except there's extra "," at the end.
Code:
awk 'BEGIN{s=12345;n=12350}
     s<n{ printf s++"," }' "file"
(I think you may have missed the 'for' out, and if you use exit in a BEGIN block then you don't need an input file to loop over).

Oh wait, Are you trying to use the fact that file has enough lines in it so the s<n test can match enough times? If so then you haven't put in what to do when s>=n. This is very bad code - you may have got it to print something close, but its the programming equivalent of standing on a unicyclebalanced on a pyramid to change a lightbulb ;-)

Last edited by paddy3118; 10-31-2007 at 01:48 AM. Reason: New insight into a 'unique' mind.
 
Old 10-31-2007, 02:16 AM   #17
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by paddy3118 View Post
Oh wait, Are you trying to use the fact that file has enough lines in it so the s<n test can match enough times?
bingo!. Just having some "bad" fun, seeing that there are so many ways to do the same thing.

Last edited by ghostdog74; 10-31-2007 at 07:11 AM.
 
Old 10-31-2007, 06:41 AM   #18
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by makyo View Post
Adding a spin with eval makes it work with bash3:
[...]
Yep,
good catch!
 
Old 10-31-2007, 06:45 AM   #19
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Quote:
Originally Posted by makyo View Post
Some version of seq can do it directly:
[...]
... and I think that this is
the "rigth" answer (IMHO, of course)!
Thanks for pointing it out!
 
Old 11-03-2007, 06:16 AM   #20
baidym
LQ Newbie
 
Registered: Oct 2007
Posts: 16

Original Poster
Rep: Reputation: 0
mmm.. lets make it a little tricker

Thats great thanks for all the suggestions guys, went with perl in the end:

print -n "enter first number:\n"
chomp($fstnum = <STDIN>);
print -n "enter last number:\n"
chomp($lastnum = <STDIN>);

print join ",",$fstnum..$lastnum;


But I now need to enclose each number in quotation marks i.e.

% <script>
enter first number:
%123
enter last number:
%127
"123","124","125","126","127"

ideas anyone??

also how can I assign variables on the command line?

i.e
% <script> 123 127
"123","124","125","126","127"

Michelle
 
Old 11-05-2007, 03:52 AM   #21
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
perldoc perlvar

(my italics
Code:
    $LIST_SEPARATOR
    $"
     This is like $, except that it applies to array and slice values
	    interpolated into a	double-quoted string (or similar interpreted
	    string). Default is	a space.
Code:
@L = ($firstnum..$lastnum)
local $" = ',';          # look up this in perlvar
@L = map {"\"$_\""} @L   # add quotes into the array (a bit naff?)
print "@L\n";
command line arguments appear in @ARGV
maybe use shift

$arg1 = shift;
 
Old 11-05-2007, 09:46 AM   #22
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
Yet another way to do it with Perl:
Code:
perl -le '$"=","; @array=(12345..12350);print "@array"'
12345,12346,12347,12348,12349,12350
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl and Python raghuveerbabu Programming 10 08-24-2007 08:45 AM
Python or Perl drdroid Programming 29 12-22-2006 09:54 AM
perl vs python yenonn Programming 6 08-01-2006 05:44 AM
Perl or Python JJX Programming 6 10-27-2004 03:58 AM
newgrp command within perl/csh script bobsey Programming 1 04-13-2001 10:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration