Explode from a txt file - arrays

I use explode in my php code to load the strings from a txt file into an array. The strings are loaded into array with no problem, however, the first element has an index 0 but I want it to have an index 1. How do I achieve that?
I appreciate any help cause I've tried so many things and nothing seems to be working and I feel I got stuck.

You can use array_unshift to insert dummy element then remove it using unset as follows :
$ex_array = array("PHP","JAVA","C#","Python", "Javascript", "C");
array_unshift($ex_array,"");
unset($ex_array[0]);

Related

Why can't I use the Match method on an array in ruby?

I need to iterate and parse 108 lines from a file and then sort them into 3 different hashes. (In one iterator.) (In Ruby)
I have the file loaded into the program and into the array I need to parse. When I try to make the iterator anyway I try to use the Regex Match command I get an error abut the unknown method. Is as simple that I can't use that method on a array?
lines = File.readlines('access_log')
lines.each.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/)
This and every other way I have tried to use the match method it hasn't worked.
This also doesn't anything for the hash, as I haven't done that yet.
/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/.match(lines)
Have also tried this, but the error output appears that you cant run it on only the array. I beilive this is where I would need to tie the iterator in, but I'm stumped.
So, what's happening is that what readlines does is it slurps the entire text file.
So you have an array with the content of the textfile separated by a newline(and the newline is kept in every string in the array).
After that, you're doing lines.each, which brings out an enumerator. Then you're calling .match on the enumerator instead of the string itself
The proper way to do this would be
lines.each { |line| line.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/) }
However, the above actually won't do anything because all you're doing is iterating against each element and checking if it matches the REGEX.If you want it to actually do something, try...
matches = lines.map { |line| line.match(/^([:\d\.]+) .*\[.*\].*\"[A-Z]+ *(.+) HTTP/) }
Remember that the match method only works on strings. If match matches something, it returns an object of the class MatchData, else if it doesn't match anything-- nil.

TYPO3 findDemanded return an empty array

i'm struggling with a tricky problem.
I'm creating a new extension for typo3, this extension need to retrive all the news matching the demand-array.
Actually my code looks like this:
$demand = $this->objectManager->get(NewsDemand::class);
$demand->setActionAndClass(__METHOD__, __CLASS__);
$demand->setStoragePage(18);
$res = $this->findDemanded($demand);
the main problem is that
$res->count()
this code retrieve correctly the number of news matching the array but if i try with
$res->getFirst()
this return nothing.
In theory the code $this->findDemanded($demand); will return an array with all the news matching the demand-array but the results is empty.
If i use $this->findDemandedRaw($demand); it returns the correct plain query (tested in phpmyadmin and returns the correct values).
I'm pretty new on TYPO3 and i don't know how it works really well, some one has any hints about that?
The news extension key is: news
Thanks in advance!
Fixed:
The returned value was an object not an array, so now i'm able to iterate every item inside the object and return it as a json.
My bad, sorry :)

Array (class) filled with non nil values stays empty

I am currently having trouble filling up an array of customClass.
I try to fill it with a jsonFile. During my json parsing (using swiftyJSON) i loop and fill my array.
The problem is, at the end of my loop, it is still empty. I tested it in different ways, and here is my code:
That's the file where the problem is. In my loop I fill an Annotation, that I add with append to my array. The problem is what my print return. Here is a part of it:
It's just a small part of a huge jsonfile. And, my tmpAnnot.name is correctly printed every iteration. But when it comes to my Array, nothing.
So I'm completly lost and hope you could help me ^^
(And for the information, here is my custom class) :
And btw, I tried to print my array.count, and it's nil too
Im so sorry if the question has been posted. I couldn't find it in the entire website.
Change your JSONAnnotationList declaration to be an non-optional and assign it an empty array
var JSONAnnotationList: [UGOAnnotation] = []
You see, you have never created an array so there was nothing to be printed.
The whole point of optionals is to use them sparingly, not everywhere.

Perl: Reading from file and saving into an array

I had got a script in Perl and my task is to do some changes in it. This of course means to understand which part does exactly what. I am not familiar with Perl language but I tried to read through some tutorials, but still some things are confusing me. And I got stuck in following part:
while (<KOEFICIENTYfile>) {
#_=(split(/\s+/, $_));
push(#ZAID,shift(#_));
$KOEFICIENTY{$ZAID[-1]}=[#_];
}
As I understands this part then it:
Reads line from filehandle KOEFICIENTYfile
Separates them by spaces (one or more)
Loads first item from this separated array into array ZAID (and in the process, removes it from #_)
??? Adds a rest of a separated array into array KOEFICIENTY? I am confused by curly brackets part and by square brackets after equals sign.
I think that I understood the meaning of #, $, #_ or negative indexing but this is beyond me. Can you please advice me on meaning of this?
[-1] indexing is just a shortcut way to say "last element of the array".
KOEFICIENTY is actually a hash (you can tell this because it is using curly braces, instead of square ones, around the index), so you're putting the rest of the array #_ into a hash called KOEFICIENTY with a key of the last element of the array.
If you include:
use Data::Dumper
at the top of the script and do
print Dumper(%KOEFICIENTY)
it will nicely format the output for you, which may help
The original coder was trying to be too clever using the negative offset. It would have been much more obvious if the code had been written with a simple temporary variable thus:
while (<KOEFICIENTYfile>) {
#_ = (split(/\s+/, $_));
my $key = shift(#_);
push(#ZAID, $key);
$KOEFICIENTY{$key} = [#_];
}
The braces on $KOEFICIENTY show that this is a "hash" of key/value pairs named %KOEFICIENTY, and not a normal array.
If you don't actually need to preserve the sort order of the keys you could just use keys %KOEFICIENTY to retrieve them instead of storing them in #ZAID.
#zaid is a list, into which the first part of the split is added.
%KOEFICIENTY is a hash, in which a reference to the rest of the split is stored as a list reference under the key of the first part.
So if the line is a b c, #zaid will get a and %KOEFICIENTY{'a'} will hold a reference to a list containing b and c.

Pymongo iteratively safe saving and counting

I'm trying to pick out unique elements from a collection of objects and then save them to a sub collection.
The code I have is:
for item in db.col1.find({'Summary': {'$ne':{}}}):
current_specs = item['Summary']['Specs']
if not db.col1.specs.find({'Specs':current_specs}).count():
db.col1.specs.save({'Specs':current_specs, 'Updated': datetime.datetime.now()},safe=True)
This produces duplicate entries within db.col1.specs. I thought by using safe=True, it would make sure the write was completed and hence duplicates would not be added but this does not seem to be the case.
Can anyone explain why this is failing and the correct way of doing it?
Cheers
Figured it out:
the item['Summary']['Specs'] entry is itself a dictionary and to search for dictionaries you have to search for each individual dictionary entry
e.g. if the object looks like:
current_specs = {'field1': something, 'field2': something_else}
then we can find it via:
find({'Specs.field1':current_specs[field1], 'Specs.field2': current_specs[field2]})
you can't just use:
find({'Specs':current_specs})

Resources