LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 03:40 AM   #1
unclesamcrazy
Member
 
Registered: May 2013
Posts: 200

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 03:42 AM.
 
Old 04-29-2016, 04:23 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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

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, 06:02 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

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

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

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

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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 11:45 AM. Reason: Corrected the first sample
 
Old 04-29-2016, 06:12 AM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,721

Rep: Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914Reputation: 5914
MadeInGermany,
Your examples are the same. It is also known as an index.
 
1 members found this post helpful.
Old 04-29-2016, 10:45 AM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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, 11:30 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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, 04:43 AM   #11
unclesamcrazy
Member
 
Registered: May 2013
Posts: 200

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, 04:53 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
Code:
uniq -s 5 -w 1 -c
 
Old 05-04-2016, 08:23 AM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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, 11:31 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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



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 12:06 AM
[SOLVED] Compare array index number against element in bash rewtnull Programming 10 11-01-2011 02:53 PM
C++ find array index for largest number. joshp Programming 7 03-15-2009 10: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 12:49 PM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 07:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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