A long boring comment which has nothing to do with the script
|
|
|
Excepting sort and reverse which you can do to an Array by one single statement such as:
Array.reverse()
everything else you may want to do to an Array is here, in one single function, which can also make valid, logic guesses in case you pass conflicting arguments; all you have to do is to copy the code and to invoke it following the detailed guidelines as outlined below for each type of task you safely want your array to undergo.
To some degree, it is suicide leading a web site like this, they say to me.
Ya know, I cover a space which is (strangely enough?) uncovered: power (java)scripting; and you can spell each single letter of it.
Every time you go to a javaScript site you mostly (mostly, not always. For a few are exceptionally good indeed. For instance one I love is Dynamic Drive, but you see: that outstanding, classy, remarkable work is the work of a team. Whereas what you see here is one man's efforts and sweat) find silly scripts to fit the needs of the less demanding surfers.
And this for the less they are demanding the more they are.
And the more they are the more the hits.
And the more the hits the more the banners.
And the more the banners the more the money.
So most just cast up all the rest, for what else matters?
So they say: nobody's gonna check his website, for nobody wants power scripts. If they have to change an Array, they just want the tiny petty snippet which just does that, and jumble the patch worked webpage as soon as possible. Moreover, a power script would also need some attention, like reading the documentation (have you ever heard those complaints about scanty documentations, bud? I have, and they often come from those who provide scanty documentations as well!). Who does want culture nowadays, and who would ever believe the net is about culture and aesthetic experiences that can deliver ethic messages?
At time I spend weeks on one scripting riddle, cat fighting with it for days, scowling and howling while it grunts back at me and insidiously bare fanged snorts and bites slices of sleep off of me. And I do not give up until I am reasonably safe (reasonably, for whoever fights with programming languages can stumble into a bug any moment) it is polished and shiny.
And all for free.
Then when I'm finished and I've finally got my 5 minutes of fun picking up some quite relatively racy (and relatively quality) pic of a beautiful woman and match it with the page, there we go again: ohhh but that's audacious, what would the kids say? Well, kids do not use power scripts.
They wonder, what's this odd, unheard of mix of poetry, scripts, glitzy pics, paintings? What's this space where I can find Escher's illusionary with a Rembrandt's canvas a Garcia Lorca's poem a complex detailed script a scanty clod sexy Pamela Anderson a quotation from Kierkegaard and a Leonardo da Vinci's drawing, all invariably partying together?
My space is for folks that surfed the net before, and if we'd all have to make websites only for "everybody", I'd seriously rule out even the Cnn and the Fox, for some news are -so to say- fairly rancid to see/to read/to remember.
I believe the work that is here is not unworthy, and I will just go on for those who may understand or find an enriching meaning in it, or just cute sturdy scripts to lurk copy and paste, hopefully with a line of acknowledgement or just keeping the comment in them. No matter how few such folks are. Were it be only for the money, I'd have chosen robbing banks, instead of providing you with freeware scripts and documentations I burn my eyes out with.
Time to script: The Array Master
|
|
The code, and the main behaviour of the script
|
I'm gonna explain to you how to use this function task by task, dividing the subject in the six actions you may want an Array undergo. Just keep in mind that whereas you see an argument named replacements passed to the function, this means that from that argument (inclusive) on, you can pass whatever arbitrary amount of next arguments for the function would assume, for it knows it, that after the last argument allowed for that specified task, all the subsequent ones that may or may not be included after it, are to be considered as bound to be bundled with that.
Therefore the script would cumulate them all, folding them all into an unique array; and it will use this latest produced array as one of its internal arguments.
This means that the argument named replacements can be either an array or a set of linear argument passed separating them with commas and that the array master will tailor to its internal workings later on. Actually after argument replacements you can pass whatever type of arguments, even mixed, such as, say:
Number, String, String, Array, Number, Array, String ...
All would be going to be patched up in one single long array you're to see soon what it is for. It was meant to give to you flexibility.
The function is tailored to return a matrix; a bidimensional array, that is. Such returned array will be of two entries, each of them holding another array itself, this latest ones of an unpredictable amount of entries (depending on your input).
So if you, in a fake shell, call the function assigning it to a foofoo variable:
var foofoo=arrayMaster(myArgs,myReplacements);
To read the outcome later on, keep in mind that after such call foofoo is like this:
foofoo //an Array itself
foofoo //an Array itself
Each of those two objects is an Array, so for instance:
foofoo[0]
would be the index number 7 in the first Array (if it has such length),
The function is tailor'd to return such two entry array anyway, even if it cannot perform for you inserted erroneous data types or so; therefore I attempted to grant more compatibility and less chances to grip a wider process you may want to use this relatively mighty subroutine in.
You may guess that something went wrong if both those two entries carry arrays of zero length:
foofoo[0].==0 //or:
foofoo[1].==0
Alternatively, if the function can work, but with your data, although correct, it could not manipulate the input array (because, for instance, it didn't find in it the coordinates you suggested), then it would still return a two entry array, whereas the former entry is your unaltered input array as well, whereas the latter is an empty array. Therefore if you find that:
foofoo[0].>0 && foofoo[1].==0
Means that the function ran, but nothing changed.
Finally, if both entries have a length, the first entry is the manipulated input, the second entry is an attempt to provide you with the replaced, deleted, changed original items.
But for details or a few exceptions to these guidelines for each task, now, see the documentation.
Documentation and Test Form
|
|
Just click the Trigger Test buttons to get acquainted with this script.
|
|
All your test forms will use, in order to simplify, a standard input array as follows:
|
ADD: APPEND, PRE PEND, INCLUDE
ACTION 1 |
 |
 |
| Arguments: |
NOTES: |
- input: the Array to manipulate
- action: pass as 0 or 1
- from: offset to start with
- replacements: the entries to add (array or subsequent list of arguments. Accepts mixed data types, excluding Function data types).
Example ( foo is placeholder):
var foo=arrayMaster(anArray,, 5, REPLACEMENTS)
Then foo holds your main output as:
foo[0]
|
from: start counting with number zero, therefore the first entry of your array is 0.
It is inclusive: from=0 from that after on. If -1 pre pends. If equals input.length-1 or higher, appends the replacements.
In the form, you can change the replacements. They will be transformed into an Array taking as splitting argument a white space.
It returns as its second returned entry (say arrayMaster(...)[1] which is the returned snippet which holds the "remains of the day") the original input before it underwent the process.
In fact, remember: the arrayMaster returns an array of 2 entries; each of them is an Array: first the output, second what changed or what the input was before the change occurred.
|
|
REPLACEMENTS: »
FROM: »
MAIN RESULTS: arrayMaster( ... )[0]
|
the REMAINS of the day: arrayMaster( ... )[1]
|
SYNTAX INSPECTOR:
[0] or [1]
|
|
DELETE: SLICES
ACTION 2 |
 |
 |
| ARGUMENTS: |
NOTES: |
- input: the Array to manipulate
- action: pass as 2
- from: offset to start with
- until: offset to end with
Example ( foo is placeholder):
var foo=arrayMaster(anArray, 7, 7)
Then foo holds your main output as:
foo[0]
|
from/until: start counting with number zero, therefore the first entry of your array is 0.
It is inclusive: from=0 and until=0 as well, means grab and delete the entry in the index slot number zero, which's the first.
If from==-1 or equals input.length-1 or higher, it may either swap with until (for the from value should be lower than the destination value until, right?) if until is fit; if it is not, does nothing and just returns the input (as arrayMaster()[0] as usual...)..
Note that if from is minus than zero, it assumes you want to grab zero because it is a deleting process: if you could have meant to do nothing, you should have not run it at all in the first place, you see.
If both values are higher than the last available index, does nothing and returns the input (as arrayMaster()[0] as usual...).
If from higher than until, it swaps them.
In the form, you can change the replacements. They will be transformed into an Array taking as splitting argument a white space.
Remember that the arrayMaster returns an array of 2 entries; each of them is an Array: first the output, second what changed.
|
|
FROM: »
UNTIL: »
MAIN RESULTS: arrayMaster( ... )[0]
|
the REMAINS of the day: arrayMaster( ... )[1]
|
SYNTAX INSPECTOR:
[0] or [1]
|
|
REPLACE: SLICES
ACTION 3 |
 |
 |
| ARGUMENTS: |
NOTES: |
- input: the Array to manipulate
- action: pass as 3
- from: offset to start with
- until: offset to end with
- replacements: the array or entries which you want to overtake the ones in the input specified from/until positions with.
Example ( foo is placeholder):
var foo=arrayMaster(anArray, 7, 7, REPLACEMENTS)
Then foo holds your main output as:
foo[0]
|
from/until: start counting with number zero, therefore the first entry of your array is 0.
It is inclusive: from=0 and until=0 as well, means grab and replace the entry in the index slot number zero, which's the first.
If both from and until are too low (minus zero) it pre pends!
If both are too high, it appends!
If they are within boundaries, slices off the entries allocated in that slice and replaces them with the replacements.
If the replacements exceed the amount of sliced entries, it will just go on nesting them from the offset on, and will trail back the original ending left to this trial. Please check in the form... this is why these forms are here, to understand how to make your function work your way, the meant way.
If the replacements fall short of the prescribed slice, only an amount of entries of the original input that match with the provided replacements would be actually replaced, whereas the others will be kept in place.
(Note for the form: be careful with loitering white spaces: may produce false empty entries)
If from higher than until, it swaps them.
In the form, you can change the replacements. They will be transformed into an Array taking as splitting argument a white space.
Remember that the arrayMaster returns an array of 2 entries; each of them is an Array: first the output, second what changed.
|
|
REPLACEMENTS: »
FROM: »
UNTIL: »
MAIN RESULTS: arrayMaster( ... )[0]
|
the REMAINS of the day: arrayMaster( ... )[1]
|
SYNTAX INSPECTOR:
[0] or [1]
|
|
REPLACE: PINPOINTED GROUPS
ACTION 4 |
 |
 |
| ARGUMENTS: |
NOTES: |
- input: the Array to manipulate
- action: pass as 4
- from: ARRAY of numerical indexes of the entries to be replaced
- REPLACEMENTS: an array or list or mixed of replacements
Example ( foo is placeholder):
var foo=arrayMaster(anArray, ARRAY_Numbers, REPLACEMENTS)
Then foo holds your main output as:
foo[0]
|
from: this time it is mandatory it is an Array. It must hold entries which are numbers. If it finds not numbers, would skip that entry upon parsing. Each number pinpoints the index of the entry in the input array which is going to be replaced.
Next, you pass all your replacements objects, as usual either an array or a list or mixed: they will be switched with the corresponding entry.
For an instance, if array passed as from is:
new Array(3,6,7)
and replacements are, say:
new Array('new1', 'new2','new3')
then what will happen is:
input[3]='new1'
input[6]='new2'
input[7]='new3'
The arrayMaster would return as [0] the input Array with replaced objects, and as [1] the object which have been eliminated, moreover even in the order the from argument dictated!
If the indexes passed into the from array are either lower than zero or beyond the last available index in the input array, they won't be replaced for such indexes do not exist in the original.
To simplify I provide only 4 replacements, ok? If you leave one empty (neither white space, careful, or you'd see a false empty entry actually carrying an... empty space), it won't be added (so you can simulate replacements inferior to the provided indexes if you want to test...)
Remember that the arrayMaster returns an array of 2 entries; each of them is an Array: first the output, second what changed.
|
MAIN RESULTS: arrayMaster( ... )[0]
|
the REMAINS of the day: arrayMaster( ... )[1]
|
SYNTAX INSPECTOR:
[0] or [1]
|
|
DELETE: PINPOINTED GROUPS
ACTION 5 |
 |
 |
| ARGUMENTS: |
NOTES: |
- input: the Array to manipulate
- action: pass as 5
- from: ARRAY of numerical indexes of the entries to be deleted
Example ( foo is placeholder):
var foo=arrayMaster(anArray, ARRAY_Numbers)
Then foo holds your main output as:
foo[0]
|
from: this time it is mandatory it is an Array. It must hold entries which are numbers. If it finds not numbers, would skip that entry upon parsing. Each number pinpoints the index of the entry in the input array which is going to be deleted.
If you replace twice or more the same index, the second returned entry (say foo[1]) of the arrayMaster will carry the replaced item as many times as you repeatedly replaced it (useless a feature at least as much as rewriting thrice the same thing with the same thing is!).
Also, the remains order will even reproduce the order you dictated in your from array of indexes!
To simplify I provide only 4 replacement indexes, ok?
Remember that the arrayMaster returns an array of 2 entries; each of them is an Array: first the output, second what changed.
|
From Index
|
From Index
|
From Index
|
From Index
|
MAIN RESULTS: arrayMaster( ... )[0]
|
the REMAINS of the day: arrayMaster( ... )[1]
|
SYNTAX INSPECTOR:
[0] or [1]
|
|
SWAP: GROUPS OR SINGLE
ACTION 6 |
 |
 |
| ARGUMENTS: |
NOTES: |
- input: the Array to manipulate
- action: pass as 6
- from: ARRAY of numerical indexes of the input entries to be swapped with the others (until)
- until: ARRAY of numerical indexes of the entries to be swapped with the others (from)
Example ( foo is placeholder):
var foo=arrayMaster(anArray, ARRAY_Numbers, ARRAY_Numbers)
Then foo holds your main output as:
foo[0]
|
from/until: this time it is mandatory they are both Arrays (actually until behaves as replacements, but I do not want you to feel dizzy: pass it as an array as well).
Both from & until arrays must hold entries which are numbers. If it finds not numbers, would skip that entry upon parsing. Each number in from pinpoints the index of the entry in the input array which is going to be swapped with the one pinpointed by the entry in until whcih is at the corrisponding level.
until rules: if its Array is shorter than the from Array, the process would go on only as far as until entries are available, ignoring possible further entries in from.
Of course, beware: you may perform inconsistent swappings, like swappings that swap twice the same items, or swappings that overlap.
Also, each new swapping swaps on the last modified conformation. So if you swap indexes [0,1] on input [A,B,C] and so you get [B,A,C]
and now you swap back [0,1], this latest swap will not go on swapping on input [A,B.C] but on the already swapped previous settings: [B,A,C]; this is the last saved conformation. Therefore it "correctly" restores [A,B,C]. I chose this, otherwise two consecutive swaps like [0,1] and again [0,1] would do the same thing; such an option would not have been completely illogical to support: would have just gone on insisiting on the input, which couldn't be considered a bug. None the less I deemed more logical not to opt for that; the alternative appears more reasonable even if it was terribly tricky to spot it!
[ email a bug if you deem you found one ]
The second returned entry (say the usual arrayMaster[1]) returns the original array as it was before undergoing the swapping process, or an empty array as second item returned if no swap was processed.
In case either of the from or until indexes are beyond whichever edge of the input dimensions, or are just non numerical, they would be ignored and no swap at that level performed, for bad indexes could swap nothing in an Array.
To simplify I provide only 2 couples of swappings, ok?
Remember that the arrayMaster returns an array of 2 entries; each of them is an Array: first the output, second what changed.
Read the indexes to swap by columns, not by rows.
|
SWAP INDEX:
|
SWAP INDEX:
|
WITH THIS INDEX:
|
WITH THIS INDEX:
|
MAIN RESULTS: arrayMaster( ... )[0]
|
the REMAINS of the day: arrayMaster( ... )[1]
|
SYNTAX INSPECTOR:
[0] or [1]
|
|
|