Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Perl: the Markov chain saw
 
PerlMonks  

Re: Split output by tabs

by shmem (Canon)
on Nov 13, 2012 at 09:20 UTC ( #1003569=note: print w/ replies, xml ) Need Help??


in reply to Split output by tabs

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.


Comment on Re: Split output by tabs
Select or Download Code
Re^2: Split output by tabs
by choroba (Abbot) on Nov 13, 2012 at 09:24 UTC
    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.

        I disagree as to the bogosity of this practice. There is nothing wrong with adding clarity to a statement that may otherwise be confusing or ambiguous. And there is nothing wrong with reducing the number of rules someone needs to know in order to correctly decipher a statement.

        This is just such a case, because an argument given to print can start with either a filehandle or a variable. Depending on what follows, it can be very hard to tell which is intended without braces.

        I do enjoy how Perl lets one push the boundaries of acceptable syntax, but that doesn't mean it's always a good idea to do so. That's just my opinion, of course.



        When's the last time you used duct tape on a duct? --Larry Wall
Re^2: Split output by tabs
by perlnoobster (Sexton) on Nov 13, 2012 at 09:35 UTC
    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.

        wow that is perfect, where can i read up on in regards to skipping blank/empty values when splitting arrays? :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (16)
As of 2014-04-24 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls