473,387 Members | 1,876 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,387 software developers and data experts.

Best way to "switch arrays"

I'm defining several arrays in the following format:

$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')

I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.

Ideally, I may even have a different set of elements and then need to
create a variable on the fly to match the element name.

Can someone suggest the best method for changing the array to use,
without having to script a different loop for each section of the
file I'm stepping through?

Thanks.

J
Jun 27 '08 #1
4 1449
javelin wrote:
I'm defining several arrays in the following format:

$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')

I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.
How do you "detect" the section?
You could have an array of arrays, keyed by a section name:
$animals['farm'] = array('horse', 'cow', 'chicken');
$animals['pets'] = array('dog', 'cat', 'bird');
$animals['zoo'] = array('monkey', 'lion', 'zebra'. 'giraffe');
Jun 27 '08 #2
javelin schrieb:
I'm defining several arrays in the following format:

$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')

I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.

Ideally, I may even have a different set of elements and then need to
create a variable on the fly to match the element name.

Can someone suggest the best method for changing the array to use,
without having to script a different loop for each section of the
file I'm stepping through?
I think, you know in wich section you are?

$actualarray = $farmanimals; // or $hose... or $zoo...
Now work with the $actualarray.

Jun 27 '08 #3
On Jun 13, 3:46 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
javelin wrote:
I'm defining several arrays in the following format:
$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')
I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.

How do you "detect" the section?
You could have an array of arrays, keyed by a section name:
$animals['farm'] = array('horse', 'cow', 'chicken');
$animals['pets'] = array('dog', 'cat', 'bird');
$animals['zoo'] = array('monkey', 'lion', 'zebra'. 'giraffe');
The file will have a header line at the start of each section. I check
for the section, then I want to "switch" to the next array
dynamically.

The code looks like this at this point, hopefully this will help:

$file = fopen('sampledata1.txt','r');
while (!feof($file)) {//step thru each line in file
$nextline = fgets($file, $currentArray['linelength']); //using the
currentArray
echo $nextline . "<br>";
if (strpos($nextline,"section2")) {
# somehow, change the $currentdata array to use the next
one in line. Need to use the
# same name, "currentdata", for the array name.
}
foreach ($currentdata as $col =$len){
# step thru the array elements, each which defines
the length of the next portion of the line
print substr(startofline, $currentdata[$len]) ."<br>";
}

}
Jun 27 '08 #4
On Jun 14, 7:37 am, javelin <google.1.jvm...@spamgourmet.comwrote:
On Jun 13, 3:46 pm, "Paul Lautman" <paul.laut...@btinternet.com>
wrote:
javelin wrote:
I'm defining several arrays in the following format:
$farmanimals = array('farmanimal1' ='horse', 'farmanimal2' ='cow',
'farmanimal3' ='chicken')
$housepets = array('housepet1' ='dog', 'housepet2' ='cat',
'housepet3' ='bird')
$zooanimals = array('zooanimal1' ='monkey', 'zooanimal2' ='lion',
'zooanimal3' ='zebra', 'zooanimal4' ='giraffe')
I'm then running through a long text file, and detecting what part of
the file I'm in. If I'm in the farm animals section, I want to use the
$farmanimals array; if I'm in the house pets section, use the
$housepets arra, and so on.
How do you "detect" the section?
You could have an array of arrays, keyed by a section name:
$animals['farm'] = array('horse', 'cow', 'chicken');
$animals['pets'] = array('dog', 'cat', 'bird');
$animals['zoo'] = array('monkey', 'lion', 'zebra'. 'giraffe');

The file will have a header line at the start of each section. I check
for the section, then I want to "switch" to the next array
dynamically.

The code looks like this at this point, hopefully this will help:

$file = fopen('sampledata1.txt','r');
while (!feof($file)) {//step thru each line in file
$nextline = fgets($file, $currentArray['linelength']); //using the
currentArray
echo $nextline . "<br>";
if (strpos($nextline,"section2")) {
# somehow, change the $currentdata array to use the next
one in line. Need to use the
# same name, "currentdata", for the array name.
}
foreach ($currentdata as $col =$len){
# step thru the array elements, each which defines
the length of the next portion of the line
print substr(startofline, $currentdata[$len]) ."<br>";
}

}
Well, I realized that the best way to deal with this is creating an
array of the arrays, returning it as a function. Then, I can easily
change arrays by referring to the index (I gave each array rec an
associated number that I can easily relate to my data).

Thanks for the help.
J
Jun 27 '08 #5

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

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
12
by: junky_fellow | last post by:
Which is better using a switch statement or the if-then equivalent of switch ?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.