This question already has an answer here:
How to pass an array as an argument list
(1 answer)
Closed 10 months ago.
I have an array stored in a constant like this:
FIELDS = ["first_name", "last_name", "occupation"]
I need to remove the square brackets only, it will look like this:
"first_name", "last_name", "occupation"
Does anyone have any ideas?
Thanks. :-)
For a little more context:
I have a complicated hash where I need to grab specific values from. My idea was to have the key of each value stored as an array, so I could then so
hash.values_at("first_name", "last_name", "occupation")
But that won't work with the square brackets from the array, hence my question!
I may be going around this the wrong way, however!
hash.values_at(*FIELDS)
The asterisk is called the splat operator. Doc
Edit: Now that you've added some context to your question, I see you'd like to do something quite different.
Here is how you would iterate (loop) over a list of key hashes to get their values:
EXAMPLE_HASH = { 'first_name' => 'Jane', 'last_name' => 'Doe', 'occupation' => 'Developer', 'other_key' => 'Dont return this' }.freeze
FIELDS = ['first_name', 'last_name', 'occupation'].freeze # Actually keys.
results = FIELDS.map{ |key|
EXAMPLE_HASH[key]
}
=> ["Jane", "Doe", "Developer"]
Or more succinctly:
EXAMPLE_HASH = { 'first_name' => 'Jane', 'last_name' => 'Doe', 'occupation' => 'Developer', 'other_key' => 'Dont return this' }.freeze
FIELDS = ['first_name', 'last_name', 'occupation'].freeze # Actually keys.
results = EXAMPLE_HASH.values_at(*FIELDS)
=> ["Jane", "Doe", "Developer"]
This is a very odd thing to do as the array doesn't actually have those square brackets in reality, it's just how the array prints out to give an observer the clue that it is any array.
So you can't ever actually "remove" those square brackets from an array, since they aren't there to begin with.
You could potentially override the to_s (and inspect?) methods on the Array class so it would print out differently.
But assuming you want a string as the output instead, this would accomplish the task:
FILEDS = ["first_name", "last_name", "occupation"]
fields_as_string = "\"#{FILEDS.join('", "')}\""
=> "\"first_name\", \"last_name\", \"occupation\""
However, it's not clear if you want a string as the output, nor is it clear if you're happy with the double-quotes being escaped in the string.
Related
I'm a novice perl programmer trying to identify which elements are in one array of hashes but not in another. I'm trying to search through the "new" array, identifying the id, title, and created elements that don't exist from the "old" array.
I believe I have it working with a set of basic for() loops, but I'd like to do it more efficiently. This only came after having tried to use grep() and failed.
These arrays are built from a database as such:
use DBI;
use strict;
use Data::Dumper;
use Array::Utils qw(:all);
sub db_connect_new();
sub db_disconnect_new($);
sub db_connect_old();
sub db_disconnect_old($);
my $dbh_old = db_connect_old();
my $dbh_new = db_connect_new();
# get complete list of articles on each host first (Joomla! system)
my $sql_old = "select id,title,created from mos_content;";
my $sql_new = "select id,title,created from xugc_content;";
my $sth_old = $dbh_old->prepare($sql_old);
my $sth_new = $dbh_new->prepare($sql_new);
$sth_old->execute();
$sth_new->execute();
my $ref_old;
my $ref_new;
while ($ref_old = $sth_old->fetchrow_hashref()) {
push #rv_old, $ref_old;
}
while ($ref_new = $sth_new->fetchrow_hashref()) {
push #rv_new, $ref_new;
}
my #seen = ();
my #notseen = ();
foreach my $i (#rv_old) {
my $id = $i->{id};
my $title = $i->{title};
my $created = $i->{created};
my $seen = 0;
foreach my $j (#rv_new) {
if ($i->{id} == $j->{id}) {
push #seen, $i;
$seen = 1;
}
}
if ($seen == 0) {
print "$i->{id},$i->{title},$i->{state},$i->{catid},$i->{created}\n";
push #notseen, $i;
}
}
The arrays look like this when using Dumper(#rv_old) to print them:
$VAR1 = {
'title' => 'Legal Notice',
'created' => '2004-10-07 00:17:45',
'id' => 14
};
$VAR2 = {
'created' => '2004-11-15 16:04:06',
'id' => 86096,
'title' => 'IRC'
};
$VAR3 = {
'id' => 16,
'created' => '2004-10-07 16:15:29',
'title' => 'About'
};
I tried to use grep() using array references, but I don't think I understand arrays, hashes, and references well enough to do it properly. My failed grep() attempts are below. I'd appreciate any ideas of how to do this properly.
I believe the problem with this is that I don't know how to reference the id field in the second array of hashes. Most of the examples using grep() that I've seen are to just look through an entire array, like you would with regular grep(1). I need to iterate through one array, checking each of the values from the id field with the id field from another array.
my $rv_old_ref = \#rv_old;
my $rv_new_ref = \#rv_new;
for my $i ( 0 .. $#rv_old) {
my $match = grep { $rv_new_ref->$_ == $rv_old_ref->$_ } #rv_new;
push #notseen, $match if !$match;
}
I also tried variations on the grep() above:
1) if (($p) = grep ($hash_ref->{id}, #rv_old)) {
2) if ($hash_ref->{id} ~~ #rv_old) {
There are a number of libraries that compare arrays. However, your comparison involves complex data structures (the arrays have hashrefs as elements) and this at least complicates use of all modules that I am aware of.
So here is a way to do it by hand. I use the shown array and its copy with one value changed.
use warnings;
use strict;
use feature 'say';
use List::Util qw(none); # in List::MoreUtils with older Perls
use Data::Dump qw(dd pp);
sub hr_eq {
my ($e1, $e2) = #_;
return 0 if scalar keys %$e1 != scalar keys %$e2;
foreach my $k1 (keys %$e1) {
return 0 if !exists($e2->{$k1}) or $e1->{$k1} ne $e2->{$k1};
}
return 1
}
my #a1 = (
{ 'title' => 'Legal Notice', 'created' => '2004-10-07 00:17:45', 'id' => 14 },
{ 'created' => '2004-11-15 16:04:06', 'id' => 86096, 'title' => 'IRC' },
{ 'id' => 16, 'created' => '2004-10-07 16:15:29', 'title' => 'About' }
);
my #a2 = (
{ 'title' => 'Legal Notice', 'created' => '2004-10-07 00:17:45', 'id' => 14 },
{ 'created' => '2004-11-15 16:xxx:06', 'id' => 86096, 'title' => 'IRC' },
{ 'id' => 16, 'created' => '2004-10-07 16:15:29', 'title' => 'About' }
);
my #only_in_two = grep {
my $e2 = $_;
none { hr_eq($e2, $_) } #a1;
} #a2;
dd \#only_in_two;
This correctly identifies the element in #a2 that doesn't exist in #a1 (with xxx in timestamp).
Notes
This finds what elements of one array are not in another, not the full difference between arrays. It is what the question specifically asks for.
The comparison relies on details of your data structure (hashref); there's no escaping that, unless you want to reach for more comprehensive libraries (like Test::More).
This uses string comparison, ne, even for numbers and timestamps. See whether it makes sense for your real data to use more appropriate comparisons for particular elements.
Searching through a whole list for each element of a list is an O(N*M) algorithm. Solutions of such (quadratic) complexity are usable as long as data isn't too big; however, once data gets big enough so that size increases have clear effects they break down rapidly (slow down to the point of being useless). Time it to get a feel for this in your case.
An O(N+M) approach exists here, utilizing hashes, shown in ikegami answer. This is much better algorithmically, once the data is large enough for it to show. However, as your array carries complex data structure (hashrefs) a bit of work is needed to come up with a working program, specially as we don't know data. But if your data is sizable then you surely want to implement this.
Some comments on filtering.
The question correctly observes that for each element of an array, as it's processed in grep, the whole other array need be checked.
This is done in the body of grep using none from List::Util. It returns true if the code in its block evaluates false for all elements of the list; thus, if "none" of the elements satisfy that code. This is the heart of the requirement: an element must not be found in the other array.
Care is needed with the default $_ variable, since it is used by both grep and none.
In grep's block $_ aliases the currently processed element of the list, as grep goes through them one by one; we save it into a named variable ($e2). Then none comes along and in its block "takes possession" of $_, assigning elements of #a1 to it as it processes them. The current element of #a2 is also available since we have copied it into $e2.
The test performed in none is pulled into a a subroutine, which I call hr_eq to emphasize that it is specifically for equality comparison of (elements in) hashrefs.
It is in this sub where the details can be tweaked. Firstly, instead of bluntly using ne for values for each key, you can add custom comparisons for particular keys (numbers must use ==, etc). Then, if your data structures change this is where you'd adjust specifics.
You could use grep.
for my $new_row (#new_rows) {
say "$new_row->{id} not in old"
if !grep { $_->{id} == $new_row->{id} } #old_rows;
}
for my $old_row (#old_rows) {
say "$old_row->{id} not in new"
if !grep { $_->{id} == $old_row->{id} } #new_rows;
}
But that's an O(N*M) solution, while there exists an O(N+M) solution that would be far faster.
my %old_keys; ++$old_keys{ $_->{id} } for #old_rows;
my %new_keys; ++$new_keys{ $_->{id} } for #new_rows;
for my $new_row (#new_rows) {
say "$new_row->{id} not in old"
if !$old_keys{$new_row->{id}};
}
for my $old_row (#old_rows) {
say "$old_row->{id} not in new"
if !$new_keys{$old_row->{id}};
}
If both of your database connections are to the same database, this can be done far more efficiently within the database itself.
Create a temporary table with three fields, id, old_count (DEFAULT 0) and new_count (DEFAULT 0).
INSERT OR UPDATE from the old table into the temporary table, incrementing old_count in the process.
INSERT OR UPDATE from the new table into the temporary table, incrementing new_count in the process.
SELECT the rows of the temporary table which have 0 for old_count or 0 for new_count.
select id,title,created from mos_content
LEFT JOIN xugc_content USING(id)
WHERE xugc_content.id IS NULL;
Gives you the rows that are in mos_content but not in xugc_content.
That's even shorter than the Perl code.
I have two arrays that need to be combined into a hash. However, one of the arrays will always be the key. So I need to go through a list of names, numbers, addresses, etc and give them all titles. An example would be:
Adjustor name => Chase, Firm name => Chase Bank
and then repeat for another location.
Adjustor name => Rob, Firm name => Walmart.
Here is what I have so far:
Array_headers = ['Adjuster Name','Firm Name', 'Firm Number', 'Adjustment Type', 'Address 1', 'Address 2', 'City', 'State', 'Zip Code', 'Phone', 'Fax', 'Website', 'Comments', 'Latitude', 'Longitude', 'Manual LongLatCalc', 'LongLat Error']
Data_examples = ["AdjusterName", "FirmName", "FirmNumber", "AdjustmentType", "Address1", "Address2", "City", "State", "ZipCode", "Phone", "Fax", "WebSite", "Comments", "Latitude", "Longitude", "ManualLongLatCalc", "LongLatError", "chase", "chase bank", "260-239-1761", "property", "501 w", "200 s", "albion", "in", "46701", "555-555-5555", "c#gamil", "whatsupwhatups.com", "hahahah", "12.332", "12.222", "no", "none"]
CombiningArrays= Hash[Array_headers.zip data_examples]
p CombiningArrays
It should return the following:
{"Adjuster Name"=>"AdjusterName", "Firm Name"=>"FirmName", "Firm Number"=>"FirmNumber", "Adjustment Type"=>"AdjustmentType", "Address 1"=>"Address1", "Address 2"=>"Address2", "City"=>"City", "State"=>"State", "Zip Code"=>"ZipCode", "Phone"=>"Phone", "Fax"=>"Fax", "Website"=>"WebSite", "Comments"=>"Comments", "Latitude"=>"Latitude", "Longitude"=>"Longitude", "Manual LongLatCalc"=>"ManualLongLatCalc", "LongLat Error"=>"LongLatError", *"Adjuster Name"=>" \r\nchase", "Firm Name"=>"chase", "Firm Number"=>"260-239-1761", "Adjustment Type"=>"property", "Address 1"=>"501 w", "Address 2"=>"200 s", "City"=>"albion", "State"=>"in", "Zip Code"=>"46701", "Phone"=>"555-555-5555", "Fax"=>"c#gamil", "Website"=>"whatsupwhatups.com", "Comments"=>"hahahah", "Latitude"=>"12.332", "Longitude"=>"12.222", "Manual LongLatCalc"=>"no", "LongLat Error"=>"none"*}
It stops at "LongLat Error"=>"LongLatError" and everything that is italicized does not show up. How do I get it to continually loop through my other array?
I also tried the following code:
#Creating a method to go through arrays
def array_hash_converter headers, data
hash = Hash.new
headers.each_with_index do |header, index|
hash[header] = data[index]
end
puts hash
end
i=0
while i < data.count do
array_hash_converter Array_header, data
i+=1
end
Please Help!
I suggest to slice the values array on the keys array length, and then just map them into an array of hashes. For example:
sliced_values = Data_examples.each_slice(Array_headers.length)
result = sliced_values.map { |slice| Array_headers.zip(slice).to_h }
You will never get a single hash as result, because you'll have collision on the keys and, then, only the last result will be returned, since it overwrites the previous ones. Remember that hash keys are unique in Ruby.
Looks like you actually want an array of hashes. So, your first array is going to be your list of keys for your hashes (I'll reference to this as "HEADER_KEYS"). In your second array, I see "\r\n". You may want to back up a step. I'm assuming this is coming from a CSV, so there are known delimiters and an unknown number of rows. To start parsing your CSV, split on the "\r\n" (or whatever the line break happens to be), and then iterate over each of those items and split on the commas. Something like:
final_dataset = []
HEADER_KEYS = [].freeze # put your actual array_of_headers here and freeze it
array_of_rows = []
csv_string.split("\r\n").each { |row| array_of_rows.push(row.split) }
This should give you an array of arrays that you can loop over.
array_of_rows.each do |row|
row_hash = {}
HEADER_KEYS.each_with_index do |key, index|
row_hash[key] = row[index]
end
final_dataset.push(row_hash)
end
There may be a more elegant way of handling this, but this should do the trick to get you going.
The variable:
$products_in_cart = '112,109,106';
The query_posts:
query_posts(array(
'post_type' => 'product',
'post__not_in' => array($products_in_cart),
...
If I replace in the query $products_in_cart by 112,109,106 it work.
The variable is ok outside the loop, can’t understand what is wrong with this basic use, thanks for your help.
You have to pass an array of IDs to post__not_in. If you start with a comma delimited string you can use the PHP function explode to expand it to an array:
...
'post__not_in' => explode(",", $products_in_cart),
...
Because you need an array on 'post__not_in' and you are creating an array but with one input: '112, 109, 106'. You have to use the explode function. Something like this:
$products_in_cart = '112,109,106';
And then:
'post__not_in' => explode(",", $products_in_cart),
Or just create an array from the beginning:
$products_in_cart = array(112, 109, 106);
I'm using several arrays of identical structure in a Perl program, and at some point, I need to output there names. I.e., I need to get 'array_name' from #array_name.
How can I do it? If it's impossible, can you suggest any convenient way of "relative" storing of arrays and their names?
Thank you.
It is certainly possible, but I'd recommend to store your arrays as anonymous array references in a hash:
my %arrays = (
'array_name' => [ qw(contents of array array_name) ],
# etc
)
Then, to retrieve an array with name $some_array_name:
my #array = #{$arrays{"$some_array_name"}};
You can create a hash of anonymous arrays as follows:
%HoA = (
flintstones => [ "fred", "barney" ],
jetsons => [ "george", "jane", "elroy" ],
simpsons => [ "homer", "marge", "bart" ],
);
You can set the first element of a particular array as follows:
$HoA{flintstones}[0] = "Fred";
To capitalize the second Simpson, apply a substitution to the appropriate array element:
$HoA{simpsons}[1] =~ s/(\w)/\u$1/;
You can print all of the families by looping through the keys of the hash:
for $family ( keys %HoA ) {
print "$family: #{ $HoA{$family} }\n";
}
There is no automatic name storage. It is just a name of the variable you use. If you need to store it, I'd recommend using an Hash like this:
$array->{name} = "array_name";
$array->{data} = [#array_name];
I have an array called $all_countries following this structure:
Array
(
[0] => Array
(
[countries] => Array
(
[id] => 1
[countryName] => Afghanistan
)
)
[1] => Array
(
[countries] => Array
(
[id] => 2
[countryName] => Andorra
)
)
)
I want to loop through an array called prohibited_countries and unset the entire [countries] element that has a countryName matching.
foreach($prohibited_countries as $country){
//search the $all_countries array for the prohibited country and remove it...
}
Basically I've tried using an array_search() but I can't get my head around it, and I'm pretty sure I could simplify this array beforehand using Set::extract or something?
I'd be really grateful if someone could suggest the best way of doing this, thanks.
Here's an example using array_filter:
$all_countries = ...
$prohibited_countries = array('USA', 'England'); // As an example
$new_countries = array_filter($all_countries, create_function('$record', 'global $prohibited_countries; return !in_array($record["countries"]["countryName"], $prohibited_countries);'));
$new_countries now contains the filtered array
Well first of all id e teh array in the format:
Array(
'Andorra' => 2,
'Afghanistan' => 1
);
Or if you need to have the named keys then i would do:
Array(
'Andorra' => array('countryName'=> 'Andorra', 'id'=>2),
'Afghanistan' => array('countryName'=> 'Afghanistan', 'id'=>1)
);
then i would jsut use an array_diff_keys:
// assuming the restricted and full list are in the same
// array format as outlined above:
$allowedCountries = array_diff_keys($allCountries, $restrictedCountries);
If your restricted countries are just an array of names or ids then you can use array_flip, array_keys, and/or array_fill as necessary to get the values to be the keys for the array_diff_keys operation.
You could also use array_map to do it.
Try something like this (it's probably not the most efficient way, but it should work):
for ($i = count($all_countries) - 1; $i >= 0; $i--) {
if (in_array($all_countries[$i]['countries']['countryName'], $prohibited_countries) {
unset($all_countries[$i]);
}
}
If you wanted to use the Set class included in CakePHP, you could definitely reduce the simplicity of your country array with Set::combine( array(), key, value ). This will reduce the dimensionality (however, you could do this differently as well. It looks like your country array is being created by a Cake model; you could use Model::find( 'list' ) if you don't want the multiple-dimension resultant array... but YMMV).
Anyway, to solve your core problem you should use PHP's built-in array_filter(...) function. Manual page: http://us3.php.net/manual/en/function.array-filter.php
Iterates over each value in the input
array passing them to the callback
function. If the callback function
returns true, the current value from
input is returned into the result
array. Array keys are preserved.
Basically, pass it your country array. Define a callback function that will return true if the argument passed to the callback is not on the list of banned countries.
Note: array_filter will iterate over your array, and is going to be much faster (execution time-wise) than using a for loop, as array_filter is a wrapper to an underlying C function. Most of the time in PHP, you can find a built-in to massage arrays for what you need; and it's usually a good idea to use them, just because of the speed boost.
HTH,
Travis