Search
 
SCRIPT & CODE EXAMPLE
 

RUBY

ruby array serach

# As not mentioned I am assuming you want the cide in python language 
# just because the given code syntax just look like python
# If you want in any other language just folluw the same approach
# That will provide you the same result

# search for track by name.

# Returns the index of the track or -1 if not found

def search_for_track_name(tracks, search_string):
    # Using while loop
    # just by traversing the tracks array if  search_string found
    # return Index and chnge flag to 1 and print found and if after traversing full array
    # take a variable flag if it is 0 return not found
    flag = 0
    i = 0
    found_index=0
    while(tracks.length()--):
        if (tracks[i] == search_string):
            flag = 1
            found_index = i
            break;
        i += 1 
    if flag == 0:
        return - 1;
    return found_index

end

def main():
    music_file = File.new("album.txt", "r")
    tracks = read_tracks(music_file)
    music_file.close()

    #  taking the input of search string
    search_string = read_string("Enter the track name you wish to find: ")

    #  calling function to search string
    index = search_for_track_name(tracks, search_string)
    if index > -1
        puts "Found " + tracks[index].name + " at " + index.to_s()
    else
        puts "Entry not Found"
    end
end

main()
Comment

PREVIOUS NEXT
Code Example
Ruby :: ruby create object with attributes 
Ruby :: ruby puts object 
Ruby :: last select in rails 
Ruby :: irb loaderror 
Ruby :: update_all 
Ruby :: rails ago 
Ruby :: how to comment out embedded ruby 
Ruby :: inverse of in rails 
Ruby :: add key and value to the beginning of a hash ruby 
Ruby :: cloudbuild ruby googl 
Ruby :: ruby * 
Ruby :: ruby-on-rails 
R :: r find elements in common between vectors 
R :: r convert string to list of characters 
R :: how to select all the records above a specific datetime in r 
R :: r mean by group 
R :: delete rows by rowname in R 
R :: how to multiply two columns in r 
R :: principal component analysis in r 
R :: r: rename a column 
R :: ggplot2 legend text 
R :: convert all numeric columns to percentages R 
R :: r string split 
R :: n list to dataframe r 
R :: copy list R 
R :: how to add a totals row in r using mutate 
R :: R concaat string and number 
R :: detect rank deficient in r 
R :: create datframe on r 
Rust :: random number generator in rust 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =