Visit Our UNIX and Linux User Community

Displaying every other line in an array.

Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Displaying every other line in an array.
# 1  
Old 12-25-2019
Displaying every other line in an array.


I have an array, that works well. But, I want to have it display every other line. Like so, 1, 3, 5, 7, etc, etc.
Here is the relevant code:

I'm sorry for the pastebin link. For some reason, I can't get the code to format properly with the code tags.

Moderator's Comments:
Mod Comment code tags work fine... everyone can use them Smilie If you cannot, watch this video.

void chomp(char *str)
        int len=strlen(str);

        while((len>0) && isspace(str[len-1]))

int pbmenu(void)

        char buf[128];
	int row=8,col=2,arraylength=0,width=75, menulength=10,selection;


        char **testarray=NULL;
        FILE *fp=fopen("hosts.txt","r");

        while(fgets(buf,128,fp) != NULL)
                chomp(buf); // Remove \n from end of line
                if(strlen(buf) > width) width=strlen(buf);
                if(!buf[0]) continue; // Ignore blank lines

                // Room for n+1 elements of char * size.
                testarray=realloc(testarray, sizeof(char *)*(arraylength+1));
                // strdup is an easy str=malloc(strlen(s)+1); strcpy(str,s);

        // The +1 gives us room for a NULL on the end.  Makes it easier to find crashes.

        // If no elements were loaded, it will still be NULL
        if(testarray == NULL)
                fprintf(stderr, "Unable to load options\n");


        selection=barmenu((const char const **)testarray,row,col,arraylength,width,menulength,0);

         if (selection == 0) pbtelnet(testarray[0], testarray[1], 24);
         if (selection == 1) pbtelnet(testarray[1], testarray[2], 26);

         if (selection == 2) pbtelnet(testarray[2], testarray[3], 23);
         if (selection == 3) pbtelnet(testarray[3], testarray[4], 23);

         if (selection == 4) pbtelnet(testarray[4], testarray[5], 23);
         if (selection == 5) pbtelnet(testarray[5], testarray[6], 23);
         if (selection == 6) pbtelnet(testarray[6], testarray[7], 23);
         if (selection == 7) pbtelnet(testarray[7], testarray[2], 23);

         if (selection == 8) pbtelnet(testarray[8], testarray[2], 23);
         if (selection == 9) pbtelnet(testarray[9], testarray[10], 23);
         if (selection == 10) pbtelnet(testarray[10], testarray[11], 23);
         if (selection == 11) pbtelnet(testarray[11], testarray[12], 23);

         if (selection == 12) pbtelnet(testarray[12], testarray[13], 23);
         if (selection == 13) pbtelnet(testarray[13], testarray[14], 23);
         if (selection == 14) pbtelnet(testarray[14], testarray[15], 23);
         if (selection == 15) pbtelnet(testarray[15], testarray[16], 23);

         if (selection == 16) pbtelnet(testarray[16], testarray[17], 23);
         if (selection == 17) pbtelnet(testarray[17], testarray[18], 23);
         if (selection == 18) pbtelnet(testarray[18], testarray[19], 23);
         if (selection == 19) pbtelnet(testarray[19], testarray[20], 23);

         if (selection == 20) pbtelnet(testarray[20], testarray[21], 23);
         if (selection == 21) pbtelnet(testarray[21], testarray[22], 23);
         if (selection == 22) pbtelnet(testarray[22], testarray[23], 23);

         if (selection == 23) pbtelnet(testarray[23], testarray[24], 23);

         if (selection == 24) pbtelnet(testarray[24], testarray[25], 23);
         if (selection == 25) pbtelnet(testarray[25], testarray[26], 23);
         if (selection == 26) pbtelnet(testarray[26], testarray[27], 23);
         if (selection == 27) pbtelnet(testarray[27], testarray[28], 23);

         if (selection == 28) pbtelnet(testarray[28], testarray[29], 23);
         if (selection == 29) pbtelnet(testarray[29], testarray[30], 23);


        // We allocated all this stuff, so free it.
        for(col=0; col<arraylength; col++) if(testarray[col]) free(testarray[col]);

        // Cannot return -1 to commandline.  127 is the maximum we should return.
        if(selection < 0) return(127);
        // 0 has special meaning, return n+1
        return selection+1;


  return 0;

 int barmenu(const char **array,const int row, const int col, const int arraylength, const int width, int menulength, int selection)
         int counter,offset=0,ky=0;
         char formatstring[7];

         if (arraylength < menulength)

         if (selection > menulength)

         sprintf(formatstring,"%%-%ds",width); // remove - sign to right-justify the menu items

         while(ky != 27)
                 for (counter=0; counter < menulength; counter++)
                         if (counter+offset==selection)


                         case KEY_UP:
                                 if (selection)
                                         if (selection < offset)
                         case KEY_DOWN:
                                 if (selection < arraylength-1)
                                         if (selection > offset+menulength-1)
                         case KEY_HOME:
                         case KEY_END:
                         case KEY_PPAGE:
                                 if (selection < 0)
                                 if (offset < 0)
                         case KEY_NPAGE:
                                 if (selection > arraylength-1)
                                 if (offset > arraylength-menulength)

                         case 10: //enter
                                 return selection;
                         case KEY_F(1): // function key 1
                                 return -1;
                         case 27: //esc
                                 // esc twice to get out, otherwise eat the chars that don't work
                                 //from home or end on the keypad
//				 system("touch temp");
                                 if (ky == 27)
                                         mvaddstr(9,77,"   ");
                                         return -1;
                                         if (ky=='[')
         return -1;

Thank you.

Last edited by Neo; 12-26-2019 at 01:45 AM.. Reason: Code Tags Please See YT Video on this:
# 2  
Old 12-26-2019
Ok. I solved it. I just created a second array. And it's working. Thanks to everyone.
# 3  
Old 12-27-2019
Rather than the effort and cost of a second array, could you create a flag that is either zero or one. You then have a simple test to add to your code:-
  • If the flag is zero, display the output and set the flag to one
  • If the flag is one, display nothing and set the flag to zero
Does that logic help?

Kind regards,
# 4  
Old 12-27-2019
Yes. The logic helps. However, i'm a newbie. And it's just easier to write arrays... A new problem has arisen.

I created an array (testarray3), but for some reason it doesn't display when I do a

           if (selection == 0) pbtelnet(testarray[0], testarray2[0], testarray3[0]);

Here is the relevant code:
           char buf3[128];
           int arraylength3=0; 
           char **testarray3=NULL;
           FILE *fp3=fopen("hosts3.txt","r"); 

           while(fgets(buf3,128,fp3) != NULL)
                         chomp(buf3); // Remove \n from end of line
                         if(strlen(buf3) > width) width=strlen(buf3); 
                         if(!buf3[0]) continue; // Ignore blank lines
                         // Room for n+1 elements of char * size. 
                         testarray3=realloc(testarray3, sizeof(char *)*(arraylength3+1)); 
                         // strdup is an easy str=malloc(strlen(s)+1); strcpy(str,s); 
           // The +1 gives us room for a NULL on the end.  Makes it easier to find crashes. 

           // If no elements were loaded, it will still be NULL 
           if(testarray3 == NULL)         
                         fprintf(stderr, "Unable to load options\n"); 

And here is the prototype for pbtelnet:
            int pbtelnet(const char *name, const char *address, const char *portnum);

Thanks for any and all help. It is greatly appreciated.
# 5  
Old 12-28-2019
Let me explain the pbtelnet function further. It uses the first array (testarray1) to display the name of the BBS. The second array (testarray2) contains the actual address of the BBS to connect to. And the third array (testarray3) contains the port number to use.

Thanks for any and all help. It is greatly appreciated.

Last edited by ignatius; 12-28-2019 at 05:21 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #267
Difficulty: Easy
In 1935, at the age of 22, Alan Turning was elected a fellow of King's on the strength of a dissertation in which he proved the central limit theorem.
True or False?

10 More Discussions You Might Find Interesting

1. Red Hat

Displaying command return in one line

Hello all I have a query (SQL) that returns a rather long field from an Oracle database. The field in question is defined on 400 characters but all these 400 cannot be displayed by the echo command. Thus when I launch the following command: echo "SELECT FIELD01 FROM TABLE_NAME;" | sqlplus -s... (9 Replies)
Discussion started by: S. BASU
9 Replies

2. Shell Programming and Scripting

Shell for displaying specific records from a line.

Input file. GMDCOM.27936 : Process Request <36812974> GMDCOM.27936 : Process Request <36812985> GMDCOM.27936 : Process Request <36812986> GMDCOM.27936 : Process Request <36812987> GMDCOM.27936 : Process Request <36812996> GMDCOM.27936 : Process Request <36812998> GMDCOM.27936 : Process... (14 Replies)
Discussion started by: ghosh_tanmoy
14 Replies

3. UNIX for Dummies Questions & Answers

Displaying field of NR, not the line #

Within AWK, how do you display a field of NR? Here's my code: awk '(NR>1) && (P1=$1-w)>=100000 {print "increase of" " " P1*.0000179," " "kW at" " " 'NR*60/431900' " " "minutes" "\n" "change from" " " 'NR-10($1)' " " "kW to" " " 'NR+70($1)' "\n"}{w=$1}' filename I can change NR and print... (3 Replies)
Discussion started by: markymarkg123
3 Replies

4. Shell Programming and Scripting

displaying a column in horizontal line separated by ', '

cat my.log blah blah blah < 1 djfh jsdfhk jksdfh < 2 dshkfl opeir pqowi < 4 khasd wouipeui say i am perfroming some action similar to below... cat my.log | egrep "<" | awk -F' ' '{print $2}' | grep -v "" it gives output as below 1 2 4 is there anyway to modify above same... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. UNIX for Dummies Questions & Answers

Displaying the Second Line of the Grep Search Results

Hi I really hope someone can help with the below question. Lets say that I have a file called output.txt and I want to display all of the lines which contain the word ‘disconnect'. I know that this can easily be obtained by using the following command: grep -i disconnect output.txt However,... (6 Replies)
Discussion started by: Sunny Sid
6 Replies

6. Shell Programming and Scripting

Displaying Array Elements in Shell Scripts

Hi All, I am using the following piece of script to print all the array elements in a script by name 31 len=${#array }; 32 j=0; 33 #echo "The length of the array is : $len" 34 while ; do 35 temp=${array} 36 echo "$temp" 37 let $j++ 38 done But I am getting the... (2 Replies)
Discussion started by: ananddr
2 Replies

7. UNIX for Dummies Questions & Answers

displaying mutliple fields on command line

This is probably the dumbest question you guys can get, but I'm trying, as a complete noob, to display the unix calendar for all the months without Saturday and Sunday showing. How can I remove those fields without having to type all the fields in individually such as: cal -y | awk '{print $2,... (3 Replies)
Discussion started by: Trellot
3 Replies

8. UNIX for Dummies Questions & Answers

displaying the last line of the file

hi... i need to display the last line of the file and capture the line in to a variable in unix envt.(not the perl ones)... please help (8 Replies)
Discussion started by: lmadhuri
8 Replies

9. Shell Programming and Scripting

displaying the path in the command line

Hi all, Does anyone know how to ammend the .cshrc file in $HOME for your session to display the path as part of the command line? So that I dont need to keep on typing pwd to see where I am? thanks Ocelot (2 Replies)
Discussion started by: ocelot
2 Replies

10. UNIX for Dummies Questions & Answers

displaying the first line?

how do i display just the first line of a file with the cat command or any command for that matter (4 Replies)
Discussion started by: imuuk
4 Replies

Featured Tech Videos