Hi all,

I am still new to Perl as I've only written about 10 very simple scripts, all with the help of a lot of Googling and reading two precious lifesaver books on Perl. Unfortunately I found myself stuck again at a simple task, I would be grateful if you could provide some hints towards how I should solve it.

I have a big text file with lots of whitespace separated columns in it. I want to ignore the first 12 lines and the two first columns and start copying the data from the third column onwards, until the end of the file. I have "written" (well, I tried one million things I read online and I found something that worked, this doesn't really count as "writing" code) this bit of code (copied below) but I have a few problems:

1. I have started copying data from where I find a match of (/^0.00000/), as this is the 13th line and this is where I want to start. I don't like that I'm matching the zeros though, as I realised that it's not sensitive enough. After checking what happens if I matched for (/^0.00/)) and (/^0.0/)) or even (/^0/)) I found that it's not looking for 5 decimal zeros, it's matching 0.00000 even when I'm looking for 0. As the file contains angle degrees, I'd like to match for something that only appears once, just to be safe.

In line 12, which is one line above where I want to start data copying, there is a unique pattern (@TYPE xy). I'd prefer to match this line and then tell the script to start copying from the next (13) line onwards. I've tried doing that in various different ways but I can't get it right. Unfortunately I don't have an example of what I wrote as I've changed the script more than 200 times, making different attempts until I stopped at matching the zeros since it worked.

2. I have managed to get my printf formatting right ("%8.3f%10.3f") but I'm stuck at another problem. I want to start importing data from column 3 onwards until the end, but the file is too big for me to count the columns, plus the column number might vary between files. So I've thought that I could ask Perl to count the columns for me, since I've already split them by whitespace, only I didn't have any success doing that. Also, I can't understand how I'll tell printf to print the first column in %8.3f format and the rest of them/unknown number in %10.3f format.

This is my script so far:

#/bin/perl/ use strict; my @files; @files = `ls 2kc29-out.txt`; my $file = $_; $_= $file; my $start = 0; open my $input, '<', './2kc29-out.txt' or die $!; open my $output, '>', 'test_mon.txt' or die $!; while (<$input>) { s/^\s+//; next unless (($start) || (/^0.00000/)); $start = 1; my @columns = split /\s+/, $_; printf $output ("%8.3f%10.3f","$columns[2] $columns[3]"); #here I' +m testing how columns 3 and 4 print #print $output (@columns[2..10]); #here I'm trying to see how to w +rite "print from column 3 until the end - pretending that 10 is the e +nd just as a test #printf $output ("%8.3f%10.3f",(@columns[2..10])); #same as above, + but trying to see how to use printf, obviously this only prints colu +mns 3 and 4 and nothing else printf $output "\n"; }

Any hints/guidelines would be much appreciated, I already feel ridiculous for spending 6 days on this and only getting this "far" :(

Update:

I've now tried the following:

print $output $columns[2] .. $columns[$#columns];

...am I getting there? The format is so all over the place that I don't know if it's indeed printing the angle degrees, but does it make sense as in "from column 3 to the last column"?


In reply to column counter and printf question by fasoli

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.