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

slicing a complex array

Hiya!

I would like to take a partial slice of a fairly complex array. The
source looks something like

array
(
[Full Name] ==array
(
[p_id] ==NN
[field1] ==blah
[fieldN] ==blah
[numbers] ==array
{
[800-555-1212] =="cell"
[123-456-7890] =="home"
]
[emails] ==array
(
[em***@add.re.ss] ==>
[e.****@addr.ess] ==>
)
)
)

for many fields 1-N across many people. I want to end up with an
array like

array
(
[Full Name] ==array
(
[display] =="Full Name"
[link] =="http://example.com/p_id=NN"
)
)

combining some pieces from the source array and adding other pieces
either constructed (eg link) or static (maybe).

Is there an easy way to do this across my entire source array without
just looping over it myself?

Why would I do this, you ask? Well, I have to print a list a bunch of
times and I am tired of rewriting the code :-) I'll need to be able
to specify display value, link target, and/or form field name for
starters; a separate array will control how to present the data so
that I can end up with anything from a space-separated list to a row
in a table to a bunch of checkboxes or radio buttons and so on. I'm
certainly open to simpler ways to accomplish the same thing, but a
function that takes data and control arrays and handles the
presentation certainly seems nice at the moment :-)
TIA & HAND

P.S. -- This is my first post-by-google, so I'm interested in seeing
how the formatting comes out. Wish me luck :-)

:-D
--
David T-G
see http://justpickone.org/email/

Sep 18 '07 #1
2 1589
On 18.09.2007 06:29 David TG wrote:
Hiya!

I would like to take a partial slice of a fairly complex array. The
source looks something like

array
(
[Full Name] ==array
(
[p_id] ==NN
[field1] ==blah
[fieldN] ==blah
[numbers] ==array
{
[800-555-1212] =="cell"
[123-456-7890] =="home"
]
[emails] ==array
(
[em***@add.re.ss] ==>
[e.****@addr.ess] ==>
)
)
)

for many fields 1-N across many people. I want to end up with an
array like

array
(
[Full Name] ==array
(
[display] =="Full Name"
[link] =="http://example.com/p_id=NN"
)
)

combining some pieces from the source array and adding other pieces
either constructed (eg link) or static (maybe).

Is there an easy way to do this across my entire source array without
just looping over it myself?
There's apparently no built-in function that can handle this. If, for
some reason, you're unhappy with "foreach", you can try array_map() or
array_walk(). The modern OO approach would be to use an iterator,
perhaps even different ones for different purposes.

class DisplayIterator implements Iterator
{
function __construct($ary) {
$this->ary = $ary;
....
function next() {
// get next element and format it how you want
}

....

foreach(new DisplayIterator($yourArray) as $element)
// do whatever with $element
>
Why would I do this, you ask? Well, I have to print a list a bunch of
times and I am tired of rewriting the code :-) I'll need to be able
to specify display value, link target, and/or form field name for
starters; a separate array will control how to present the data so
that I can end up with anything from a space-separated list to a row
in a table to a bunch of checkboxes or radio buttons and so on. I'm
certainly open to simpler ways to accomplish the same thing, but a
function that takes data and control arrays and handles the
presentation certainly seems nice at the moment :-)
TIA & HAND

P.S. -- This is my first post-by-google, so I'm interested in seeing
how the formatting comes out. Wish me luck :-)

:-D
--
David T-G
see http://justpickone.org/email/
The formatting is ok, but you should add a space after "--".
--
gosha bine

makrell ~ http://www.tagarga.com/blok/makrell
php done right ;) http://code.google.com/p/pihipi
Sep 18 '07 #2
gosha, et al --

gosha bine wrote:
On 18.09.2007 06:29 David TG wrote:

I would like to take a partial slice of a fairly complex array. The
source looks something like
....
Is there an easy way to do this across my entire source array without
just looping over it myself?

There's apparently no built-in function that can handle this. If, for
some reason, you're unhappy with "foreach", you can try array_map() or
Ahhhh... Those both sound familiar; I'll check 'em out.

array_walk(). The modern OO approach would be to use an iterator,
perhaps even different ones for different purposes.
[snip]

Oho! Thanks; I'll have to play with that.

>
....

P.S. -- This is my first post-by-google, so I'm interested in seeing
how the formatting comes out. Wish me luck :-)
....
>
The formatting is ok, but you should add a space after "--".
Heh. That's a web form for you. It was there originally :-/

I also noticed lots of extra linefeeds in my post. I'm not sure
whether I like 'em or not, as wrapping lines is a good thing but not
doing it where I want gets on my nerves.
Thanks again & HAND

:-D
--
David T-G

Sep 18 '07 #3

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

Similar topics

2
by: Yun Mao | last post by:
Hi python gurus, I have some questions when I'm using python numeric: 1. When I do v = u, it seems u and v still point to the same memory. e.g. When I do v=0, u will be zero out as well....
54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
3
by: Bryan Olson | last post by:
I recently wrote a module supporting value-shared slicing. I don't know if this functionality already existed somewhere, but I think it's useful enough that other Pythoners might want it, so here...
11
by: jbperez808 | last post by:
>>> rs='AUGCUAGACGUGGAGUAG' >>> rs='GAG' Traceback (most recent call last): File "<pyshell#119>", line 1, in ? rs='GAG' TypeError: object doesn't support slice assignment You can't assign to...
4
by: jeg | last post by:
dear all, i'm an astronomer working with 2d images -- 2d numarrays. i have a script which basically does some operations on some images, and one of the first steps is to find a galaxy on an...
17
by: baibaichen | last post by:
i have written some code to verify how to disable slicing copy according C++ Gotchas item 30 the follow is my class hierarchy, and note that B is abstract class!! class B { public: explicit...
2
by: jccorreu | last post by:
I'm taking in data from multiple files that represents complex numbers for charateristics of different elements. I begin with arrays of doubles and interpolate to get standard values in real and...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
16
by: Yves Dorfsman | last post by:
Is there a way to do: x = x That would return: I am surprised this notation is not supported, it seems intuitive. A concrete example of the sort of thing I want to do:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.