Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Displaying every other line in an array. Post 303042458 by ignatius on Wednesday 25th of December 2019 10:32:01 PM
Old 12-25-2019
Displaying every other line in an array.

Hi,


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.



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

        if(len>0)
        while((len>0) && isspace(str[len-1]))
                str[--len]='\0';
}

int pbmenu(void)
{

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

	header();

        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

                arraylength++;
                // 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);
                testarray[arraylength-1]=strdup(buf);
        }

        // The +1 gives us room for a NULL on the end.  Makes it easier to find crashes.
        testarray[arraylength]=NULL;
        fclose(fp);

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

        initscr();
        noecho();
        keypad(stdscr,TRUE);

        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);

        refresh();
        getch();


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

        // 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;

        endwin();

  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];
         curs_set(0);

         if (arraylength < menulength)
                 menulength=arraylength;

         if (selection > menulength)
                 offset=selection-menulength+1;

         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)
                                 attron(COLOR_PAIR(1));
                         mvprintw(row+counter,col,formatstring,array[counter+offset]);
                         attroff(COLOR_PAIR(1));
                         }

                 ky=getch();

                 switch(ky)
                         {
                         case KEY_UP:
                                 if (selection)
                                         {
                                         selection--;
                                         if (selection < offset)
                                                 offset--;
                                         }
                                 break;
                         case KEY_DOWN:
                                 if (selection < arraylength-1)
                                         {
                                         selection++;
                                         if (selection > offset+menulength-1)
                                                 offset++;
                                         }
                                 break;
                         case KEY_HOME:
                                 selection=0;
                                 offset=0;
                                 break;
                         case KEY_END:
                                 selection=arraylength-1;
                                 offset=arraylength-menulength;
                                 break;
                         case KEY_PPAGE:
                                 selection-=menulength;
                                 if (selection < 0)
                                         selection=0;
                                 offset-=menulength;
                                 if (offset < 0)
                                         offset=0;
                                 break;
                         case KEY_NPAGE:
                                 selection+=menulength;
                                 if (selection > arraylength-1)
                                         selection=arraylength-1;
                                 offset+=menulength;
                                 if (offset > arraylength-menulength)
                                         offset=arraylength-menulength;
                                 break;

                         case 10: //enter
                                 return selection;
                                 break;
                         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");
                                 ky=getch();
                                 if (ky == 27)
                                         {
                                         curs_set(0);
                                         mvaddstr(9,77,"   ");
                                         return -1;
                                         }
                                 else
                                         if (ky=='[')
                                                 {
                                                 getch();
                                                 getch();
                                                 }
                                         else
                                                 ungetch(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: https://youtu.be/4BuPvWJV__k
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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 compare.sh: 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

6. 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

7. 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

8. 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

9. 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

10. 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
All times are GMT -4. The time now is 08:46 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy