LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2012, 10:42 PM   #46
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234

Quote:
Originally Posted by PTrenholme
I started from his posted code and changed it 'till I got output that looked more like your specification.
And the resulting code corresponds to the method you used to write it: a few initial lines from H_TeXMeX_H code and all the rest yours. So I assume it’s your code inspired by H_TeXMeX_H code. (I asked you about that because I should put the information about the authors into the plugin itself as well as into the THANKS file.)

Quote:
What H_TeXMeX_H's code seemed to be doing was trying to write the HH|MM|SS values as six-binary-digit numbers. Somewhat harder to "read," but a "true" binary clock.
Something that you call “six-binary-digit numbers clock” I call “6-bits binary clock” (I mean each number consists of 6 bits). The vertical binary clock for wminfo can be only 4-bits clock. The horizontal binary clock for wminfo can be 6-bits clock and thanks to two additional lines it can display also the month and the day.

As for the binary watches I prefer 4-bits versions because they look cooler and are easier to read.

Quote:
(Being 72 years old, I'm not mentally flexible enough to understand binary notation as "intuitively" as decimal. )
At the beginning I asked: “Is it the reverse numbers notation or something like that?”. Then I checked your profile. According to the picture you indeed can be in your forties, fifties, etc. (The picture is blurred so it’s difficult to determine the age more precisely.) Well, yesterday I told my friend about that thread. She smiled and said: “That must be very young people”. I think youth is a state of mind. I know a lot of people much younger than me that look, behave, and – as a result – are older than me.
 
Old 09-23-2012, 02:39 AM   #47
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Alright, I have fixed it to the way you want:

PHP Code:
// binary clock
#include <time.h>
#include <stdio.h>
//#include <unistd.h>

int main (void)
{
    
time_t rawtime;
    
struct tm timeinfo;

    
/*for (;;)
    {*/
        
time ( &rawtime );
        
timeinfo localtime ( &rawtime );

        
printf("HH|MM|SS\n%c%c|%c%c|%c%c\n%c%c|%c%c|%c%c\n %c|%c%c|%c%c\n %c| %c| %c\n",
        ((
timeinfo->tm_hour 10) & 1) ? '*' ' ', ((timeinfo->tm_hour 10) & 1) ? '*' ' ',
        ((
timeinfo->tm_min  10) & 1) ? '*' ' ', ((timeinfo->tm_min  10) & 1) ? '*' ' ',
        ((
timeinfo->tm_sec  10) & 1) ? '*' ' ', ((timeinfo->tm_sec  10) & 1) ? '*' ' ',
        ((
timeinfo->tm_hour 10) & 2) ? '*' ' ', ((timeinfo->tm_hour 10) & 2) ? '*' ' ',
        ((
timeinfo->tm_min  10) & 2) ? '*' ' ', ((timeinfo->tm_min  10) & 2) ? '*' ' ',
        ((
timeinfo->tm_sec  10) & 2) ? '*' ' ', ((timeinfo->tm_sec  10) & 2) ? '*' ' ',
                                                    ((
timeinfo->tm_hour 10) & 4) ? '*' ' ',
        ((
timeinfo->tm_min  10) & 4) ? '*' ' ', ((timeinfo->tm_min  10) & 4) ? '*' ' ',
        ((
timeinfo->tm_sec  10) & 4) ? '*' ' ', ((timeinfo->tm_sec  10) & 4) ? '*' ' ',
                                                    ((
timeinfo->tm_hour 10) & 8) ? '*' ' ',
                                                    ((
timeinfo->tm_min  10) & 8) ? '*' ' ',
                                                    ((
timeinfo->tm_sec  10) & 8) ? '*' ' ');

        
/*printf("|H|M|S|\n|%c|%c|%c|\n|%c|%c|%c|\n|%c|%c|%c|\n|%c|%c|%c|\n|%c|%c|%c|\n  |%c|%c|\n", \
        (timeinfo->tm_hour &  1) ? '*' : ' ', (timeinfo->tm_min &  1) ? '*' : ' ', (timeinfo->tm_sec &  1) ? '*' : ' ',
        (timeinfo->tm_hour &  2) ? '*' : ' ', (timeinfo->tm_min &  2) ? '*' : ' ', (timeinfo->tm_sec &  2) ? '*' : ' ',
        (timeinfo->tm_hour &  4) ? '*' : ' ', (timeinfo->tm_min &  4) ? '*' : ' ', (timeinfo->tm_sec &  4) ? '*' : ' ',
        (timeinfo->tm_hour &  8) ? '*' : ' ', (timeinfo->tm_min &  8) ? '*' : ' ', (timeinfo->tm_sec &  8) ? '*' : ' ',
        (timeinfo->tm_hour & 16) ? '*' : ' ', (timeinfo->tm_min & 16) ? '*' : ' ', (timeinfo->tm_sec & 16) ? '*' : ' ',
                                              (timeinfo->tm_min & 32) ? '*' : ' ', (timeinfo->tm_sec & 32) ? '*' : ' ');*/

    /*    sleep(1);
    }*/

    
return 0;

Performance is the same:
Code:
bash-4.1$ ./do-timings.sh 100
time for 00-nothing              : 0.31 real, 0.12 user, 0.09 sys, 67.92% CPU
time for 07-rotate1-w1k0         : 3.13 real, 0.41 user, 0.79 sys, 38.48% CPU
time for 08-rotate-multi-PTrenhol: 3.10 real, 0.47 user, 0.82 sys, 41.89% CPU
time for 15-gawk-ntubski         : 0.46 real, 0.22 user, 0.20 sys, 93.47% CPU
time for 17-simpler-time-vars-w1k: 2.08 real, 0.43 user, 0.79 sys, 58.84% CPU
time for 18-heredoc-grail        : 1.28 real, 0.79 user, 0.32 sys, 87.09% CPU
time for 21-arg-grail            : 1.16 real, 0.77 user, 0.32 sys, 94.91% CPU
time for 23-no-transpose-grail   : 1.14 real, 0.72 user, 0.32 sys, 91.84% CPU
time for 25-bash-PTrenholme      : 0.59 real, 0.30 user, 0.12 sys, 72.79% CPU
time for 29-bash-grail           : 0.53 real, 0.30 user, 0.11 sys, 77.47% CPU
time for 47-binclock             : 0.06 real, 0.00 user, 0.01 sys, 21.52% CPU
bash-4.1$ ./47-binclock 
HH|MM|SS
* |**|  
  |* |  
 *|  |**
  | *|
EDIT:
I like this format better for 4-bit:

Code:
printf("|HH|MM|SS|\n|%c%c|%c%c|%c%c|\n|%c%c|%c%c|%c%c|\n  %c|%c%c|%c%c|\n  %c| %c| %c|\n",
|HH|MM|SS|
|* |  |* |
|  |  |**|
  *|* | *|
   |  |  |
EDIT2: Fixed the bug.

Last edited by H_TeXMeX_H; 09-23-2012 at 06:41 AM.
 
1 members found this post helpful.
Old 09-23-2012, 06:16 AM   #48
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@H_TeXMeX_H,

Thank you very much. It goes to the next wminfo release.
 
Old 09-23-2012, 06:42 AM   #49
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Check the post again, I fixed a bug because I forgot that the maximum for the hours. It works properly now, but test it just to be sure.
 
1 members found this post helpful.
Old 09-23-2012, 08:09 AM   #50
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@H_TeXMeX_H,

It seems now everything is all right. The plugin works like a charm.
 
Old 09-23-2012, 09:20 PM   #51
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
You lost me Daniel???
Looking at this code with a clear head I changed it to:
Code:
echo -e "$VAR1\n$VAR2\n$VAR3"  \
|awk -F "" '{for (i=1; i<=NF; i++) a[i]=a[i]substr("AB",1+$i,1)} 
  END {i=1; while (i in a) {print a[i]; i++;}}'
This words as intended.

Daniel B. Martin
 
1 members found this post helpful.
Old 09-24-2012, 06:57 AM   #52
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
You lost me Daniel???
A variation on the same theme:
Code:
echo -e "$VAR1\n$VAR2\n$VAR3"  \
|awk -F "" 'BEGIN {b[0]="A";b[1]="B"}  \
  {for (i=1; i<=NF; i++) a[i]=a[i]b[$i]}   \
  END {i=1; while (i in a) {print a[i]; i++;}}'
Daniel B. Martin
 
1 members found this post helpful.
Old 09-24-2012, 10:13 AM   #53
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
@Daniel - So are you happy with your solutions now or is it not producing the output you wish? (Sorry to semi-hijack the question)
 
Old 09-24-2012, 10:33 AM   #54
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
@Daniel - So are you happy with your solutions now or is it not producing the output you wish? (Sorry to semi-hijack the question)
Happy with these solutions. I am 2mm closer to being a competent awker.

Daniel B. Martin
 
Old 09-24-2012, 01:03 PM   #55
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
Quote:
Originally Posted by grail View Post
(Sorry to semi-hijack the question)
No problem. danielbmartin asked again exactly the same question I asked in post #5 so it isn't hijacking at all and my thanks go to you.

[EDIT]
My thanks go of course to danielbmartin because he published the solution.
[/EDIT]

Last edited by w1k0; 09-24-2012 at 01:04 PM.
 
Old 09-24-2012, 09:36 PM   #56
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
H_TeXMeX_H, PTrenholme, grail, and ntubski,

Thanks to ntubski the next wminfo release can run not only bash scripts but also scripts written in the other languages as well as binary programs. I tested all your scripts once again running them as wminfo plugins. Two of them written in C have the best performance. Five following written in bash or Ruby have slightly worse performance. One written in bash has the worst performance.

I put the current pre-release of wminfo here: http://linux-bsd-unix.strefa.pl/wminfo-pre3.1.0.tar.gz. (Before releasing it I have at least update some Internet-dependent plugins and test the program and plugins in some other distribution.)

Here’s an excerpt from wminfo documentation:

Quote:
Eight different 4-bits vertical binary clock plugins -- two from plugins.binary directory, five from plugins.system directory, and one from samples directory -- do exactly the same and are the result of the discussion in the Programming section of LinuxQuestions.org (http://www.linuxquestions.org).

A few debaters -- H_TeXMeX_H, PTrenholme, grail, ntubski, and w1k0 -- wrote the 4-bits vertical binary clocks designed as the plugins for wminfo (some of them wrote a few versions).

Two binary clocks (written in C):

- binclock-vertical-1.bin by H_TeXMeX_H
- binclock-vertical-2.bin by PTrenholme

are in the plugins.binary directory (these are the plugins using the CPU power the least).

Five binary clocks (three written in bash and two written in Ruby):

- binclock-vertical-3.wmi by grail
- binclock-vertical-4.wmi by Noam Postavsky aka ntubski
- binclock-vertical-5.wmi by grail
- binclock-vertical-6.wmi by PTrenholme
- binclock-vertical-7.wmi by grail

are in the plugins.system directory (these are the plugins using the CPU power moderately).

One binary clock (written in bash):

- binclock-vertical-8.wmi by Cezary M. Kruk aka w1k0

is in the samples directory (this is the plugin using the CPU power the most).

I added to all of them the necessary comments as well as the procedures allowing to switch on and switch off the label and the separators.
Here are the results of the tests of the performance:

Code:
The CPU usage of the respective plugins used by wminfo is the following:

    plugin                  | author     | language | CPU usage   | median
    ------------------------+------------+----------+-------------+-------
    binclock-vertical-1.bin | H_TeXMeX_H | C        | 1.3% - 1.5% | 1.2%
    binclock-vertical-2.bin | PTrenholme | C        | 1.3% - 1.6% | 1.2%
    ------------------------+------------+----------+-------------+-------
    binclock-vertical-3.wmi | grail      | bash     | 2.0% - 2.3% | 1.9%
    binclock-vertical-4.wmi | ntubski    | bash     | 2.0% - 2.6% | 1.9%
    binclock-vertical-5.wmi | grail      | Ruby     | 1.8% - 2.3% | 2.0%
    binclock-vertical-6.wmi | PTrenholme | bash     | 2.2% - 2.4% | 2.0%
    binclock-vertical-7.wmi | grail      | Ruby     | 2.0% - 2.5% | 2.0%
    ------------------------+------------+----------+-------------+-------
    binclock-vertical-8.wmi | w1k0       | bash     | 3.8% - 4.4% | 3.9%

(The tiny median difference between 1.9% and 2.0% means in practice the same
performance taking into consideration possible inaccuracy of the
measurement.)

The performance was mesured with the command:

    top -b -n 40 | grep '^Cpu' | sort -n
Thank you guys once again for your kind assistance.

***

This is my post #999. With the next post I’ll reach the title of “Senior Member”. Thanks to that thread and the private mailing with ntubski I learned a lot and came closer to be a senior.

My question from post #24 remains current: “Is it the most active solved thread ever?”.
 
1 members found this post helpful.
Old 09-25-2012, 12:46 AM   #57
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
To help you get to #1000, here's a pure gawk solution.
Code:
BEGIN {
  a="*"
  b=" "
  bin[0][1]=b;bin[0][2]=b;bin[0][3]=b;bin[0][4]=b
  bin[1][1]=b;bin[1][2]=b;bin[1][3]=b;bin[1][4]=a
  bin[2][1]=b;bin[2][2]=b;bin[2][3]=a;bin[2][4]=b
  bin[3][1]=b;bin[3][2]=b;bin[3][3]=a;bin[3][4]=a
  bin[4][1]=b;bin[4][2]=a;bin[4][3]=b;bin[4][4]=b
  bin[5][1]=b;bin[5][2]=a;bin[5][3]=b;bin[5][4]=a
  bin[6][1]=b;bin[6][2]=a;bin[6][3]=a;bin[6][4]=b
  bin[7][1]=b;bin[7][2]=a;bin[7][3]=a;bin[7][4]=a
  bin[8][1]=a;bin[8][2]=b;bin[8][3]=b;bin[8][4]=b
  bin[9][1]=a;bin[9][2]=b;bin[9][3]=b;bin[9][4]=a
# Uncomment the next two lines and the system, fflush and the bracket for continous run
#  while (1) { 
#    print "\x1b[2;J\x1b[1;1;G"
    print strftime("%H|%M|%S")
    print "HH|MM|SS"
    date=strftime("%H%M%S")
    split(date,digit,"")
    for (i=1;i<=4;++i) {
      for (j=1;j<=6;++j) {
        printf("%1s", bin[digit[j]][i])
        if (j%2==0 && j < 6) printf("|")
      }
      print ""
    }
#    system("sleep 1s")
#    fflush()
#  }
}
And its output:
Code:
$ ./bin_clock.gawk
22|42|37
HH|MM|SS
  |  |  
  |* | *
**| *|**
  |  |**
 
1 members found this post helpful.
Old 09-25-2012, 02:42 AM   #58
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
If you have more plugins to write, just make another thread, and will be long just like this one
 
Old 09-25-2012, 02:51 AM   #59
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234
@PTrenholme,

I hoped my post #1000 will be wiser but the harsh true is that writing one post can change Member into Senior Member but can’t change layman into expert.

I can’t run your script.

Your script produces the following result:

./bin_clock.gawk
Code:
./bin_clock.gawk: line 1: BEGIN: command not found
./bin_clock.gawk: line 4: bin[0][1]=b: command not found
.
.
.
./bin_clock.gawk: line 13: bin[9][4]=a: command not found
./bin_clock.gawk: line 17: syntax error near unexpected token `('
./bin_clock.gawk: line 17: `    print strftime("%H|%M|%S")'
Your script with sha-bang at the beginning:

Code:
#!/usr/bin/gawk -f
produces the following result:

./bin_clock.gawk
Code:
gawk: ./bin_clock.gawk:5:   bin[0][1]=b;bin[0][2]=b;bin[0][3]=b;bin[0][4]=b
gawk: ./bin_clock.gawk:5:         ^ syntax error
.
.
.
gawk: ./bin_clock.gawk:14:   bin[9][1]=a;bin[9][2]=b;bin[9][3]=b;bin[9][4]=a
gawk: ./bin_clock.gawk:14:                                             ^ syntax error
gawk: ./bin_clock.gawk:24:         printf("%1s", bin[digit[j]][i])
gawk: ./bin_clock.gawk:24:                                    ^ syntax error
Your script changed to:

Code:
#!/usr/bin/gawk
/usr/bin/gawk 'BEGIN {
  a="*"
  b=" "
.
.
.
}'
produces the following result:

./bin_clock.gawk
Code:
gawk: ./bin_clock.gawk
gawk: ^ syntax error
gawk: ./bin_clock.gawk
gawk:   ^ unterminated regexp
Your script changed to:

Code:
#!/bin/bash
gawk 'BEGIN {
  a="*"
  b=" "
.
.
.
}'
produces the following result:

./bin_clock.gawk
Code:
gawk: cmd. line:3:   bin[0][1]=b;bin[0][2]=b;bin[0][3]=b;bin[0][4]=b
gawk: cmd. line:3:         ^ syntax error
.
.
.
gawk: cmd. line:12:   bin[9][1]=a;bin[9][2]=b;bin[9][3]=b;bin[9][4]=a
gawk: cmd. line:12:                                             ^ syntax error
gawk: cmd. line:22:         printf("%1s", bin[digit[j]][i])
gawk: cmd. line:22:                                    ^ syntax error
Your script started with the command:

gawk bin_clock.gawk

produces the following result:

Code:
gawk: bin_clock.gawk
gawk:          ^ syntax error
Your script renamed to bin_clock and started with the command:

gawk bin_clock

displays nothing and waits until I press Ctrl-C.

The same script started with the command:

gawk -f bin_clock

produces the following result:

Code:
gawk: bin_clock:4:   bin[0][1]=b;bin[0][2]=b;bin[0][3]=b;bin[0][4]=b
gawk: bin_clock:4:         ^ syntax error
.
.
.
gawk: bin_clock:13:   bin[9][1]=a;bin[9][2]=b;bin[9][3]=b;bin[9][4]=a
gawk: bin_clock:13:                                             ^ syntax error
gawk: bin_clock:23:         printf("%1s", bin[digit[j]][i])
gawk: bin_clock:23:                                    ^ syntax error
It seems these are all methods that I found here or there and tried so far.

PTrenholme, enlighten me, please, and tell me how can I run your script in order to force it to work?
 
Old 09-25-2012, 05:23 AM   #60
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
First line needs to be an interpreter just like bash, so try adding the following as the first line:
Code:
#!/usr/bin/awk -f
Of course it will be your path to awk if it is different from above

Although I think ntubski's version from post 15 is probably more concise

Last edited by grail; 09-25-2012 at 05:25 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert columns of data into rows jaufer Linux - Software 3 06-29-2010 05:19 PM
convert columns to rows (tab separated file to csv) doug23 Programming 16 08-16-2009 09:14 PM
Script to convert logs columns to rows fono Linux - Software 10 05-19-2009 08:29 PM
How to print data in rows and columns suran Linux - General 3 03-15-2009 02:53 PM
text data conversion: rows into columns frankie_DJ Programming 6 06-03-2006 06:43 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration