Tony Tony - 1 year ago 68 Question get the *values* of the first and last lines of an array

I have an array of numbers like this:

{10 20 30 40 50 60 70 80 90}

The start and end numbers will not always be the same (in fact none of them will).

I want to get the value (not the index) of the first and last entries.

So in the above example, all I'm interested in getting are the values "10" and "90".

This array is being constructed from a comma delimited text file which looks like this:


This is my code:

Dim arrName, arrValue As New List(Of String)()
Dim sdata() As String
Dim column1, column2 As Decimal

For Each line As String In IO.File.ReadAllLines("c:\file.txt")
If line <> "" And Not line.StartsWith("#") Then
sdata = line.Split(","c)
' get data from array
column1 = sdata(0)
column2 = sdata(1)
End If

So what I want is to be able to create two variables where:

column1Lowest=<the lowest value> (in this case 10)
column1Highest=<the highest value> (in this case 90)

I have tried several method, but all I seem to be getting back is the entire array each time, or "-1" (no match found in array I know).

Things I have tried: IndexOf(), FirstIndexOf(),LastIndexOf()

I am only interested in the lowest an highest values in column1 (but I still use the rest elsewhere).

I am now out of ideas.

Please help - with answers in the form that a novice can understand.



To add to Steve's answer below, I tried the Inumerable strings option but I believe I was running into issues with my file format - some files were fine - some were not (usually files without a line feed after the last line of data!).

This is my code:

Dim Lines As Collections.Generic.IEnumerable(Of String) = File.ReadLines("c:\file.txt").Where(Function(q) q.StartsWith("#") = False)
Line0 = Lines.FirstOrDefault
LineN = Lines.LastOrDefault
lowestNum = Line0.Split(New Char() {","c})
highestNum = LineN.Split(New Char() {","c})
lowNum = lowestNum(0)
highNum = highestNum(0)


So if I wanted to use this chunk of code on my already parsed file - how would I use the existing array with it?

I have just tried:

Dim Lines As Collections.Generic.IEnumerable(Of String) = {column1}
FirstValue = Lines.FirstOrDefault
LastValue = Lines.LastOrDefault

But this still prints the entire array?

To display the output I'm using:


I've tried inserting the listbox code both in and outside of the "For Each" loop. Putting them outside gives me variable issues - so I tried to declare the variables outside of the loop to no avail.

I did say I was a novice!

Answer Source

There are these pretty IEnumerable extensions that you can use

Dim first = numbers.First()
Dim last = numbers.Last()

However from your question is not clear if you want just the first and last element of the array or the highest and lowest value in the array.

For the second case you have

Dim high = numbers.Max()
Dim low = numbers.Min()

I have also noticed that you use strings to manage these values, but if they are numbers and you want to retrieve the highest and lowest numbers then you should absolutely convert these values to integers otherwise you will have a difficult time to explain to your computer that a string "100" is higher than a string "20".

Loop over your lines and convert every line to a decimal then get the min and max (Note, no check on the correctness of the input)

Dim names = new List(Of Decimal)()
Dim values = new List(Of Decimal)()
For Each line As String In IO.File.ReadAllLines("c:\file.txt")
    If line <> "" And Not line.StartsWith("#") Then
        sdata = line.Split(","c)
    End If
Dim lowValue = values.Min()
Dim highValue = values.Max()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download