Console output formatting

R

ra don

Hello all, I got two quick questions. First can anyone tell me how to
format the output of a hash in pairs in a table format like

a=1 b=2 c=3
d=4 e=5 f=6

I would prefer the list go down the left most row first but across would
be fine.

Second is there any tutorials or gems/librarys that will deal with
console formating on a screen scale rather than strings. I am thinking
the way aircrack-ng looks. I am not trying anything illegal, just one of
the few programs that I know look like what I want. A picture is at
http://www.aircrack-ng.org/screenshots/aircrack-ng.gif .

Thanx in advance.
 
T

Tadeusz Bochan

Try,

hash.each {|key,val| printf "%s = %d\n",key,val}

or

hash.each_with_index do |a,i|
printf "%s = %d ",a.first,a.last
puts if (i%3==2)
end
 
B

botp

Hello all, =A0I got two quick questions. First can anyone tell me how to
format the output of a hash in pairs in a table format like

a=3D1 b=3D2 c=3D3
d=3D4 e=3D5 f=3D6

ruby is an enjoyable language to work or play with.

eg, try each_slice,

h #=3D> {:a=3D>1, :b=3D>2, :c=3D>3, :d=3D>4, :e=3D>5, :f=3D>6}

h.each_slice(3) do |x,y,z|
p x+y+z
end

[:a, 1, :b, 2, :c, 3]
[:d, 4, :e, 5, :f, 6]
Second is there any tutorials or gems/librarys that will deal with
console formating on a screen scale rather than strings..

search about curses. read more ruby docs/manuals and try the tutorials..

best regards -botp
 
R

ra don

Thank you guys that is perfect. I got one more quick question off
topic. Is there an equivalent in ruby to pythons ctype. All research I
have found shows writing code directly for use in ruby(like in the
pickaxe). What I would like to do is make calls directly to c libraries
that aren't meant for ruby like system libraries.
 
R

ra don

RubyDL looks like it would work but can't find any good docs. Here is
what I got down.

require 'dl'
me = ""
clib = DL.dlopen("libc.so.6")
message = "Hello\n"
printf("Testing: %s", message)
clib.scanf("%s", me)
printf("danrn, %s!\n", me)
puts message

running in irb i get no errors loading but the printf throws an error if
I put clib. in front of it. The scanf gets throws an error also. I know
I am doing something incorrectly but I don't know what. Thanks for your
help and info.
 
T

Tadeusz Bochan

The 'dl' package is certainly one of the most useful Ruby libs out there
and avery clever piece of work. It's a shame it hasn't been
documented/supported very well.

The following works on 1.8.6 and I believe it doesn't in 1.9.
No doubt some gurus will be able to clarify.

require 'dl'
clib = DL.dlopen("/usr/lib/libc.sl")
strlen=clib['strlen',"IS"]
message = "Hello\n"
p strlen[message].first => 6

The above is calling strlen which has simple parameters,
an 'I'nteger return, and a 'S'tring by reference.
(It's too late in the night to do scanf, and the Standard library
already does scanf)

You can use this for any C routine, as long as you get the
parameter prototype string setup correctly.
 
R

ra don

Oops. I was using 1.9 and guess I didn't mention that. I will try the
above in 1.8 and see if I get any further. Thanks a lot.
 
T

Tadeusz Bochan

Hi.
If you look in your ruby folders, you should find a file called dl.txt.
Rudimentary and cryptic documuntation, but reading it with a bit of
patience may help. It is some time ago that I looked it, and the best
doc I found was at http://www.jbrowse.com/text/rdl_en.html but at the
moment I get 'access denied' when I tried to go there.

The method I described earlier has served me very well and it is quite
easy once you get the hang of it.
Firtly, you tell DL where your library is :
lib1=DL.dlopen(library) # Open library
then you create 'pointers' to the routines you want, along with the
parameter prototype string. I remember there were quite a few parameter
code, but the only ones I ended up using were 'I' & 'S' without
problems.
Some others were "L" for long, "P" for pointer.
As long as you are clear about passing parameters by value or by
reference you should be ok. (I don't do C, but I still manage)

func1=lib1['funtion1','ISSSS') # integer returncode +4 parameters passed
by reference
Then to call the function, you can use .call or []

returncode=func1.call(p1,p2,p3,"p4").first
or
returncode=func1[p1,p2,p3,"p4"].first

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top