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 26 Nov 2014, 09:49
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 Posts_count  
Author Message
RSH


Joined: 05 Sep 2011
Posts: 2420
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: 887

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: 2420
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: 2278

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: 5042
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: 887

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: 5042
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.

Edited_time_total
Back to top
View user's profile Send_private_message 
technosaurus


Joined: 18 May 2008
Posts: 4379

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:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0599s ][ Queries: 12 (0.0042s) ][ GZIP on ]