how can I print random rows

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.

Moderators: Community Moderators, iMacros Moderators

Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

how can I print random rows

by green.pine on Fri Apr 08, 2011 2:20 pm

using imacros for ff 7.1.1.2;

trying to have an output from one column randomized and another in sequence,
lest say I have a two column csv file:
url1,kw1
url2,kw2
url3,kw3
I want to have the output from url column goes one row at the time, but
the keyword column goes randomly, i want to use it as anchor text, since my url list are limited, and when a url outputs twice I want to make sure different keyword is used with that url, so making it random is a good solution for me.

like this:
url1 kw3
or
url2 kw41
or
url3 kw1

I am not sure how to make it work.
anyone willing to help me out?


thanks
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by green.pine on Sun Apr 10, 2011 2:19 am

I want to make this work with the code I already got help from moderator on this forum, this is the thread:
viewtopic.php?f=11&t=12551

and here is the code that works for printing three keywords:

Code: Select all
TAB T=1
SET !DATASOURCE MyKeyWords.csv
SET !DATASOURCE_COLUMNS 1

'Store in !VAR1 3* the current !LOOP value
SET !VAR1 {{!LOOP}}
ADD !VAR1 {{!LOOP}}
ADD !VAR1 {{!LOOP}}
'Subtract 2 to get line number for the first of the 3 values...
ADD !VAR1 -2
SET !DATASOURCE_LINE {{!VAR1}}
'...and store read data in !VAR2
ADD !VAR2 {{!COL1}}
'The next...
ADD !VAR1 1
SET !DATASOURCE_LINE {{!VAR1}}
'put in !VAR2 this piece as well
ADD !VAR2 {{!COL1}}
'and the last of the 3
ADD !VAR1 1
SET !DATASOURCE_LINE {{!VAR1}}
'put in !VAR2 this also
ADD !VAR2 {{!COL1}}

'!VAR2 has the 3 keywords concatenation

URL GOTO=http://www.url1.com
TAG POS=1 TYPE=INPUT:TEXT FORM=NAME:newpage ATTR=ID:page CONTENT=pageName
TAG POS=2 TYPE=INPUT:TEXT FORM=NAME:newpage ATTR=ID:f2 CONTENT={{!VAR2}}
TAG POS=1 TYPE=BUTTON ATTR=NAME:go
FRAME F=2
TAG POS=2 TYPE=SPAN ATTR=TXT:Save


the way I wanted to do was having couple of urls that goes with a list of keywords, but I get many duplicate anchor text as the code was in loop, so now i want to make the keyword randomized, this way i can use one url many times,

Since the code prints out 3 rows in sequence at once, I have difficulty to figure out how to have 3 randomized keywords in front of these urls,

right now the code print 3 keywords in sequence,
I have changed the keyword column to url column, it is like this:
url1
url2
url3
and the next loop will be like this:
url4
url5
url6

now I want to add randomized keyword right next to url like this from the keyword column:
url1 kw15
url2 kw29
url3 kw7

I have put the keyword column in separate csv file, and randomized it, but I can only make it work for one line of url not three of them.

appreciate any help on this.
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by MattBell7 on Mon Apr 11, 2011 2:20 am

i'm not aware of a proper 'randomize' function in imacros, but you may be able to cheat by using the current time and formatting it.

e.g. SET !VAR1 {{!NOW:nnss}} (this is minutes and seconds)

you could even do something like Var1 is minutes, var 2 is seconds, add them together then use that, it'd be pretty random, but possible to get duplicates still.
MattBell7
 
Posts: 628
Joined: Thu Nov 26, 2009 4:07 am
Location: United Kingdom

Re: how can I print random rows

by green.pine on Mon Apr 11, 2011 3:18 am

thank you for your response MattBell7;

I am not sure if this is doable for what i want to do, the max number for sec is 60 and for hour is 48, and the list of my keywords are way m ore than that, besides there is js code for randomize number but I can not implement that to what I want since in the code above it prints three rows of data from one column at once, now I have difficulty to have three randomized data from second column to be printed right next to each url.

I tried to use the same code for second column and add random number, but unfortunately
I needed more VAR than imacros allows, two VAR already used so I can only use one VAR;

meanwhile I have some problem of using js code for random number,
I have this code:
Code: Select all
var randomnumber=Math.floor(Math.random()*150)

var retcode=0;

iimSet("rnumber", randomnumber);
retcode = iimPlay("#Current.iim");

if (retcode < 0)               // an error has occured
{
   errtext = iimGetLastError();
   alert("Error "+retcode+": "+errtext);
}


and I use
SET !DATASOURCE_LINE {{rnumber}}
to have number generated by js but it wont work, if I use elsewhere like:
CONTENT={{rnumber}}
the number is printed there, no problem, anyone know why?

appreciate your help to make the randomizing second column data works.
Thanks
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by MattBell7 on Mon Apr 11, 2011 3:44 am

ah, didn't realise you were scripting, yeh, thats a better way for random numbers.

when you say it doesn't work, do you get an error, or do you get no result?

(p.s. if you did use the time, its down to second precision, so minute - second done this way nnss actually gives 3600 possible combinations, but thats irrelevant now)
MattBell7
 
Posts: 628
Joined: Thu Nov 26, 2009 4:07 am
Location: United Kingdom

Re: how can I print random rows

by green.pine on Mon Apr 11, 2011 4:03 am

Thanks for reply;
I don't know js i found the js code from this forum.
yes I do get an error:
error 951 runtime error: invalid datasource_line value: 41, line:6

every time I run I get of course different number in front of value, now it is 41, it means the js code works, but why I get error?



Edited: my bad, I typed wrong error message, i get error 951
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by MattBell7 on Mon Apr 11, 2011 4:23 am

in that case the problem seems to be with the Datasource,

-951 DATASOURCE_LINE exceeds actual number of rows in data source file

try modifying you're script to pass through a value of 1 (take out the randomisation bit) and see what you get.

clearly the random number works and is being passed through, but can't be found in the datasource.

also, can you post your actual macro code. Ta
MattBell7
 
Posts: 628
Joined: Thu Nov 26, 2009 4:07 am
Location: United Kingdom

Re: how can I print random rows

by green.pine on Mon Apr 11, 2011 4:35 am

Thanks for reply;
I tried my code with : SET !ERRORIGNORE YES
and it does work, but as you mension it seems there is problem with amount of data,
because datasource does not accept the number, and just print the first row.
here is the macro:
Code: Select all
SET !EXTRACT NULL
SET !EXTRACT_TEST_POPUP NO
SET !ERRORIGNORE YES
URL GOTO=http://localhost/macrotest/test.html
SET !DATASOURCE kw-url.csv
SET !DATASOURCE_COLUMNS 2
SET !DATASOURCE_LINE {{rnumber}}
TAG POS=1 TYPE=TEXTAREA FORM=NAME:form1 ATTR=ID:bs<SP>text CONTENT={{!COL2}}{{rnumber}}



and here is the js code:
Code: Select all
var randomnumber=Math.floor(Math.random()*150)

var retcode=0;

iimSet("rnumber", randomnumber);
retcode = iimPlay("#Current.iim");

if (retcode < 0)               // an error has occured
{
   errtext = iimGetLastError();
   alert("Error "+retcode+": "+errtext);
}


the macro code is a simple code to try the js, the actual macros code I have problem with is on my second post.

thanks
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by green.pine on Mon Apr 11, 2011 4:41 am

I deleted most of the data from csv file, now I have only 12 rows of url and keyword totally in 2 column,
but when I run it, still with the same result, it prints the first row. somehow it ignores the number.

Edited:
I think I know what the problem is, the quantities of cells on two column have to be same even if the cell on the column that suppose to be printed is not empty,
I print from col2, I had made some changes to csv file and made 2nd column to 20 keywords, but made the first column to have only 5 item in it,
when I run the macros, if random number is 5 or below 5 then it would print the the right data from 2nd column, but if the number is above 5, it print from the first row of 2nd column and ignore the row number, I even switched the column but no result,
well this another problem I got now, I guess I have to use different csv file now.
Last edited by green.pine on Mon Apr 11, 2011 9:50 am, edited 1 time in total.
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by MattBell7 on Mon Apr 11, 2011 5:34 am

ok, with multiple column CSVs with varying amounts of data you need to be really careful with the format. every column needs the same number of cells in it. so if you want a blank, you have to put an empty ,

e.g.

A1,B1
A2,B2
A3,
A4,B4
,B5
,B6
A7,B7

if you dont, and you do this:

A1,B1
A2,B2
A3
A4

row 3, Col2 will be A4

try checking this in your datasource. also make sure you have no extra commas at the end you dont need.

e.g.
A1,B1,
A2,B2

will be broken as well
MattBell7
 
Posts: 628
Joined: Thu Nov 26, 2009 4:07 am
Location: United Kingdom

Re: how can I print random rows

by green.pine on Mon Apr 11, 2011 1:35 pm

thanks MattBell7,
having the column in a separate csv file, the random data works only for one of the printed data not all three rows of data that I have in mind.

anyone have any solution to make my code works?
got help to print three rows of data from one column at one go, now I want to make it print three random data from 2nd column in front of those printed, as I explained in my second post.
appreciate your help.
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by MattBell7 on Tue Apr 12, 2011 1:57 am

let me just get it straight, you have CSV1 which has a list of URLs, CSV2 has a list of keywords

you want to print the next URL from CSV1 with 3 random keywords from CSV2 after it?

correct? if i can figure out what you're trying to do, then i should be able to figure something out.
MattBell7
 
Posts: 628
Joined: Thu Nov 26, 2009 4:07 am
Location: United Kingdom

Re: how can I print random rows

by green.pine on Tue Apr 12, 2011 5:50 am

yes that is right, but not one url three urls ,

it does not matter to have two csv files or one;

EDITED: apparently columns on csv file have to have same quantity of cell otherwise it won't work properly, so I guess two csv files has to be used in this case.

if you try the code above on my second post, lets say you put the loop to 3, then you will see that it prints out three rows of data ( urls) at one go like this:
loop1:

url1
url2
url3

loop2:

url4
url5
url6

loop3:

url7
url8
url9

As I numbered these data you see they are in sequence, as they should be,
Now I want to have data from other column ( keyword column) to be in front of these urls
but the data from second column or second csv file which are keywords have to be randomized, like this:

loop1:

url1 keyword4
url2 keyword23
url3 keyword61

loop2:

url4 keyword18
url5 keyword3
url6 keyword9

loop3:

url7 keyword1
url8 keyword34
url9 keyword71

thanks for your help,
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Re: how can I print random rows

by MattBell7 on Tue Apr 12, 2011 7:22 am

Code: Select all
var rand1=Math.floor(Math.random()*150);
var rand2=Math.floor(Math.random()*150);
var rand3=Math.floor(Math.random()*150);

var retcode=0;

iimSet("rand1", rand1);
iimSet("rand2", rand2);
iimSet("rand3", rand3);

var i=1;
for (i=1;i<=100;i++)
{
iimSet("loopNum", i);
retcode = iimPlay("#Current.iim");
if (retcode < 0)               // an error has occured
{
   errtext = iimGetLastError();
   alert("Error "+retcode+": "+errtext);
}
}


Code: Select all
TAB T=1
SET !DATASOURCE URLs.csv
SET !DATASOURCE_COLUMNS 1
SET !VAR3 {{loopNum}}
ADD !VAR3 {{loopNum}}
ADD !VAR3 {{loopNum}}
'Subtract 2 to get line number for the first of the 3 values...
ADD !VAR3 -2
SET !DATASOURCE_LINE {{!VAR3}}
'this is the URL
SET !VAR1 {{!COL1}}
ADD !VAR3 +1
SET !DATASOURCE_LINE {{!VAR3}}
SET !VAR2 {{!COL1}}
ADD !VAR3 +1
SET !DATASOURCE_LINE {{!VAR3}}
SET !VAR3 {{!COL1}}
SET !DATASOURCE URLs.csv
SET !DATASOURCE_COLUMNS 1
SET !DATASOURCE_LINE {{rand1}}
ADD !VAR1 {{!COL1}}
SET !DATASOURCE_LINE {{rand2}}
ADD !VAR2 {{!COL1}}
SET !DATASOURCE_LINE {{rand3}}
ADD !VAR3 {{!COL1}}


This should give you in your macro:

!VAR1 = http://www.url1.com randkw35
!VAR2 = http://www.url2.com randkw32
!VAR3 = http://www.url3.com randkw68

let me know how you get on with that
MattBell7
 
Posts: 628
Joined: Thu Nov 26, 2009 4:07 am
Location: United Kingdom

Re: how can I print random rows

by green.pine on Tue Apr 12, 2011 9:17 am

thank you for code;
there are some problems,

1- I get an error:

ERROR 911, bad parameter datasource_line must be positive integer line 17
and this is line 17:
SET !DATASOURCE_LINE {{!VAR3}}

after getting the error I have put the SET !ERRORIGNORE YES in the code, and I play ( single play), and the code continues to play and it won't stop, I can only pause it, I have to close the my browser (FF) to stop it.

2-in this part of code does datasource suppose to be keywords csv?
ADD !VAR3 +1
SET !DATASOURCE_LINE {{!VAR3}}
SET !VAR3 {{!COL1}}
SET !DATASOURCE URLs.csv
SET !DATASOURCE_COLUMNS 1
SET !DATASOURCE_LINE {{rand1}}


I assume it is, and I did change it to kws.csv where I have my keywords,
if my assumption is not right then I got all wrong please advise me how to proceed.

and changing it to kws.csv, I still don't get the desired output,
I have put var3, and col1 for the area of output like this:

TAG POS=1 TYPE=TEXTAREA FORM=NAME:form1 ATTR=ID:bs<SP>text CONTENT={{!VAR3}}{{!COL1}}

but the output is one url and two keywords in front of it.
I have my kws csv file with one column and have 103 keywords in it, one on each row,
and one url.csv file with one column as well with 15 urls in it, one on each row.

in the keyword csv file I have put </br> at the end of the each keyword, so the next url should to be on new line , I am not sure how this could effect the url output to work with keyword output one on each line.

thank you for your help;
green.pine
 
Posts: 62
Joined: Thu Nov 04, 2010 10:21 am

Next

Return to iMacros for Firefox

Who is online

Users browsing this forum: No registered users and 15 guests

-->