Jump to content

Populating Multi Dimensional Array using StringRegExp


Recommended Posts

Hi all.

I'm struggling with what I hope is a really basic problem.

I'm reading a file containing HTML formatted text, and using StringRegExp I can successfully match the strings I want to extract from particular lines, e.g.

<a name="year-1977"> </a><h4>Season 1, Episode 1: <a href="/title/tt0501326/">Creed of Slaves</a></h4><b>Original Air Date: 18 September 1977</b><br> "Necessity is the plea for every infringement of human freedom. It is the argument of tyrants. It is the creed of slaves" (William Pitt). Home Affairs correspondent Jim Kyle, a journalist for one of Britain's three remaining newspapers, provides assistance to a doctor struggling to help his asthmatic daughter leave the United Kingdom. His endeavours bring him into close contact with the Public Control Department and its tools of bureaucratic repression.<br/> <br/>

However, I want to write these matches to a multi dimensional array, by looping through the file and this is where I've come unstuck, as all my attempts have only produced no results!

What I need to do is to parse the above example text line six times extracting the relevant info (e.g. StringRegExp($Temp, '(?:year-)([0-9]{4})', 1) parses the year) and then write this data to the relevant columns within an array.

Looking at the help file (yes, I have read it! :P ), has only confused me further, so is there some kind soul out there who could at least point me in the right direction?

TIA

DG

Link to comment
Share on other sites

  • Moderators

Hi all.

I'm struggling with what I hope is a really basic problem.

I'm reading a file containing HTML formatted text, and using StringRegExp I can successfully match the strings I want to extract from particular lines, e.g.

However, I want to write these matches to a multi dimensional array, by looping through the file and this is where I've come unstuck, as all my attempts have only produced no results!

What I need to do is to parse the above example text line six times extracting the relevant info (e.g. StringRegExp($Temp, '(?:year-)([0-9]{4})', 1) parses the year) and then write this data to the relevant columns within an array.

Looking at the help file (yes, I have read it! :P ), has only confused me further, so is there some kind soul out there who could at least point me in the right direction?

TIA

DG

You're lacking information that could be vital to help you.

1. What does the other array look like.

2. Why are you parsing it 6 times?

3. You've showed us the first bit of data you want, what is the rest you want?

First part of getting help is to explain yourself fully (what you've done, what the before attempt results are (You've proved that), and after desired results are (You haven't provided that)), provide all the code you've attempted, and make sure your pseudo / re-creation code isn't more than 50 lines... most vets won't look at it if it is.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You're lacking information that could be vital to help you.

1. What does the other array look like.

2. Why are you parsing it 6 times?

3. You've showed us the first bit of data you want, what is the rest you want?

First part of getting help is to explain yourself fully (what you've done, what the before attempt results are (You've proved that), and after desired results are (You haven't provided that)), provide all the code you've attempted, and make sure your pseudo / re-creation code isn't more than 50 lines... most vets won't look at it if it is.

Sorry - I forgot to include a code snippet in my haste.

Below is a snippet where I'm trying to populate an array of (for now) 2 columns, but I'm having no joy at all no matter how I change the code. Basically, for each line of text, there will eventually be output six columns of data, but it's the population of the array that's giving the headache (literally! :P ).

#include <array.au3>

Dim $Array[1][2]

Local $ImportTextFileHandle, $Temp
        
; Open Import File for Read
$ImportTextFileHandle = FileOpen("IMDb.txt", 0)

; Write File Data to Data Array
While 1
    $Temp = FileReadLine($ImportTextFileHandle)
    If @error = -1 Then ExitLoop
    MsgBox(0,"",$Temp); Display fileline
    $Array[0][0] = StringRegExp($Temp, '(?:year-)([0-9]{4})', 1)
    $Array[0][1] = StringRegExp($Temp, '(Season [0-9]{1,2}, Episode [0-9]{1,4})', 1)
    _ArrayDisplay($Array)
WEnd

; Close Import File
$ImportTextFileMsg = FileClose($ImportTextFileHandle)

Regards

DG

Link to comment
Share on other sites

  • Moderators

Sorry - I forgot to include a code snippet in my haste.

Below is a snippet where I'm trying to populate an array of (for now) 2 columns, but I'm having no joy at all no matter how I change the code. Basically, for each line of text, there will eventually be output six columns of data, but it's the population of the array that's giving the headache (literally! :P ).

#include <array.au3>

Dim $Array[1][2]

Local $ImportTextFileHandle, $Temp
        
; Open Import File for Read
$ImportTextFileHandle = FileOpen("IMDb.txt", 0)

; Write File Data to Data Array
While 1
    $Temp = FileReadLine($ImportTextFileHandle)
    If @error = -1 Then ExitLoop
    MsgBox(0,"",$Temp); Display fileline
    $Array[0][0] = StringRegExp($Temp, '(?:year-)([0-9]{4})', 1)
    $Array[0][1] = StringRegExp($Temp, '(Season [0-9]{1,2}, Episode [0-9]{1,4})', 1)
    _ArrayDisplay($Array)
WEnd

; Close Import File
$ImportTextFileMsg = FileClose($ImportTextFileHandle)

Regards

DG

I still don't get it... Why 6 times... please answer the 3 things above I posted.... more detail more detail more detail lol...

You could loop that for infinity and the answer is always going to be the last result of the loop itself. You don't increment your array in order to take into account each loop.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

I still don't get it... Why 6 times... please answer the 3 things above I posted.... more detail more detail more detail lol...

You could loop that for infinity and the answer is always going to be the last result of the loop itself. You don't increment your array in order to take into account each loop.

There are six pieces of data held in the text file line which I wish to extract.

And, at the moment, the text file only contains 1 line, as the code is 'proof of concept' at the moment, so I haven't built in the looping yet :P

I've never had to use arrays before and the problem I'm having is populating the correct column with the data returned by the StringRegExp function - nothing I try seems to work :P

Regards

DG

Link to comment
Share on other sites

  • Moderators

There are six pieces of data held in the text file line which I wish to extract.

And, at the moment, the text file only contains 1 line, as the code is 'proof of concept' at the moment, so I haven't built in the looping yet :P

I've never had to use arrays before and the problem I'm having is populating the correct column with the data returned by the StringRegExp function - nothing I try seems to work :P

Regards

DG

#include <array.au3>
Local $s_string = FileRead("IMDb.txt")
Local $a_sre = StringRegExp($s_string, "(?i)<a name=.year-(\d+).+?(Season.+?):", 3)
Local $a_array[((UBound($a_sre) + 1) / 2) + 1][2], $i_add = 0
$a_array[0][0] = UBound($a_array, 1) - 1

For $i = 0 To $a_array[0][0] Step 2
    $i_add += 1
    $a_array[$i_add][0] = $a_sre[$i]
    $a_array[$i_add][1] = $a_sre[$i + 1]
Next
_ArrayDisplay($a_array)

$a_array[0][0] will have the total number found [1][0] = year [1][1] = Season/Episode, the element for the data starts at 1 for the first index.

Edit:

You'll also only need to read the file once this way.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

#include <array.au3>
Local $s_string = FileRead("IMDb.txt")
Local $a_sre = StringRegExp($s_string, "(?i)<a name=.year-(\d+).+?(Season.+?):", 3)
Local $a_array[((UBound($a_sre) + 1) / 2) + 1][2], $i_add = 0
$a_array[0][0] = UBound($a_array, 1) - 1

For $i = 0 To $a_array[0][0] Step 2
    $i_add += 1
    $a_array[$i_add][0] = $a_sre[$i]
    $a_array[$i_add][1] = $a_sre[$i + 1]
Next
_ArrayDisplay($a_array)

$a_array[0][0] will have the total number found [1][0] = year [1][1] = Season/Episode, the element for the data starts at 1 for the first index.

Edit:

You'll also only need to read the file once this way.

Hi

Many thanks for your work on this, but I get an error when I run it:

9) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$a_array[$i_add][0] = $a_sre[$i]

^ ERROR

->18:33:15 AutoIT3.exe ended.rc:1

>Exit code: 1 Time: 1.694

What does this mean?

Regards

DG

Link to comment
Share on other sites

  • Moderators

Hi

Many thanks for your work on this, but I get an error when I run it:

What does this mean?

Regards

DG

There's no error checking to see if regex actually found anything.

Upload the actual text file so I can see the issue.

For future help: http://www.autoitscript.com/wiki/Arrays

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

There's no error checking to see if regex actually found anything.

Upload the actual text file so I can see the issue.

For future help: http://www.autoitscript.com/wiki/Arrays

I get an 'Upload failed. Please ask the administrator to check the settings and permissions' error when trying to upload the file.

But the one line of data is this:

<a name="year-1977"> </a><h4>Season 1, Episode 1: <a href="/title/tt0501326/">Creed of Slaves</a></h4><b>Original Air Date: 18 September 1977</b><br> "Necessity is the plea for every infringement of human freedom. It is the argument of tyrants. It is the creed of slaves" (William Pitt). Home Affairs correspondent Jim Kyle, a journalist for one of Britain's three remaining newspapers, provides assistance to a doctor struggling to help his asthmatic daughter leave the United Kingdom. His endeavours bring him into close contact with the Public Control Department and its tools of bureaucratic repression.<br/> <br/>

And many thanks for the link - it's exactly what I was looking for, but somehow missed. I'm reading it now :P

Thanks again.

DG

Link to comment
Share on other sites

  • Moderators

I get an 'Upload failed. Please ask the administrator to check the settings and permissions' error when trying to upload the file.

But the one line of data is this:

And many thanks for the link - it's exactly what I was looking for, but somehow missed. I'm reading it now :P

Thanks again.

DG

Try to find an alternate place to upload to then link here. The reason I'm asking is because this obviously isn't the issue, and what I originally wrote works for what you asked for:
#include <array.au3>
Local $s_temp = '<a name="year-1977"> </a><h4>Season 1, Episode 1: <a href="/title/tt0501326/">Creed of Slaves</a></h4><b>Original Air Date: 18 September 1977</b><br> ' & _
                '"Necessity is the plea for every infringement of human freedom. It is the argument of tyrants. It is the creed of slaves" (William Pitt). Home Affairs ' & _
                "correspondent Jim Kyle, a journalist for one of Britain's three remaining newspapers, provides assistance to a doctor struggling to help his asthmatic " & _
                "daughter leave the United Kingdom. His endeavours bring him into close contact with the Public Control Department and its tools of bureaucratic repression.<br/> <br/>'"

Local $s_string = $s_temp;FileRead("IMDb.txt")
Local $a_sre = StringRegExp($s_string, "(?i)<a name=.year-(\d+).+?(Season.+?):", 3)
Local $a_array[((UBound($a_sre) + 1) / 2) + 1][2], $i_add = 0
$a_array[0][0] = UBound($a_array, 1) - 1

For $i = 0 To $a_array[0][0] Step 2
    $i_add += 1
    $a_array[$i_add][0] = $a_sre[$i]
    $a_array[$i_add][1] = $a_sre[$i + 1]
Next
_ArrayDisplay($a_array)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...