From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 5282 invoked by alias); 24 Apr 2013 20:33:40 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17772 Received: (qmail 4443 invoked from network); 24 Apr 2013 20:33:28 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at spf_s.oracle.com designates 156.151.31.82 as permitted sender) Message-ID: <51782DC5.8050407@oracle.com> Date: Wed, 24 Apr 2013 15:08:53 -0400 From: peter.d.miller@oracle.com Organization: Oracle Corporation User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130402 Thunderbird/17.0.5 MIME-Version: 1.0 To: zsh-users@zsh.org Subject: Re: How should I construct this? References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Source-IP: userp1040.oracle.com [156.151.31.81] On 04/24/2013 02:39 PM, thomasg wrote: > On Wed, Apr 24, 2013 at 8:16 PM, TJ Luoma wrote: >> I am trying to write a shell script which will help my computer >> automatically join Wi-Fi networks. >> >> Each network needs to have an SSID (which may have spaces in it) and a >> password (which may have spaces, punctuation, etc in it). >> >> I'm trying to figure out the best way to create this. >> >> I thought about trying to make an array or something like this where the >> first 'column' would be the SSID and the 2nd column would be the passwords >> >> ALL_WIFI_NETWORKS=( >> Home >> 89382ashfa >> Work >> 0823u2j98dyumn >> "Coffee House" "" >> "Jenny's Wifi" 8675309 >> ) >> >> >> but then I need to be able to loop through $ALL_WIFI_NETWORKS using only >> first column… something like this >> >> for SSID in {{{The First Arg in Each Line of $ALL_WIFI_NETWORKS}}} >> do >> echo "foo" >> >> done >> >> >> where the part in {{{ and }}} indicates the part where I really don't know >> how to do what I want to do. >> >> It seems like there's got to be an easier / better way of doing this, but I >> can't figure out what it is, other than keeping two lists/arrays, one of the >> SSIDs, and one with the passwords, but that seems kludgey because I have to >> ask the user (whoever uses this script besides me) to put the SSIDs in >> twice. >> >> TjL >> > What you want to use in a case like this are hashtables. > Basically this is a special array type, defined like this: > > typeset -A ALL_WIFI_NETWORKS > ALL_WIFI_NETWORKS=(name 'password' name2 'password2) > > You can then loop: for FOO in ${(k)ALL_WIFI_NETWORKS}; BAR > > Hope this helps, > > -- > thomasg here's another way I=(a b c d); for ((i=1; i<=$#I/2; i++ )); do echo $I[$((2*i-1))] $I[$((2*i))]; done Peter