please help count the number of pictures. I create a node node - product.tpl.php her pictures and printing some goods as follows:
<?php
print render($content['uc_product_image']['0']);
print render($content['uc_product_image']['1']);
?>
you want to count the number of images that are associated with this product.
I have tried
echo('<pre>');
print count($content['uc_product_image']);
echo('</pre>');
did not help(
I have used this in custom modules to get the count
$count = count($node->uc_product_image['und']);
Related
I need to randomly select names from a list and randomly place them into a defined number of groups. For instance, I have 20 names in a list and I want my code to randomly pick a name and place them into one of the 4 groups till the end of the list. At the end of the code, I would like to output the names for each generated group or team.
This is what I have so far however it is not working.
data = readtable("NameList (2).xlsx");
%Check and make sure the script is accurately pulling names
Names = (data{:,1}); disp(Names)
%Arranging and Organizing Data
number_of_people = numel(data);
%Scramble array
s= number_of_people(randperm(length(number_of_people)))
number_of_groups = 4;
divisions = sort(randperm(number_of_people-1, number_of_groups-1) + 1, 'ascend')
divisions = [0, divisions, number_of_people]
%Cell array that will hold the groups
groups = cell(1, number_of_groups);
for i= 1:number_of_groups
indexes = divisions(i)+1;
usersInThisGroups = length(indexes);
fprintf('Assigning %d participant (indexes %d to %d) to group %d.\n', ...
usersInThisGroups, divisions(i)+1,divisions(i+1), i);
groups{i} = s(indexes);
end
celldisp(groups); % Display groups in command window.
If you want the number of elements in each group to be as near as possible, I suggest that you determine the size of 2D array first. Then assign all people into 2D array randomly. Mine implementation is as following:
data = readtable("NameList (2).xlsx");
%Check and make sure the script is accurately pulling names
Names = (data{:,1}); disp(Names)
number_of_people = numel(data);
number_of_groups = 4;
%Initialize array
num_of_element_group = uint32(ceil(number_of_people/4))
groups = cell(number_of_groups, num_of_element_group);
%Initialize a random array
rand_num_arr = randperm(number_of_people)
%Using random array to assign people to group randomly
for i=0:number_of_people-1 %Iteration of the people list
groups(mod(i,number_of_groups)+1, floor(i/number_of_groups)+1) = ...
Names(rand_num_arr(i+1)) %Assign the cell with randomly selected name
end
celldisp(groups); % Display groups in command window.
The code crashes after 3 tries.
How do I manage to print all 10 values without repeating them?
var windCard = [1:11, 2:12, 3:21, 4:22, 5:31, 6:32, 7:41, 8:42, 9:51, 10:52 ]
var die = 0
die = Int(arc4random())%windCard.count
print("The wind blow the mosquitoes \(windCard[Int(die)]!)")
windCard.removeValue(forKey: die)
The issue is that Int(arc4random())%windCard.count generates keys in the range 0 to windCard.count-1, while your keys start from 1 and after you remove the first element, the keys won't even be contiguous. So for instance if you remove a key from the middle of your Dictionary (let's say key 5), windCard will have 9 elements and hence die will be in the range 0-8, but your Dictionary will be missing a key, so your code will crash on windCard[key]! if die is 5.
You can achieve your goal by using arc4random_uniform, which accepts an upperBound input argument and using the generated random number to subscript the keys of your windCard Dictionary, which will guaranteed to be contiguous.
while windCard.count > 0 {
let die = Int(arc4random_uniform(UInt32(windCard.keys.count)))
let key = Array(windCard.keys)[die]
print("The wind blow the mosquitoes \(windCard[key]!)")
windCard.removeValue(forKey: key)
}
My approach will be to store the random number you get in every loop in a temp file or database, then compare the new values, if they match, generate a new random value, and then compare again against file or database, that way even if they match you assure you get a genuine value each time. Hope this "algorithm" helps you.
I am rather new to perl, but so far I have found it a very strong language.
Every month I pull an extract from a license register for a product that I am managing, and the data is in CSV format.
I have managed to complete the code to get a sorted list, and also sorted as per my requirements. The list is some 1200 rows.
The format of the list looks like this (I have kept only the vital parts):
Customer;CustomerID;ProductLine;Platform;Version
operatorx;1234;XX;Linux;15
operatorx;1234;YY;x86;7
operatorx;1234;ZZ;Sparc;7
operatory;2345;YY;x86;8
operatory;2345;YY;Sparc;7.1
operatory;2345;ZZ;x86;7.2
The output wanted is like this for the above:
Customer;CustomerID;ProductLine;Platform;Version
operatorx;1234;XX;Linux;15
operatory;2345;YY;x86;8
operatory;2345;ZZ;x86;7.2
My list in the code does not contain any ';', the values are stored in a array like this:
#sortedlist = ([Customer,customerID,ProductLine,Platform,Version])
So any customer can have many rows in my original list, but if the product is XX, then only the first occurrence in the list should be kept, and no occurrences of product YY or ZZ can be kept.
If a customer has no product XX, then the first occurrence of product YY and first occurrence of product ZZ should be kept.
The list is sorted so that the "best" entry is always the first per customerID.
I have tried a very simple code, checking that current customerID != prevCustomerID then push the row to a new list, but this makes me miss out when a customer has both products YY and ZZ...
I have also tried nesting a lot of if statements, to try to keep track of current row and previous row... but the code grew a lot, and still didn't give me the expected result :-(
I am starting to think that I approach this from the wrong angle, and I have tried to dig into hashes, but since a customer can actually have one or two entries in the final list, I think a hash is disqualified, as the key value here has to be customerID, and in a hash, there should only be one occurrence per customerID.
Does anyone have any idea on how to attack this problem?
Starting from the top, push the very first element to a new list, and then for each consecutive row, check if it exists in the new list, and what product the new list contains, and if product == XX, then scrap the rest for the same customerID, or if product in the new list == YY, scrap the rest until it finds product == ZZ for the same customerID. Then repeat the same, until it finds a new customerID?
--- updated ---
I managed to solve my issue using awk instead.
./myperlscript.pl input.csv | awk -F ';' '!array[$1,$2,$3]++'| awk -F ';' '{ {if ($2 != prev) {print $0; prev = $2; prevprod = $3}} {if ($2 = prev && prevprod != "XX") { prev =$2}}} > output.csv
But if anyone whould know how to achieve the same with standard perl, it would be very nice.
Here is a naive implementation in perl using state variables that achieves the same result. If you actually have more than 3 variables to consier (XX,YY,ZZ here), you could generalize this into a state array and a function that updates the array and decides what to do based on the state of the array.
filter.pl
#!/usr/bin/env perl
use warnings;
use strict;
my $last_customer = '';
my ($seen_xx, $seen_yy, $seen_zz);
while (my $line = <>) {
# Header
if ($. == 1) {
print $line;
next;
}
# Data
my ($customer_name, $customer_id, $product_line, $platform, $version) = split /;/, $line;
die "Unable to parse line : $line"
unless defined $customer_name;
if ($customer_name ne $last_customer) {
$last_customer = $customer_name;
($seen_xx, $seen_yy, $seen_zz) = (0,0,0); # Reset
}
if (not $seen_xx and $product_line eq 'XX') {
# Print first XX
print $line;
($seen_xx, $seen_yy, $seen_zz) = (1,1,1); # Ignore the others
}
if (not $seen_yy and $product_line eq 'YY') {
# Print first YY if no XX
print $line;
$seen_yy = 1;
}
if (not $seen_zz and $product_line eq 'ZZ') {
# Print first ZZ if no XX
print $line;
$seen_zz = 1;
}
}
Output
cat input | perl filter.pl
Customer;CustomerID;ProductLine;Platform;Version
operatorx;1234;XX;Linux;15
operatory;2345;YY;x86;8
operatory;2345;ZZ;x86;7.2
I'm trying to create a script that reads data from a text file, and plots the data onto a scatter plot.
For example, say the file name is prices.txt and contains:
Pens 2 4
Pencils 1.5 3
Rulers 3 3.5
Sharpeners 1 3
Highlighters 3 4
Where columns 2 and 3 are prices of the items for two different stores.
What my script should do is read the prices, calculates (using another function) future prices of the stores and plots these prices onto a scatter plot where x is one store and y is another. This is a silly example I know but it fits the description.
Don't worry to much about the other function that does the calculation, just assume it does what its supposed to.
Basically, I've come up with the following:
pricesfile = fopen('Prices.txt');
prices = textscan(pricesfile, '%s %d d');
fclose(pricesfile);
count = 1;
while count <= length(prices{1})
for item = constants{1}
name = constants{1}{count};
store_A = prices{2}{count};
store_B = prices{3}{count};
(...other function goes here...)
end
end
After doing this I'm completely stuck. My thought process behind this was to go through each item name, and create a vector that's assigned to this name with its two corresponding prices as items in the vector eg:
pens = [2 4]
pencils = [1.5 3]
etc. Then, I would somehow plot those items in the vector on a scatter plot and use the name of the vector as a label.
I'm not too sure how to carry out the rest of my code or even if what I've written will get me to the solution.
Please help and thanks in advance.
pricesfile = fopen('Prices.txt');
data = textscan(pricesfile, '%s %d d');
fclose(pricesfile);
You were on the right track but after this (through a bit of hackery) you don't actually need a loop:
plot(repmat(data{2},1,2)', repmat(data{3},1,2)', '.')
legend(data{1})
What you DO NOT want to do is create variables named after strings. Rather store them in an array with an array of the names (which is basically what your textscan code gives you). Matlab is very good at handling matrices/arrays.
You could also split your price array up for example:
names = prices{1};
prices = [data{2:3}];
now you can perform calculations on prices quite easily like
prices_cents = prices*100;
plot(prices_cents(:,[1,1]), prices_cents(:,[2,2]))
legend(names)
Note that the [1,1] etc above is just using indexing as a short hand to achieve what repmat does...
as a green hand python programmer, I have a little problem, I'll appreciate if somebody can help!!
I have two lists
a list of random repeated numbers(unknow size) like:
number = [44,198,57,48,658,24,7,44,44,44..]
for n in number
I want to give these numbers to a list of people in order, one number for one person. If a number repeat, the program will find out the person whom got this number when the first time it shows up. It means I want to print a list like
people = [1,2,3,4,5,6,7,1,1,1...]
print people
Store the mapping from numbers to indexes in a dict, and update it as you go.
numbers = [44,198,57,48,658,24,7,44,44,44.]
index_mapping = {}
indexes = []
next_index = 1
for number in numbers:
if number in index_mapping:
# already seen
indexes.append(index_mapping[number])
else:
# a new one
indexes.append(next_index)
index_mapping[number] = next_index
next_index += 1
print indexes