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

  • Home
  • SEARCH
  • 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 1048433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:27:16+00:00 2026-05-16T16:27:16+00:00

I have a gross looking function and am wanting to clean it up. All

  • 0

I have a gross looking function and am wanting to clean it up. All it contains is some maps, a for loop, and if statements.

Basically I have a map that I would like to grab only certain information from but one of the keys that I need from it changes by one number in each map.

I was thinking maybe a simple switch statement or something should fix it, but I often get confused with simplifying such things.

Here is what the function looks like:

public void separateBooksFromList() {

book1 = [:]                     //map for book1
book2 = [:]                     //map for book2
book3 = [:]                     //map for book3
book4 = [:]                     //map for book4
book5 = [:]                     //map for book5
book6 = [:]                     //map for book6
book7 = [:]                     //map for book7
book8 = [:]                     //map for book8
book9 = [:]                     //map for book9
book10 = [:]                    //map for book10

lastModified = new Date(dataFile.lastModified())         //last time the file was scanned
readDate = new Date()                                    //current date the text file was read

for(int i = 0; i < bookList.size(); i++) {


    if(i==0) {
        book1['lastScan'] = lastModified
        book1['readDate'] = readDate
        book1['bookNumber'] = bookList['Book Number 0'][i]      // <- only part of the map that changes
        book1['bookTitle'] = bookList['Book Title'][i]

    }

    if(i==1) {
        book2['lastScan'] = lastModified
        book2['readDate'] = readDate
        book2['bookNumber'] = bookList['Book Number 1'][i]      // <- only part of the map that changes
        book2['bookTitle'] = bookList['Book Title'][i]
    }

    if(i==2) {
        book3['lastScan'] = lastModified
        book3['readDate'] = readDate
        book3['bookNumber'] = bookList['Book Number 2'][i]      // <- only part of the map that changes
        book3['bookTitle'] = bookList['Book Title'][i]
    }   

    if(i==3) {
        book4['lastScan'] = lastModified
        book4['readDate'] = readDate
        book4['bookNumber'] = bookList['Book Number 3'][i]      // <- only part of the map that changes
        book4['bookTitle'] = bookList['Book Title'][i]      
    }   

    if(i==4) {
        book5['lastScan'] = lastModified
        book5['readDate'] = readDate
        book5['bookNumber'] = bookList['Book Number 4'][i]      // <- only part of the map that changes
        book5['bookTitle'] = bookList['Book Title'][i]      
    }   

    if(i==5) {
        book6['lastScan'] = lastModified
        book6['readDate'] = readDate
        book6['bookNumber'] = bookList['Book Number 5'][i]      // <- only part of the map that changes
        book6['bookTitle'] = bookList['Book Title'][i]
    }   

    if(i==6) {
        book7['lastScan'] = lastModified
        book7['readDate'] = readDate
        book7['bookNumber'] = bookList['Book Number 6'][i]      // <- only part of the map that changes
        book7['bookTitle'] = bookList['Book Title'][i]
    }

    if(i==7) {
        book8['lastScan'] = lastModified
        book8['readDate'] = readDate
        book8['bookNumber'] = bookList['Book Number 7'][i]      // <- only part of the map that changes
        book8['bookTitle'] = bookList['Book Title'][i]  
    }

    if(i==8) {
        book9['lastScan'] = lastModified
        book9['readDate'] = readDate
        book9['bookNumber'] = bookList['Book Number 8'][i]      // <- only part of the map that changes
        book9['bookTitle'] = bookList['Book Title'][i]  
    }

    if(i==9) {
        book10['lastScan'] = lastModified
        book10['readDate'] = readDate
        book10['bookNumber'] = bookList['Book Number 9'][i]     // <- only part of the map that changes
        book10['bookTitle'] = bookList['Book Title'][i]
    }

  }

}

As you can see, it’s quite an ugly function.

Am I able to do a simple switch statement or something to cut down the code and make it look more professional?

  • 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-16T16:27:16+00:00Added an answer on May 16, 2026 at 4:27 pm

    There are a few things you can do here. You can use a list of books rather than book1, book2, etc… This will save repeating yourself so much.

    This is almost the same but with that change. It will create a list of size 10 (assuming 10 books) with a map entry like what you had before.

    public void separateBooksFromList() {
        lastModified = new Date(dataFile.lastModified())         //last time the file was scanned
        readDate = new Date()                                    //current date the text file was read
    
        // Use a list of Maps instead of separate variables
        int numberOfBooks =  bookList.size()
        def books = []
        numberOfBooks.times {
            books[it] = [
                lastScan: lastModified, 
                readDate: readDate, 
                bookNumber: booklist["Book Number $it"][it], 
                bookTitle: booklist["BookTitle"][it]
            ]
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an iPhone application that contains several views and their associated controllers. Looking
This has probably been answered before, but I have spent some time looking and
I have a javascript function that I am trying to add to my document,
I have a long running rake task that is gobbling up all my system
I have 2 Ids #totalcbm and #gross , i want to divide both these
I have a table with a PK that grows fairly quickly, but since rows
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
We have a mid-size SQL Server based application that has no indexes defined. Not
I have a huge database that has several tables that hold several million records.
I'm using RestSharp in a Mono project to upload some files and I have

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.