Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Wed 23 Jul 2014, 18:26
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Array in bash
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [8 Posts]  
Author Message
RSH


Joined: 05 Sep 2011
Posts: 2283
Location: Germany

PostPosted: Thu 20 Dec 2012, 19:06    Post subject:  Array in bash  

Hi.

Is there a short solution for this?
Code:
DVArray=("203" "204" "205" "206" "207" "208" "208" "210" ... ... ... ... ... "300")

Maybe something like this?
Code:
DVArray=("203" .. "300")

Thanks

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Thu 20 Dec 2012, 20:17    Post subject: Re: Array in bash  

RSH wrote:
Hi.

Is there a short solution for this?
Code:
DVArray=("203" "204" "205" "206" "207" "208" "208" "210" ... ... ... ... ... "300")

Maybe something like this?
Code:
DVArray=("203" .. "300")

Thanks

RSH


Perhaps this-
Code:
# DVArray=( `for i in {203..300};do echo "$i ";done` )
#  echo ${DVArray[2]}
205


Cheers,
s
(Note space here in "$i ")
Back to top
View user's profile Send private message 
RSH


Joined: 05 Sep 2011
Posts: 2283
Location: Germany

PostPosted: Thu 20 Dec 2012, 20:33    Post subject:  

Code:
# DVArray=( `for i in {203..300};do echo "$i ";done` )
#  echo ${DVArray[2]}
205

Thank you, seaside!

This is exactly what I was looking for! Smile

I need to have a lot more bash knowledge! Laughing

RSH

_________________
LazY Puppy
RSH's DNA
SARA B.
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 2217

PostPosted: Fri 21 Dec 2012, 02:36    Post subject:  

Even better:
DVArray=( $(seq 203 300) )
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5009
Location: Arizona, U.S.A.

PostPosted: Fri 21 Dec 2012, 19:11    Post subject:  

amigo; That`s a loop method I`ve never seen before.

I`ve got to go back and study the new Bash version docs.
Back to top
View user's profile Send private message 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: Sat 22 Dec 2012, 11:24    Post subject:  

sunburnt wrote:


I`ve got to go back and study the new Bash version docs.


sunburnt,

"seq" is a separate program and since bash 3+, practically anything done by seq is now possible in bash "for" loops.

Regards,
s
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 5009
Location: Arizona, U.S.A.

PostPosted: Sat 22 Dec 2012, 22:52    Post subject:  

seaside; Thanks for the info. Now I really need to do a refresher...

I was surprised to find that the for loop will read a file directly!

This loop: echo "$List" |while read Line ;do ... ;done
Still has an advantage or two.

Last edited by sunburnt on Tue 25 Dec 2012, 21:35; edited 1 time in total
Back to top
View user's profile Send private message 
technosaurus


Joined: 18 May 2008
Posts: 4277

PostPosted: Tue 25 Dec 2012, 16:52    Post subject:  

It looks like the question asked was solved, but it is possible that it may not have been the right question. There are often better ways of handling arrays of sequential info depending on the context, by just using a few variables. (arrays are bashisms btw)... so the shorter solution could be not to use an array.

Ex. use:
starting_point, end_point, step
or
offset, increment_by, max_value

Which often need to be setup in the loops in some hacky way around the array anyways ... usually with an offset and/or max and step value.

_________________
Web Programming - Pet Packaging 100 & 101
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [8 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0590s ][ Queries: 11 (0.0073s) ][ GZIP on ]