Please explain AWK Fibonnaci for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please explain AWK Fibonnaci for loop
# 1  
Old 07-29-2016
Please explain AWK Fibonnaci for loop

Referring to this:
Code:
#!/bin/awk -f

 BEGIN{ 
 for(i=0;i<=10;i++) 
 { 
  if (i <=1 )
  {
   x=0;
   y=1;
   print i;
  }
  else
  {
   z=x+y;
   print z; 
   x=y;
   y=z;
  }
 } 
    }

I see iteration from 0 to 10, and printing 0, 1 (i) and another 1 (z), but I'm not seeing how this part works:
Code:
x=y;
y=z;

nor how it's integrating with iteration. I realize that the continued iteration after 0, 1, 1 comes from the else section, but I'm not seeing the connection of x and y and iteration of i.

Thanks in advance for explanation. Sorry about bold words; No Parse (NP) is showing in red and isn't rendering (FF 47.0 Linux Mint).

Edit, I just saw how it works Smilie, but I can't see a delete thread option.
# 2  
Old 07-29-2016
The fibbonaci sequence is the last two numbers, summed. X is two numbers previous, Y is one number previous, so the next, Z, is X + Y. It starts at 0 and 1 here.

Then they have to move all the variables around so the "current" gets set in the one-number-previous variable, Y=Z, and "one previous" becomes "two previous", X=Y. That way they can just repeat the loop to repeat the calculation.

Except if you do it in that order, you just copy the "current" one, so X=Y happens first, then Y=Z.
# 3  
Old 07-29-2016
Hello p1ne,

Following explanation could help you in same.
Code:
#!/bin/awk -f
 BEGIN{                   #### Starting BEGIN section of awk here.
 for(i=0;i<=10;i++)       #### starting a for loop here, in which i is a variable, it's very first value is set to 0 and then it is set to run for less than or equal to value 10, 3rd section of for loop is for incrementing the variable.
 { 
  if (i <=1 )             #### Now here is a if condition where we are checking the value of variable i, either it's value is less than or equal to 1 or not. If YES(TRUE) then execute following statements in block.
  {                       #### Block starts from here when condition above is TRUE, means when value of variable i is either less than or equal to 1.
   x=0;                   #### Setting value of variable named x to 0.
   y=1;                   #### Setting value of variable named y to 1.
   print i;               #### printing the value of variable i here.
  }                       #### Ending the BLOCK here for TRUE condition statements.
  else                    #### mentioning else here, off course if above(if condition) is NOT TRUE(FALSE) then following statements should be executed.
  {                       #### Starting block for statements which will be executed when above if condition will be FALSE.
   z=x+y;                 #### Adding the values of variable named x and y and saving their sum to a variable named z.
   print z;               #### printing the value of variable named z.
   x=y;                   #### Assigning the value of variable y to variable x. As we know = is called assignment operator to assign the values to variables.
   y=z;                   #### Assigning the value of variable named z(which we got from previous step itself) to variable y. Because as per BASIC funda of Fabonacii series is to get the SUM of previous value and current value. eg--> (0(will come from fist condition),1(will come from first condition),1(sum of 0+1),2(sum of 1+1),3(sum of 2+1),5(sum of 3+2) and so on.
  }                       #### Ending the BLOCK here for FALSE conditoin statements.
 }                        #### Closing the BLOCK for, for loop here.
    }                     #### Closing the BEGIN section here.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 07-29-2016
Thanks for responses. After staring at the else statement for a few minutes and working out what was happening on a piece of paper, the concept became clear.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Explain awk

I have 2 files recevied abc def ghi totallist abc 123 jasdhfaj def 345 fjdgkfsfh ghi 567 dfjdhdhfj jkl 678 djkahfdjshdf xyz 984 jdfdhfhdh myOutputFile jkl 678 djkahfdjshdf xyz 984 jdfdhfhdh I used this command for the output : awk 'FNR==NR {f1;next} !($1 in f1)' recevied... (2 Replies)
Discussion started by: nani1984
2 Replies

2. Shell Programming and Scripting

Explain this awk

found this handy one liner in another thread which is closed, it does what i need but im trying to understand it. it basically matches the field that contains the value v and prints its position awk -F, '{for(i=1;i<=NF;i++)if($i==v)print i}' v=yourfield inputfile my understanding is assign... (3 Replies)
Discussion started by: jack.bauer
3 Replies

3. Shell Programming and Scripting

Please explain what this Awk code is doing

Hi Guys, Please help me, I am new to programming and I don’t understand what some parts of this code are doing. I have comments on the parts I know, please help if my understanding of the code is not correct and also help with parts with questions. awk ' { gsub( ">",... (1 Reply)
Discussion started by: James_Owen
1 Replies

4. Shell Programming and Scripting

explain while loop in ksh shell script

#!/bin/ksh log=ABCl log=EFG log=HIJ i=0 while <------ what is the meaning of ($i - lt 3) do print ${log} (( i=i+1 )) done (1 Reply)
Discussion started by: Bperl1967
1 Replies

5. UNIX for Dummies Questions & Answers

Please explain this simple AWK example

awk '!_++' Most importantly, I want to know what the underscore does "!_" But ideally, please breakdown the whole thing. It is supposed to remove duplicate lines when found in a file. (1 Reply)
Discussion started by: glev2005
1 Replies

6. Shell Programming and Scripting

Explain this AWK script plz

Hi frnds, one my frnds has given resolution for my problem as below. it working great , but i couldnt understand somethings in the script. Why ++ operator after the function calling. how these each block working. will each run for each input line sequencially or one block for all the lines... (9 Replies)
Discussion started by: Gopal_Engg
9 Replies

7. Shell Programming and Scripting

AWK - HELP pls explain this ?

echo "23.54" | awk ' function round(A) { return int( A + 0.5 ) } { printf("%d\n",round($1)); }'> > > > > > awk: syntax error near line 2 awk: bailing out near line 2 (2 Replies)
Discussion started by: santosh1234
2 Replies

8. Shell Programming and Scripting

plese explain awk '{print \$NF}'

please explain this awk '{print \$NF}' i have a command grep -i adding /logs/eap | grep -iv equation | awk '{print \$NF}' | sort -u | sed 's/\.\$//' >> /temp/t please explain the above awk and sed as well how it works and also what is \$NF (1 Reply)
Discussion started by: mail2sant
1 Replies

9. Shell Programming and Scripting

Explain awk

Hi, I found this command in this forum, but, couldnt understand much from it. could any one help me understand that??? the commands are : awk '{sub(/ ~/,""); printf $0 ($0~/\|$/?ORS:"")}' file1 > file2 awk '{sub(/~ */,x);printf $0(/\|$/?ORS:x)}' awk '{sub(/~ */,x);sub(/\|$/, "|\n")}8'... (4 Replies)
Discussion started by: hitmansilentass
4 Replies

10. Shell Programming and Scripting

Please explain read in a while loop

I have a script which tries to read input from a user for every value read from a file. The input file is #> more testfile TEST1 | D200 | 12345601 | | ABC company | m TEST2 | D201 | 12345602 | | ABC company | m The script test.sh is as follows while read line do read test?"Enter a... (5 Replies)
Discussion started by: jerardfjay
5 Replies
Login or Register to Ask a Question