Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^3: divide multi-column input file into sub-files depending on specific column's value

by BrowserUk (Patriarch)
on Jul 05, 2016 at 14:37 UTC ( [id://1167249]=note: print w/replies, xml ) Need Help??


in reply to Re^2: divide multi-column input file into sub-files depending on specific column's value
in thread divide multi-column input file into sub-files depending on specific column's value

Are you not getting loads of warnings when you run your code?

For example: Scalar value @columns[$#columns] better written as $columns[$#columns] at ...


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
  • Comment on Re^3: divide multi-column input file into sub-files depending on specific column's value
  • Download Code

Replies are listed 'Best First'.
Re^4: divide multi-column input file into sub-files depending on specific column's value
by angela2 (Sexton) on Jul 05, 2016 at 15:50 UTC

    I was only getting this one warning but I thought it would be ok...? Oops :( I fixed it now.

    I'm now facing another problem - I'm trying to print the formatted input into a new file so that I have the nicely formatted input saved somewhere.

    However when I do

    #!/usr/bin/perl use warnings; use strict; my %fhs; # hash with last_column values open my $INPUT, '<', 'input_file' or die $!; while (my $line = <$INPUT>) { #chomp; # this gives me syntax errors??? my @columns = $line =~ m/\s*(-?[.\d]+)/g; # split columns foreach ($columns[$#columns]) { # foreach element of the last column + = cluster ID open my $FORM, '>', 'output.FORM' or die $!; my $columnform = "%10s"x(@columns) . "\n"; # printf ($FORM $columnform, @columns); # hangs, creates empty file printf $columnform, @columns; # prints on screen correctly close $FORM; } }

    When I print on screen, it prints fine. When I try to print in a file, it doesn't. You might notice that I changed the way I split my columns as the fixed width solution was a bit easier to understand but also a bit dangerous as I can't be 100% that all files will have fixed width entries, so I changed it. I'm probably a pain but if you could hint what's happening with printf I'd be super grateful.

    Edit: I'm also trying this - where the $FORM printf bit is out of the loop, and interestingly it does print something, but it's only one random line.
    #!/usr/bin/perl use warnings; use strict; my %fhs; # hash with last_column values open my $INPUT, '<', 'input_file' or die $!; while (my $line = <$INPUT>) { #chomp; # this gives me syntax errors??? my @columns = $line =~ m/\s*(-?[.\d]+)/g; # split columns open my $FORM, '>', 'output.FORM' or die $!; my $columnform = "%10s"x(@columns) . "\n"; # printf ($FORM $columnform, @columns); # hangs, creates empty file printf $columnform, @columns; # prints on screen correctly close $FORM; foreach ($columns[$#columns]) { # foreach element of the last column + = cluster ID # blah } }
      while (my $line = <$INPUT>) {
      #chomp; # this gives me syntax errors???
      ...
      }

      The  chomp; statements in the code in this post operate on the default  $_ scalar, which does not seem to be initialized anywhere in the code. Let me suggest that what you are seeing is not a syntax error but a "Use of uninitialized value ..." warning (not an error) because you have very wisely enabled warnings in your code.

      c:\@Work\Perl\monks>perl -wMstrict -le "chomp; " Use of uninitialized value $_ in scalar chomp at -e line 1.
      A solution is to chomp something that has been assigned a value, like  $line in the while-loop conditional expression:
          chomp $line;


      Give a man a fish:  <%-{-{-{-<

      I can't see anything wrong with your printf statement, and it works for me:

      open $FORM, '>', 'column.FORM' or die $!;; @c = (1.2, 1.3, 1.4, 1.5); $t = '%10s'x@c . "\n"; printf( $FORM $t, @c );; close $FORM;; ^C C:\test>type column.FORM 1.2 1.3 1.4 1.5

      so what is going wrong I have no idea.

      Equally your comment, #chomp; # this gives me syntax errors??? makes no sense, chomp; cannot be a syntax error.

      Your comments in an earlier thread to the effect of "I don't know why it didn't work before but it does now", all suggest that the way you are writing your code; or running your scripts, or some other environmental factor is causing you to experience problems that are not down to Perl, or the code you are posting.

      The upshot is, I'm going to suggest that you try to seek out someone local to you with some programming knowledge to watch you write and run a small, simple program and perhaps he will see the problem that we cannot see when interacting with you this way.

      A final comment on your script above: You do know that:$columns[$#columns] is a single value?

      If so, why are you using a foreach loop to iterate over a single value?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Thank you for the information and suggestions. It's a bit complicated but I found myself doing lots of coding without expecting to and nobody has the time to explain stuff because, of course, they have their own work to do and can't really give me any more time than they already have. The tricky bit is that the (indeed huge) help I received was just me being a backseat coder - which although it helped tremendously in terms of the project progressing, it couldn't do much in terms of me actually learning to code. I did learn to avoid syntax errors, I did learn to do some things, I can write some stuff but I lack proper knowledge and it's not possible to get more support at this stage. All I can do is google and ask questions - and produce dubious code, unfortunately.

        Anyway. No, I didn't realise I was looping through a single value. I was under the impression (or at least hoping) that I was looping through all values of the last column. And this is where disappointment and tiredness kicks in - I've been "refining" (lol) a piece of code for 10 straight hours and in the end it's completely wrong and doing something weird - and I don't even know what it's doing if it's looping through a single value! In any case thank you very much for the contributions, time and patience.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-04-23 15:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found