Brian, I haven't read your report below closely, but bear in mind that the code 
I posted assumes pages are counted from zero. In fact, it enforces this 
assumption by converting "foo.png" explicitly to "foo 0.png".  

I also didn't get a chance to look into the earlier report about out-of-order 
indices, though I'm quite surprised that explicit sorting was required to fix 
it. My goal in designing (|@:- 2&| * (- 2&|)@#) and earlier versions was 
specifically to create a purely arithmetic, ordering-independent function.  

In other words I set out to define a scalar dyad, which, given the input page 
number and the total number of pages in the book, would produce the corrected 
page # for odd pages.  That it requires the input _array_ to be sorted confuses 
me: except for the #, none of its components can even see the "array", per se 
(only the individual scalars). 

BTW, that # is the first place you should look to permit one-based page 
numbering.

-Dan

PS:  The reason the rest of the code is "so clear" vs np is because the focus 
of my effort was a compact, elegant way to express np.  The rest of the verb 
was just stamp-collecting (so I didn't focus on making it "interesting" :) .  
I'm sure there are straightforward but unexciting ways to express the core page 
flipping verb, particularly at rank 1 .





-----Original Message-----
From: [email protected] 
[mailto:[email protected]] On Behalf Of Brian Schott
Sent: Sunday, December 22, 2013 3:20 PM
To: Chat forum
Subject: Re: [Jchat] Fwd: Archive j

I thought the verb flipOddPages worked, but the reversing of odd pages line 
works for neither an odd nor an even number of pages. Let me demonstrate with 
the verbs I defined in my construction early in this thread.

   NB. argument is an integer indicating number of pages in a report
   nodd=: >.@-:
   neven=: <.@-:
   odd=: >:@+:@i.@nodd
   even=: 2+|.@:+:@i.@neven
   zero=: '0'&,@":"0

   (,.@i.;zero@ (odd,even)"0) ] 5
┌─┬──┐
│0│01│
│1│03│
│2│05│
│3│04│
│4│02│
└─┴──┘
   (,.@i.;zero@ (odd,even)"0) ] 6
┌─┬──┐
│0│01│
│1│03│
│2│05│
│3│06│
│4│04│
│5│02│
└─┴──┘

In contrast to the above, with the code that "reverses order of odds" in the 
verb `flipOddPages` I get the following results below (which I cannot make 
agree with the above).

   (+ 2&| * #-+:) i. 6
0 5 2 3 4 1
   >: (+ 2&| * #-+:) i. 6  NB. same as above, but adding 1
1 6 3 4 5 2

   (|@:- 2&| * (- 2&|)@#) i. 6
0 5 2 3 4 1

   (|@:- 2&| * (- 2&|)@#) i. 5
0 3 2 1 4

Actually, to make the verbs `odd` and `even` above work with the order of the 
files in my Mac directory, rather than with the indices 0, 1, 2, .. , the 
examples above need to be as follows, where the boxes are rotated to correspond 
to the order of the filenames in the directory/

   (,.@i.;&(1&|.)zero@ (odd,even)"0) ] 5
+-+--+
|1|03|
|2|05|
|3|04|
|4|02|
|0|01|
+-+--+
   (,.@i.;&(1&|.)zero@ (odd,even)"0) ] 6
+-+--+
|1|03|
|2|05|
|3|06|
|4|04|
|5|02|
|0|01|
+-+--+

I suspect I can make this work on my own because the rest of the code is so 
clear, but I am keeping the discussion open if anyone is keeping up. I hope I 
am not wrong in my assessment here, but I do have a less than stellar history, 
and may be quite wrong.

Thanks,




On Mon, Dec 16, 2013 at 6:51 AM, Dan Bron <[email protected]> wrote:

> Sorry, I should have tested my code on odd-length arguments as well as 
> even.
>
>
> Thanks to Raul's help on twitter, we can fix the bug by using (|@:- 
> 2&| *
> (-
> 2&|)@#) in place of (+ 2&| * #-+:) .
>
> To address the '\' vs '/' issue, I suppose we should standardize by 
> using PATHJSEP_j_ (or PATHSEP_j_ in J6-) in place of raw '\'.
>
> I'm not sure what changes to make to the verb, if any, to get it to 
> work properly on the example directory you gave.  I suspect you're 
> getting blank output because it's not scanning the directory you want 
> it to.  I suggest you embed a line like
>
>    smoutput jpath dir,pfx,'*.',sfx
>
> somewhere in the verb, to see what directory J's looking in.  If the 
> directory looks right, but you're still getting blank output, then the 
> filename pattern may be wrong.
>
> If the directory doesn't look right, remember that ~ is being expanded 
> by jpath, not your shell. In your shell, ~ means "home", i.e. your 
> user directory.  In J, it means your "J home", which is often a 
> subdirectory of your user directory.  Try using an absolute path 
> (starting with '/') to force J to look in the right place. If that 
> works, you can experiment with jpath and USERFOLDERS to figure out if you can 
> use a shorter synonym.
>
> If the directory looks right, but the pattern doesn't, then you can 
> try experimenting with the LHA to flipOddPages. By default, the verb 
> will look for files using the pattern 'comb*.png'. You can adjust 
> either the prefix
> ('comb') or the suffix ('png') or both, using the x argument.
>
> For example, in your original message, you listed a number of files 
> whose names started with 'combined', (as in 'combined 4.png'), so you could 
> say:
>
>         'combined' flipOddPages some_directory
>
> You also discussed generating PDFs instead of PNG files; in that case, 
> you could say:
>
>         'combined pdf' flipOddPages some_directory
>
> or
>
>         ('combined';'pdf') flipOddPages some_directory
>
> In any case, I don't think we're far off. Should just take some 
> experimentation and maybe a few adjustments to the code.
>
> -Dan
>
>
> flipOddPages=:verb define
>         'comb png' flipOddPages y
> :
>         NB.y=directory to scan, x=file prefix and suffix
>         'pfx sfx'=.2 {. (;:^:(0=L.) x),<'png'
>         dir =. (,PATHJSEP_j_-.{:)y   NB. Use PATHSEP_j_ in J6 and earlier
> versions
>
> smoutput jpath dir,pfx,'*.',sfx
>
>         NB. Scan directory
>         fn  =. {. |: 1!:0 jpath dir,pfx,'*',sfx
>
>         NB. Zero-fill here will convert comb.png to comb 0.png
>         NB. The increments account for the space after comb and
>         NB. the dot before png respectively.
>         pn=.0 ".&> (1+#pfx) }.&.> (-1+#sfx)}.&.> fn
>
>         NB. Reverse order of odds
>         pn=. (|@:- 2&| * (- 2&|)@#) pn     NB. *************
>
>         NB. Table of old-filename, new-filename
>         fn ,. <@;"1 (<pfx,' '),.(":&.> pn),.<'.',sfx
> )
> NB. Can remove smoutput line once you're done debugging
>
> NB. Usage examples:
> flipOddPages '~temp\archive1'
> 'combined' flipOddPages '~temp\archive1'
> 'combined pdf' flipOddPages '~temp\archive1'
> ('combined';'pdf') flipOddPages '~temp\archive1'
>
>
>
> -----Original Message-----
> From: [email protected] 
> [mailto:[email protected]] On Behalf Of Brian Schott
> Sent: Saturday, December 14, 2013 1:17 PM
> To: Chat forum
> Subject: Re: [Jchat] Fwd: Archive j
>
> Raul and Dan,
>
> Yes, when I change '\' to '/' Dan's flipOddPages works great in my j 
> temp folder. I am struggling making it work in a folder like the one I 
> showed, but that is temporary, I hope. When I do, I get a blank line output.
>
> I also want to see how to deal with an odd number of pages.
>
> Great app, though.
>
> Thanks, again.
>
>
> --
> (B=)
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>



--
(B=) <-----my sig
Brian Schott
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to