LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-07-2006, 01:42 AM   #1
jayakumar
LQ Newbie
 
Registered: Dec 2006
Posts: 2

Rep: Reputation: 0
Unhappy How to initialize index with string in shell programming


Hai All,


I have some doubts in shell programming . I want to initialize index array with string ...

Like

a[abc]="hello"
a[def]="welcome"


if i give echo {$a[abc]} it should give as hello.
One more question ..

Search a pattern using grep in a file is faster or we can use a array .. whichis faster ?

Anybody can help me please ?

Regards,
Jayakumar
 
Old 12-07-2006, 07:16 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
if i give echo {$a[abc]} it should give as hello.
If you echo ${a[abc]} it will.


Search a pattern using grep in a file is faster or we can use a array .. whichis faster ?
Easy:
- have file with contents,
- time grep routine,
- time array match routine,
- compare.
I mean, you actually can find that out yourself.
 
Old 12-08-2006, 12:33 AM   #3
jayakumar
LQ Newbie
 
Registered: Dec 2006
Posts: 2

Original Poster
Rep: Reputation: 0
Hai Vikas,

Thanks for your repsonse.What u have mentioned is initializing an array in index with number ..But our requirement is initializing index with string .
a[abc]="hello"
a[def]="welcome"

One more thing is how to store the array variables permanently in memory (We will be initailzing the array with the values only once ...) The array needs to be global even the script got over.We are using solaris Os.

Could you please help me out ..?

Thanks in advance.

Jayakumar
 
Old 12-08-2006, 01:09 AM   #4
fvu
LQ Newbie
 
Registered: Nov 2006
Distribution: Ubuntu 7.10
Posts: 28

Rep: Reputation: 15
From Wikipedia article on Associative arrays:

KornShell 93 (and compliant shells: ksh93, bash, zsh, ...)

Note: The latest version of bash, 3.2, doesn't support associative arrays properly yet.

Definition:
typeset -A phonebook;
phonebook=(["Sally Smart"]="555-9999" ["John Doe"]="555-1212" ["J. Random Hacker"]="555-1337");
Dereference:
${phonebook["John Doe"]};

Freddy Vulto
http://fvue.nl/wiki/Bash
 
  


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
how to modify a text file or a string in shell programming luckyvietman Programming 5 07-12-2005 05:08 PM
C - Printing ELF String Table Index nixnoobie Programming 3 04-01-2005 06:10 AM
how to reverse a string in C programming egoleo Programming 12 04-05-2004 05:45 AM
C programming assigning an integer value to a string Linh Programming 4 06-22-2003 07:02 AM

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

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