LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-29-2016, 04:40 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Rep: Reputation: 1
Bash - find number of keys in particular array


Hello sir,

I have a array like this, I need to find number of keys in it.

name[11]="u1"
name[12]="u2"
name[13]="u3"
name[21]="zim1"
name[22]="zim2"
name[31]="zam1"

In above array, I have to find number of keys start with 1 and 2 and 3, in above case it will be 3, 2, 1

The data is changed always, sometimes it is like

name[11]="u1"
name[12]="u2"
name[13]="u3"
name[14]="u4"
name[15]="u5"
name[16]="u6"
name[21]="zim1"
name[22]="zim2"
name[23]="zim3"
name[24]="zim4"
name[25]="zim5"
name[26]="zim6"
name[27]="zim7"
name[28]="zim8"
name[29]="zim9"
name[31]="zam1"
name[32]="zam2"
name[33]="zam3"
name[41]="zam4"
name[42]="zam5"
name[43]="zam6"
name[44]="zam7"
name[45]="zam8"

In this case the number of keys start with 1 are 6, start with 2 are 9, start with 3 are 3 and start with 4 are 5

The values will be changed always but not the name of array and 11,12,13,21,22,23,24.... will be in order.

Please help.

Last edited by unclesamcrazy; 04-29-2016 at 04:42 AM.
 
Old 04-29-2016, 05:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
What have you tried? This is a fairly trivial exercise.
 
Old 04-29-2016, 06:44 AM   #3
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Original Poster
Rep: Reputation: 1
I tried
echo ${#name[@]}
But it shows total number of values.

Other things I tried
[admin@localhost]$ echo ${#name[11]}
2
[admin@localhost]$ echo ${#name[12]}
2
[admin@localhost]$ echo ${#name[13]}
2
[admin@localhost]$ echo ${#name[21]}
4
[admin@localhost]$ echo ${#name[31]}
4
[admin@localhost]$ echo ${#name[41]}
4
[admin@localhost]$ echo ${#name[51]}
0

I didnt get correct answer
 
Old 04-29-2016, 07:02 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 14,842

Rep: Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823Reputation: 1823
On the contrary, you got only correct answers.
 
Old 04-29-2016, 07:07 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,948

Rep: Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530
This might help.
http://www.linuxjournal.com/content/bash-arrays
 
Old 04-29-2016, 07:07 AM   #6
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Original Poster
Rep: Reputation: 1
Is there any way so I can get 6 for start with 1 case?
 
Old 04-29-2016, 07:10 AM   #7
MadeInGermany
Member
 
Registered: Dec 2011
Posts: 198

Rep: Reputation: 77
Google for "bash array list keys"
and you will find that
Code:
for i in ${name[@]}
do
  echo $i
done
lists the values, while
Code:
for i in ${!name[@]}
do
  echo $i
done
lists the keys!

Last edited by MadeInGermany; 05-04-2016 at 12:45 PM. Reason: Corrected the first sample
 
Old 04-29-2016, 07:12 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 14,948

Rep: Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530
MadeInGermany,
Your examples are the same. It is also known as an index.
 
1 members found this post helpful.
Old 04-29-2016, 11:45 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,962

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by grail View Post
This is a fairly trivial exercise.
Really? I can think of several ways to do that, but I wouldn't call any of them "trivial." Simplest I've come up with:
Code:
for i in ${!name[@]}; do [[ $i =~ ^1 ]] && echo $i; done | wc -l
Or, doing a bit more in the shell to avoid the wc invocation:
Code:
n=0; for i in ${!name[@]}; do [[ $i =~ ^1 ]] && ((++n)); done; echo $n
 
2 members found this post helpful.
Old 04-29-2016, 12:30 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Code:
for i in ${!name[*]};do (( count[i/10]++ ));done
Now you have an array indexed with the numbers you are after each with the total you require

So yes, I thought it was fairly trivial.
 
1 members found this post helpful.
Old 05-04-2016, 05:43 AM   #11
unclesamcrazy
Member
 
Registered: May 2013
Posts: 187

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by rknichols View Post
Really? I can think of several ways to do that, but I wouldn't call any of them "trivial." Simplest I've come up with:
Code:
for i in ${!name[@]}; do [[ $i =~ ^1 ]] && echo $i; done | wc -l
Or, doing a bit more in the shell to avoid the wc invocation:
Code:
n=0; for i in ${!name[@]}; do [[ $i =~ ^1 ]] && ((++n)); done; echo $n
Thanks rknichols for your helpful solution and gratitude atleast you do not think it was trivial.
It solved my issue. Used your code in my loop and adjust little according to my script and it worked like a charm.
Thanks again.
 
Old 05-04-2016, 05:53 AM   #12
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,124

Rep: Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271
Code:
uniq -s 5 -w 1 -c
 
Old 05-04-2016, 09:23 AM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,962

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
Quote:
Originally Posted by unclesamcrazy View Post
Thanks rknichols for your helpful solution and gratitude atleast you do not think it was trivial.
I rather liked @grail's solution, which in one shot gives you an array with the counts for each of the leading digits as long as there are no indices longer than 2 digits, but I wouldn't classify that one as "trivial" either. "Clever", yes.
 
Old 05-04-2016, 12:31 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Sorry if I upset anyone with my comment. As it popped into my head immediately it did seem trivial to me (sometime forget this is noob section and so learning means not trivial )

Good point on the indices not longer than 2 digits. I did have an alternative, but wasn't sure if it went higher if we then wanted 11 out of 110 or just 1
Assuming only first digit, slight change is:
Code:
for i in ${!name[*]};do (( count[${i:0:1}]++ ));done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to find the index of bash array jags1984 Linux - Newbie 2 01-20-2014 01:06 AM
[SOLVED] Compare array index number against element in bash rewtnull Programming 10 11-01-2011 03:53 PM
C++ find array index for largest number. joshp Programming 7 03-15-2009 11:56 AM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 01:49 PM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 08:59 AM


All times are GMT -5. The time now is 02:20 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration