LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 05-26-2006, 09:55 AM   #1
bryan.out.there
LQ Newbie
 
Registered: May 2006
Posts: 6

Rep: Reputation: 1
Renaming array elements in bash


Hullo World,

Does bash have a foreach loop which allows you to work on each element in that array? (as you can in Perl for instance).

I've tried:
stripped_file+=`basename "${filename[$i]}" .ext`" "
and:
stripped_file=( ${stripped_file[@]} `basename "${filename[$i]}" .ext`".newext "

but these produce single element arrays without separating on whitespace.

Failing that - how do you split an array on whitespace? (as in perl's split).

Thanks a bunch,

Bryan.
 
Old 05-26-2006, 11:45 AM   #2
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Check out the Advanced Bash Scripting Guide. Specifically Examples 26-4 and 26-5. Using it as a guide, I think that what you are wanting is ...

Code:
for element in $(seq 0 $((${#stripped_file[@]} - 1 )))
do
 #whatever you want to do using ${stripped_file[$element]}
done
seq will return a sequence of numbers from begin to end. Begin in this case is 0, and end is the number of elements in the $array array minus one. The for loop then assigns each number of that sequence in turn to element.

To change a single element array into a multi-element ...

Code:
new_array=($(echo $old_array))
assuming that $old_array is your single element array.
 
Old 05-31-2006, 11:44 PM   #3
bryan.out.there
LQ Newbie
 
Registered: May 2006
Posts: 6

Original Poster
Rep: Reputation: 1
Thanks for array element assistance Hobbletoe,
Much appreciated. Apologies for late reply but been surfing the sea and not the web for a few days.
 
  


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
Deleting elements from array in perl with splice signalno9 Programming 2 08-16-2005 10:57 PM
bash help renaming files kahn Programming 6 06-16-2005 07:15 AM
odd behaviour of array elements in c++ markhod Programming 4 03-14-2005 09:58 AM
perl - get number of elements in an array AM1SHFURN1TURE Programming 3 03-07-2005 03:59 PM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM

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

All times are GMT -5. The time now is 02:25 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