simple array problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple array problem
# 8  
Old 10-22-2010
Just to expand on what vgersh99 said.

The offending part of your code is the "i++":
Quote:
Originally Posted by enes71
Code:
#!/usr/bin/awk -f
{
t[i++]=$2               #only second and fifth field i want to extract
rssi[i++]=$5

You increase the variable "i" two times for each record, so the $2 of the first line is stored in "t[0]", the $5 of the first line in "rssi[1]", the $2 of the second line in "t[3]", etc..

Summary: it is rather dangerous to use inline arithmetic ("i++", "++i", etc.) when you're not absolutely sure what you do. It would probably suffice to change the first "i++" to "i" to make your code work.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 9  
Old 10-22-2010
Quote:
Originally Posted by michaelrozar17
The OP need field 2 and 5 of the file to be printed subsequently as
Oh Ok then go for

Code:
{ while read a a b b b c ; do echo "$a\n$b" ; done } <inputfile



---------- Post updated at 03:48 PM ---------- Previous update was at 03:47 PM ----------




Code:
# cat myt
100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a
100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a
100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b
100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a
100814 1209 1724127 7451382 -24 00:30:1b:48:92:3a
100814 1210 1724127 7451382 -41 00:30:1b:48:92:3a
# { while read a a b b b c ; do echo "$a\n$b" ; done } <myt
1205
-10
1206
-72
1207
-72
1208
-72
1209
-24
1210
-41
#

# 10  
Old 10-22-2010
@bakunin

Thanks for the explanation. I too guessed the same reason, but the solution given does not seems to work.Below is how i changed the code from i++ to i
Code:
#!/usr/bin/awk -f
{
t[i]=$2 ; rssi[i]=$5
#s=i
}
END {
        for (i=0;i<=NR;i++)
        {
        print t[i]
        print rssi[i]
        }
} inputfile

It just prints 6 empty lines..

Last edited by michaelrozar17; 10-22-2010 at 10:57 AM.. Reason: missed the title..
# 11  
Old 10-22-2010
Code:
t[++i]=$2 ; rssi[i]=$5

# 12  
Old 10-22-2010
Quote:
Originally Posted by ctsgnb
Code:
# echo "100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a
> 100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a
> 100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b
> 100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a
> 100814 1209 1724127 7451382 -24 00:30:1b:48:92:3a
> 100814 1210 1724127 7451382 -41 00:30:1b:48:92:3a" | awk '{print$5}' | uniq
-10
-72
-24
-41
#

If this is the output wanted (according to the given sample), then it does the work doesn't it ?
Yes, according to my statement above, you are right. Thanks for your help. However, I still need to make an array for each field because I will need to analyze each field in the future. I am trying to write a more complicated script to extract useful information from each field. I hope I will have something to ask again in this code and get your useful comments and help.
# 13  
Old 10-29-2010
a slight change and comparison of each array value

Hello again experts;

I have been working on my code to make it multitask. I deleted unnecessary fields in my input file, here is the last version;

Code:
1716978 7451870 -60 00:3A:98:B6:E3:B0     
1716978 7451870 -61 00:3A:98:B6:E3:B0     
1716978 7451870 -63 00:3A:98:B6:E3:B0     
1716978 7451870 -73 00:3A:98:B6:E3:B0     
1716978 7451870 -65 00:3A:98:B6:E3:B0     
1716978 7451869 -54 00:3A:98:B6:E3:B0     
1716977 7451868 -52 00:3A:98:B6:E3:B0     
1716978 7451867 -52 00:3A:98:B6:E3:B0     
1716985 7451866 -52 00:3A:98:B6:E3:B0     
1716990 7451865 -52 00:3A:98:B6:E3:B0     
1716978 7451864 -52 00:3A:98:B6:E3:B0     
1716979 7451863 -52 00:3A:98:B6:E3:B0     
1716979 7451864 -52 00:3A:98:B6:E3:B0     
1716978 7451865 -53 00:3A:98:B6:E3:B0     
1716978 7451865 -44 00:3A:98:B6:E3:B0     
1716978 7451866 -44 00:3A:98:B6:E3:B0

Suppose 1st field is X and 2nd field is Y. If I treat them in a 2 dimensional coordinate system, each record has its own point as X and Y. What I would like to do is to compare each points in each record and report if there is a 5 meter change. I would like to do this using awk and arrays. It will be comparison of each value with the previous one and if there is a change, make an array for changed instances.
Here is my code;

Code:
{
   rssi[FNR]=$3
   mac[FNR]=$4
   pos[FNR]=sqrt(($1^2)+($2^2))
   fnr=FNR
}
END {
        for (i=1;i<=fnr;i++)
        {
                if ((pos[i]-pos[i-1])>5 && (pos[i]-pos[i-1])<-5)
                pos_changed[i]=pos[i]             #I would like to assign changed values into different array.      
        }
        for (i=1;i<=fnr;i++)
        print pos_changed[i]                        #at last, reporting the changes
}

Output of this code is nothing actually, blank page and then command window again...
I think I have a problem in assigning the changed value. I searched a lot in the forum but there is no exact problem, similar but not helpful for my code.

I am looking forward to see your feedbacks again.
Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

simple wc problem

Hi there, Im sure there is a simple explanation for this but I have a file like this with no balnk lines peter paul john I run the command # var=`grep paul file.txt` # echo $var paul # echo $var | wc -l 1 but when I grep for a value that isnt in the file, i still... (4 Replies)
Discussion started by: rethink
4 Replies

2. UNIX for Dummies Questions & Answers

LWP::Simple Problem !!

Hi All, I'm having a problem when I run the following code for example perl -e 'use LWP::Simple; getprint "http://google.com"' Can't locate LWP/Simple.pm in @INC (@INC contains: /System/Library/Perl/5.8.6/darwin-thread-multi-2level /System/Library/Perl/5.8.6... (7 Replies)
Discussion started by: pawannoel
7 Replies

3. Shell Programming and Scripting

Please help me with a simple problem

Hi, I have a very simple script like below: for n in 10 20 30 do for a in 30 40 50 60 70 80 do for r in 2 3 4 5 6 7 do m=$((r*a)) count=1 while do echo "a = " $a ", m = " $m ", n = " $n ... (2 Replies)
Discussion started by: Dark2Bright
2 Replies

4. Programming

Simple g++ compilation problem

I'm trying to port some code over to unix Solaris. I'm not really a unix programmer so I'm sure this is something straight-forwards but I'm getting the following: > g++ -c CTrade.cpp In file included from CTrade.cpp:1: stdafx.h:6: warning: `#pragma once' is obsolete CTrade.cpp:4: iostream: No... (1 Reply)
Discussion started by: achartley
1 Replies

5. UNIX for Dummies Questions & Answers

simple fork() problem

I have this little program ... int main(void){ printf("Before"); fork(); printf("After"); } output is this..... BeforeAfterBeforeAfter Shouldnt it be.....BeforeAfterAfter After parent is forked child receives the copy of program and continues from next statement... (3 Replies)
Discussion started by: joker40
3 Replies

6. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

7. Shell Programming and Scripting

Simple list file ls to an array

Hi all, Simple question, how can I simply create an array from listing the files in a directory i..e myvar=`ls`; echo $myvar gives a fulllist of files/ directories how can I now convery myvar to an array so I can loop around and read each file/dir? Thnaks CF:) (6 Replies)
Discussion started by: cyberfrog
6 Replies

8. UNIX for Dummies Questions & Answers

Simple Array in Ksh Scripting

Ksh Scripting Can some one give me a simple example of array operations using ksh. For Ex: week_array = {Sunday Monday Tuesday Wednesday Thursday Friday Saturday} I want to assign and retrieve and print them along with their index. I am looking for the o/p like: 0 Sunday 1 Monday ... (2 Replies)
Discussion started by: ravikirankethe
2 Replies

9. Shell Programming and Scripting

simple Bourne problem

Hi, I'm a newer for this languages, and I have a log file, which is something like this: 35.75.253.207 - - "GET /products/orgonizer/title.png HTTP/1.1" 200 1555 "-" "Mozilla 1.4" Now, I want to write a shell code to accoplish like ./XXX.sh -N n n is a number by user input, the code should... (5 Replies)
Discussion started by: pnxi
5 Replies

10. UNIX for Advanced & Expert Users

A simple Samba problem, please help

Hi, I have successfully setup my Samba server on my Sun Solaris 8 Sparc machine. My Win2000 workstation is able to see the unix worstation and its shared directory from Network Neighborhood. However, the Unix workstation appears as Ip address instead of Hostname on my network neighborhood... (5 Replies)
Discussion started by: champion
5 Replies
Login or Register to Ask a Question