Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1113899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:55:31+00:00 2026-05-17T02:55:31+00:00

I writing a line item and the values are like this Ad# Pub# Loc

  • 0

I writing a line item and the values are like this

Ad#        Pub#     Loc    date    line#
ad1        P001     AK     093010   1
ad1        P001     AK     093010   2
ad1        P001     AK     093010   3
ad1        P001     AK     100110   1

When the ad#,Pub#, loc and date are same as the previous record the line is incremented. Once any of the values in ad,pub, loc and date changes compared to previous record value of line is changed to 1 again.
I am writing these lines in a for loop. I need to know what logic i need to use to get the line# values? Do i use an array to store values of previous line in an array and compare them with current line values and if they are same then add 1 to line#. Is there a better logic? Or is there a better way other than arrays to compare the previous line and current line

These values are got in from various tables.

for (int l = 0; l < totalrecords; l++)
{
    for (int i = 0; i < fields.Length; i++)
    {
        if (i == 0)
        {
            // logic to get the value for field 1
        }
        else if (i == 1)
        {
            // logic to get the value for field 2
        }
        if (i == 3)
        {
            // logic to get the value for field 3
        }
        else if (i == 4)
        {
            // logic to get the value for field 4
        }
        else if (i == 5)
        {
            // What would be the logic to write the line#???
        }
    }
}

thanks
Prady

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T02:55:32+00:00Added an answer on May 17, 2026 at 2:55 am

    First off, I don’t know why you need the inner i loop; Is the number of fields dynamic?

    Anyways, back to your question. Without knowing whether your source is DataTables, DataReaders, Arrays, etc, it’s hard to answer.

    But yes, I’d remember the previous values between iterations and reset the line number when current values differ.

    If using DataTable, I typically use this logic:

    var dt = ...
    for(var rowIndex = 0; rowIndex < dt.Rows.Count;)
    {
        // start of new set
        var curAd = (string)dt.Rows[rowIndex]["ad"];
        var curPub = (string)dt.Rows[rowIndex]["pub"];
        var curLoc = (string)dt.Rows[rowIndex]["loc"];
        var curDate = (DateTime)dt.Rows[rowIndex]["date"];
        var line = 1;
    
        for (; rowIndex < dt.Rows.Count;)
        {
            // end of set?
            if (curAd != (string)dt.Rows[rowIndex]["ad"] ||
               curPub != (string)dt.Rows[rowIndex]["pub"] ||
               curLoc != (string)dt.Rows[rowIndex]["loc"] ||
               curDate != (DateTime)dt.Rows[rowIndex]["date"])
            {
                break;
            }
    
            // write out current values with new line number
            // ...
    
            // get ready for next row
            rowIndex++;
            line++;
        }
    
        // key fields have changed, or eof, loop around to terminate or start new set as needed
    
    }
    

    The benefit of this is that if you need to do anything at the end of each set, eg. totals, you can do that after the inner loop finishes, before looping around to start a new set.

    I’m sure LINQ could make this easy, too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just recently found myself writing this line of code, which i did not
EDIT: To explain my motivation for this, I'm writing a command-line utility that takes
I'm writing a command line program in ANSI C to parse a Quake 2
I'm writing a command-line development utility for my team, using Ruby. I'm trying to
I am writing a small command line utility. It should hopefully be able to
I am writing a python command line script that takes a .ldif and two
I have been writing C++ Console/CMD-line applications for about a year now and would
I'm writing a simple ruby sandbox command-line utility to copy and unzip directories from
I'm writing a .NET command-line application that will migrate users from an existing database
I'm writing a little command-line program in Haskell. I need it to dispatch to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.