473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Array index question

I have source information in a file, and (part of) this information comes as
coordinates.
A coordinate consists of a letter followed by a number, ranging from a1 to
i9
I'd like to count the number of occurrences of each coordinate, and put them
in a matrix.
The matrix should look like this (metasyntax):

matrix = array ['a'..'i'][1..9]

Later on in the script I'd like to be able to refer to a cell in the matrix
by using the same letter/number combo.
I know I can define an array using strings as keys, but how can I define
a..i as keys without the need to specify them all individually?
TIA
Pjotr

Jul 17 '05 #1
4 1866
.oO(Pjotr Wedersteers)
I have source information in a file, and (part of) this information comes as
coordinates.
A coordinate consists of a letter followed by a number, ranging from a1 to
i9
I'd like to count the number of occurrences of each coordinate, and put them
in a matrix.
The matrix should look like this (metasyntax):

matrix = array ['a'..'i'][1..9]

Later on in the script I'd like to be able to refer to a cell in the matrix
by using the same letter/number combo.
I know I can define an array using strings as keys, but how can I define
a..i as keys without the need to specify them all individually?


For example in a loop. This creates the empty matrix:

$matrix = array();
for ($i = ord('a'); $i <= ord('i'); $i++) {
$matrix[chr($i)] = array_fill(1, 9, 0);
}

or

$matrix = array();
foreach (range('a', 'i') as $i) {
$matrix[$i] = array_fill(1, 9, 0);
}

Then you can access the elements like this:

$coord = 'i9';
$matrix[$coord{0}][$coord{1}]++;
HTH
Micha
Jul 17 '05 #2
"Pjotr Wedersteers" wrote:
I have source information in a file, and (part of) this information
comes as
coordinates.
A coordinate consists of a letter followed by a number, ranging from a1 to
i9
I’d like to count the number of occurrences of each coordinate,
and put them
in a matrix.
The matrix should look like this (metasyntax):

matrix = array [’a’..’i’][1..9]

Later on in the script I’d like to be able to refer to a cell in
the matrix
by using the same letter/number combo.
I know I can define an array using strings as keys, but how can I
define
a..i as keys without the need to specify them all individually?
TIA
Pjotr


Hope this helps.

Read the coordinates from the file. I assume after you read then, they
are going to be in an array called $coor, where $coord would have
values like ’a1’ , ’b5’, etc.

Then do this:
foreach ($coord as $line) {
$alpha = preg_replace("/\d/", ’’, $line); //leaves only the
alpha part
$num = preg_replace("/\D/", ’’, $line); //leaves the numeric
part only
$newcoord[$alpha][$num] += 1; //increment count

}

You now have the counts for each coordinate combination.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-Array-in...ict131894.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=440194
Jul 17 '05 #3
steve wrote:
"Pjotr Wedersteers" wrote:
> I have source information in a file, and (part of) this information
> comes as
> coordinates.
> A coordinate consists of a letter followed by a number, ranging

from
> a1 to
> i9
> I'd like to count the number of occurrences of each coordinate,
> and put them
> in a matrix.
> The matrix should look like this (metasyntax):
>
> matrix = array ['a'..'i'][1..9]
>
> Later on in the script I'd like to be able to refer to a cell in
> the matrix
> by using the same letter/number combo.
> I know I can define an array using strings as keys, but how can I
> define
> a..i as keys without the need to specify them all individually?
> TIA
> Pjotr


Hope this helps.

Read the coordinates from the file. I assume after you read then, they
are going to be in an array called $coor, where $coord would have
values like 'a1' , 'b5', etc.

Then do this:
foreach ($coord as $line) {
$alpha = preg_replace("/\d/", '', $line); //leaves only the
alpha part
$num = preg_replace("/\D/", '', $line); //leaves the numeric
part only
$newcoord[$alpha][$num] += 1; //increment count

}

You now have the counts for each coordinate combination.


Now that's a neat solution. Elegant too! Thanks a lot! Good thinking.
Pjotr
Jul 17 '05 #4
Michael Fesser wrote:
.oO(Pjotr Wedersteers)
I have source information in a file, and (part of) this information
comes as coordinates.
A coordinate consists of a letter followed by a number, ranging from
a1 to i9
I'd like to count the number of occurrences of each coordinate, and
put them in a matrix.
The matrix should look like this (metasyntax):

matrix = array ['a'..'i'][1..9]

Later on in the script I'd like to be able to refer to a cell in the
matrix by using the same letter/number combo.
I know I can define an array using strings as keys, but how can I
define a..i as keys without the need to specify them all
individually?


For example in a loop. This creates the empty matrix:

$matrix = array();
for ($i = ord('a'); $i <= ord('i'); $i++) {
$matrix[chr($i)] = array_fill(1, 9, 0);
}

or

$matrix = array();
foreach (range('a', 'i') as $i) {
$matrix[$i] = array_fill(1, 9, 0);
}

Then you can access the elements like this:

$coord = 'i9';
$matrix[$coord{0}][$coord{1}]++;
HTH
Micha


Okay, thanks Micha. This will work. The range solution seems best suited for
me here. !
Pjotr
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
4
by: James | last post by:
Just learning C#. What's the easiest way to assign numbers 1-10 randomly to an array? Basically I just want to take the numbers 1-10 and arrange them randomly in slots 0-9 of an array. Thanks
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
5
by: Peter Nofelt | last post by:
Hi all, My scenario is as follows: * Receive a base 1 array (index starts at 1 instead of 0) of data from a COM component . * Need to pass this array to a .net component that requires its...
5
by: Jan Smith | last post by:
I've searched the overloads for the Array.Sort method, and I haven't found a clear answer to my question. Maybe it's not in Array.Sort. Here's the question: I initialize an array X with the...
2
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of...
3
by: uche | last post by:
Please give me some feed back on this issue: Here is the complier error: hexdmp.cpp: In function `void output(unsigned char, int, bool&)': hexdmp.cpp:133: error: invalid types `unsigned char'...
3
by: =?Utf-8?B?UmljY2FyZG8=?= | last post by:
What is the best method to compare 2 array to knw if each element is equal? Now, i have to compare the datarow.itemarray of 2 datarows wich is equals in structure. I tried to write this routine:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.