top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Tips or Tricks to print out tab-separated data in perl

0 votes
387 views

I routinely generate rows of tab-separated data like this;

my @array = ( "boris", "natasha", "rocky", "bullwinkle"); print join "t", @array, "n";

However this code inserts an extra tab between bullwinkle and the newline character. So when it is important I do this instead:print join "t", @array;print "n";
I suppose you could put both statements on a single line. Is there a simpler/faster way to generate this output: boristnatashatrockytbullwinklen?

posted Jun 21, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

print join( "t", @array ), "n";

answer Jun 21, 2013 by anonymous
Similar Questions
0 votes

trying to figure out what `last' actually does, I wrote this test script:

use strict;
use warnings;
use autodie;

my $counter = 0;

while($counter < 8) {
 last if($counter > 2) {
 print "if: " . $counter . "n";
 }
 else {
 print "else: " . $counter . "n";
 }
 $counter++;
}

Unfortunately, that gives syntax errors. I would expect the following output:

else: 0
else: 1
else: 2
if: 3

What's wrong with that (letting aside that it is terribly ambiguous)?

+1 vote

I've a program that needs to print some fields formatted in different ways according to some conditions. The solution I come up is working, but I'm looking for a suggestion for something more elegant.

What I do is something like the following:

print sprintf $formats[ $condition ], @fields;

where $condition is the condition used to select a sprintf format string out of an array (@formats) that contains something like:

my @formats = (
 qw( %09d %-1s %03d ... )
, qw(%-4s %1s %09d %1s %-150s %-4s %011d)
, ...
);

Now, while this approach is working really fine, it is a little hard to decode, especially considering that I've got some formats with 50+ fields. I don't believe that using Perl formats is a solution, it will provide a quite longer configuration (consider I've got even fields specified as "-100%s"!).

Any suggestion to get a more readable code?

+1 vote

I have a perl script test.pl. I want to create desktop shortcut. While I click on this shortcut icon it will run my perl script and keep the terminal open.

I have tried the command

ln -s test.pl ~/Desktop/abc

while I click on this abc Icon from my desktop. It run and same time the terminal exit. Could you please help here?

My requirement is:

I want to create desktop shortcut for particular perl program. While I click on this icon from desktop, it should run and keep the terminal open.

...