<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>techslaves.org &#187; perl</title>
	<atom:link href="http://techslaves.org/tag/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://techslaves.org</link>
	<description>Owned (and fascinated) by technology!</description>
	<lastBuildDate>Thu, 23 Feb 2012 04:55:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>l2c.pl &#8211; Lines to Columns</title>
		<link>http://techslaves.org/2010/03/30/l2c-pl/</link>
		<comments>http://techslaves.org/2010/03/30/l2c-pl/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 02:23:09 +0000</pubDate>
		<dc:creator>rthomson</dc:creator>
				<category><![CDATA[Sysadmin]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://techslaves.org/?p=32</guid>
		<description><![CDATA[Here is a little perl script I wrote some time back for converting a text file&#8217;s contents from lines to columns. I&#8217;d like to present this script and then psuedo-challenge other visitors of this site to write a more efficient algorithm in the language of their choice. Unfortunately, any way (that I know of) for [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Here is a little perl script I wrote some time back for converting a text file&#8217;s contents from lines to columns.</p>
<p>I&#8217;d like to present this script and then psuedo-challenge other visitors of this site to write a more efficient algorithm in the language of their choice. Unfortunately, any way (that I know of) for measuring the efficiency of two or more algorithms across different languages is going to be skewed due to differences in efficiency of compilers, interpreters and what not.</p>
<p><span id="more-32"></span></p>
<p>The script converts a file in the format:</p>
<p>1 2 3 4 5 6 7 8 9 10<br />
11 12 13 14 15 16 17 19 19 20</p>
<p>to:</p>
<p>1 11<br />
2 12<br />
3 13<br />
4 14<br />
5 15<br />
6 16<br />
7 17<br />
8 18<br />
9 19<br />
10 20</p>
<p>I should add that the program must be able to handle unequal number of items in each line, too.</p>
<pre>#!/usr/bin/perl
#
# l2c.pl - lines to columns
#
# http://techslaves.org/

# variables
my @lines;
my @columns;
my $iter = 0;
my $linesize = 0;

# if num of command line arguments is 2
if (($#ARGV + 1) == 2) {

  # open input file for reading
  open(INFILE, $ARGV[0]);

  # read input file into an array of arrays
  # an array of "lines" and each "line" is an array of it's "elements" (based on space or tab)
  while()  { push @lines, [ split(/\s+/, $_) ]; }

  # close input file
  close(INFILE);

  # find the longest line (so we know how many times to loop below)
  foreach $line (@lines) {
    if(@$line &gt; $linesize) { $linesize = @$line; }
  }

  # open output file
  open(OUTFILE, "&gt;$ARGV[1]");  

  # $iter = line element counter
  # loop while the line element counter is less than the longest line
  while ($iter &lt; $linesize) {
    # loop through every line in the array of lines
    foreach $line (@lines) {
      # if a line element at position $iter exists...
      if( @$line-&gt;[$iter] ) {
        # print the line element + a tab to the output file
        print OUTFILE @$line-&gt;[$iter], "\t";
      }
      # advance the line element counter
    } $iter++; print OUTFILE "\n";
  }

  # close output file
  close(OUTFILE);

# if num of command line arguments is NOT 2 print usage
} else { print("usage: $0 inputfile outputfile\n"); }</pre>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://techslaves.org/2010/03/30/l2c-pl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

