Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Split output by tabs

by perlnoobster (Sexton)
on Nov 13, 2012 at 08:32 UTC ( #1003565=perlquestion: print w/ replies, xml ) Need Help??
perlnoobster has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I was just wondering if someone could help me, the original file contains columns seperated by tabs, the coding then sorts that file and creates an output file (thank you choroba for helping me with that!) all i require is for the results in the output file to be separated by tabs, is this possible? if so how?
use warnings; use strict; my $infile = 'orders.txt'; my $output = 'orders_today.txt'; open (OUT, "+>$output"); open my $IN, '<', $infile or die "ERROR: Cannot open '$infile': $!"; my @out; while (<$IN>) { chomp; my @text = split /\t/; push @out, [@text]; } open my $OUT, '>', $output or die "Cannot open '$output': $!"; #@$_ split /\t/; print {$OUT} map "@$_\n", sort { $a->[10] cmp $b->[10] } @out;
thank you

Comment on Split output by tabs
Download Code
Re: Split output by tabs
by choroba (Abbot) on Nov 13, 2012 at 08:56 UTC
    See join, use "\t" for a tab. split makes a list from a string, join glues a list to a string.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thank you choroba, you have been really helpful over the last two days :)
Re: Split output by tabs
by shmem (Canon) on Nov 13, 2012 at 09:20 UTC
    print {$OUT} map "@$_\n", sort { $a->[10] cmp $b->[10] } @out;

    Gratuitous use of curlies: sometimes it is necessary to employ a BLOCK returning a file handle, but here it isn't.

    Then, - TIMTOWTDI - see the special variable $" in perlvar:

    { local $" = "\t"; print $OUT map "@$_\n", sort { $a->[10] cmp $b->[10] } @out; }

    Here, the curlies are necessary to localize the (otherwise global) variable $" to the enclosed block.

      Perl Best Practices #136: Always put filehandles in braces within any print statement. It is easier to read, helps you not to write a comma, and you do not have to remember when to use braces and when not.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Perl Best Practices #136: Always put filehandles in braces within any print statement. It is easier to read, helps you not to write a comma, and you do not have to remember when to use braces and when not.

        Excellent example of a bogus "best practice", thank you: it is always better to know when to use curlies, and when not.

      wow, it works great! thank you :)Just one more question, is there a way of specifying certain columns to be printed? e.g the 10th,18th and 20th column?
        is there a way of specifying certain columns to be printed? e.g the 10th,18th and 20th column?

        One way to do it - use an array slice:

        { local $" = "\t"; print $OUT map "@{$_}[10,18,20]\n", sort { $a->[10] cmp $b->[10] } @ +out; }

        See References quick reference.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003565]
Approved by shmem
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-09-01 08:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (299 votes), past polls