LinuxQuestions.org
Help answer threads with 0 replies.
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
 
LinkBack Search this Thread
Old 06-18-2007, 06:36 AM   #1
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Quad boot :: Windows vista 64-bit | Vector Linux | Slackware 13.0 64-Bit | Debian 6.0 64-bit
Posts: 138

Rep: Reputation: 17
[Perl] simple print question.


Hello all.

I have a little problem with print command, like this:
Code:
#!/usr/bin/perl
use strict;
use warnings;
#*************#

my $foo = "Hello world.\n";

print $foo;
It of course prints $foo to the left side of terminal window,
Code:
slax ~ # /Desktop/hello.pl
Hello world.
slax ~ #
but what I want to do is instead of printing $foo to the left side, I want to print it to right side of terminal.
Code:
slax ~ # /Desktop/hello.pl
                                                               Hello world.
slax ~ #
Is there a way to do that (without using tabs or any kind of whitespace characters)?

Cheers ////
 
Old 06-18-2007, 06:54 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
By default, tty drivers print from left to right. You have to pad with whitespace, or use escape characters (ANSI escape sequences) to move things to wierd positions on the screen like that. So, the problem is not related to perl really. What exactly are you trying to do?


For escape sequences see:
http://www.dee.ufcg.edu.br/~rrbrandt/tools/ansi.html

You can use the COLUMNS environment variable to get your screen width.
 
Old 06-18-2007, 07:39 AM   #3
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Quad boot :: Windows vista 64-bit | Vector Linux | Slackware 13.0 64-Bit | Debian 6.0 64-bit
Posts: 138

Original Poster
Rep: Reputation: 17
Quote:
Originally Posted by jim mcnamara
What exactly are you trying to do?
Thanks for answer, what I'm doing is a perl script that takes input from iptables ip_queue module (port 80) and then uses HTML::LinkExtor and parses all links from that traffic.

Then it matches those extracted links with different regexes that I have written/borrowed from different intrusion detection systems.

I show few pics, easier that way for me
It prints like this now:
http://img340.imageshack.us/my.php?image=ssim2.png

I would like to print like this:
http://img255.imageshack.us/my.php?image=ssmodbf0.png

My problem is that those url's are different length so I cant use any kind of tabs or whitespaces.

Code:
print colored ("$url", $script_color);
print colored ("  Rule:026", $rule_color);
print YELLOW, "     ", get_time(), RESET, "\n";
That get_time() produces that timestamp that I would like to print to the right side of terminal.

/////
 
Old 06-18-2007, 11:21 AM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
try something that creates a filler padded dynamically with spaces:
Code:
my $cols = 128;
my $url = "xxxxx xx xx  http://somewhere.com";
my $rule = " Rule 029";
my $tm = sprintf("%s", get_time() );

$tmp = cols - length($url) - length($rule) - length($tm) - length("\n");
$filler = sprintf("%*s", $tmp, " ");

print colored ("$url", $script_color);
print colored ("  Rule:026", $rule_color);
print YELLOW  $filler, $tm, RESET, "\n";
 
Old 06-18-2007, 03:00 PM   #5
//////
Member
 
Registered: Nov 2005
Location: Land of Linux :: Finland
Distribution: Quad boot :: Windows vista 64-bit | Vector Linux | Slackware 13.0 64-Bit | Debian 6.0 64-bit
Posts: 138

Original Poster
Rep: Reputation: 17
Hi, I managed to get it to work. Thanks for hints about ansi codes.

Just change the length of that $url string and it still prints timestamps at the right place.
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
use Term::ANSIColor qw(:constants);
#********************#
my $url = "a href http://www.gogggggggggogle.com/";

my $time = "[18/Jun/2007:21:33:30]";

sub counter
{
	for my $char ('.') {
	my $count = () = ($url =~ /$char/g);
	my $pos = (120 - $count);
	return $pos;
	}
}

print $url;
print YELLOW, "\e[", counter($url), "C", $time, "\e[C", RESET, "\n";
#**********************************************************#
That print looks really ugly right now but I'm going to clean it up later

Cheers, /////
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple Perl Question whohasit Programming 3 11-05-2004 10:52 AM
simple Perl question true_atlantis Programming 5 11-18-2003 08:37 PM
Simple Perl Question jacksmash Programming 1 11-13-2003 09:17 PM
Simple Perl question! Please help! jacksmash Programming 4 11-07-2003 05:56 AM
Simple Perl Question Danny.au Programming 6 07-18-2002 09:46 AM


All times are GMT -5. The time now is 02:56 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration