in reply to problem with splitting file on whitespace: how to circumvent inconsistent formatting through file

Just a guess, are the columns fixed width? If so, unpack can help.

while (<DATA>) { chomp; my @fields = unpack('a7 a7 a7'); print join(" ", map {"\"$_\""} @fields), "\n"; } __DATA__ 15,567-25,324-45,234 -13,345 53,562 13,452 -7,521-22,454-54,671

Outputs:

" 15,567" "-25,324" "-45,234" "-13,345" " 53,562" " 13,452" " -7,521" "-22,454" "-54,671"
  • Comment on Re: problem with splitting file on whitespace: how to circumvent inconsistent formatting through file
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: problem with splitting file on whitespace: how to circumvent inconsistent formatting through file
by angela2 (Sexton) on Jul 04, 2016 at 12:41 UTC

    Hi and thanks for your answer, it's the one I think I understand the most, but it's very kind that you all took the time and tried to help. I'll also try to understand the other two answers hopefully.

    I wasn't successful implementing this but I'm wondering if it has to do with the fact that I haven't really given you a clear picture of what my data looks like, so this file looks like this - this is probably a pretty silly way to show you but here's a link to a screenshot of my data https://www.dropbox.com/s/68xjbspn47jzmx9/Untitled.png?dl=0 , sorry I know this is really random but I have no clue how to properly format the numbers so that you can see.

    As you can see I have 4 columns, the last one is always 1 digit and the rest always have 3 decimal digits. Can your solution (with some editing) still be implemented on my data?

      Can't you just copy and paste some of your data here inside <code> tags? Those will preserve formatting.

      Anyway, from this small sample it does look like your columns are fixed width. The first one looks to have either 7 or 8 characters, the second and third 8 characters - try an unpack pattern of "a8 a8 a8" and see what you get. Adjust and extend this pattern as is appropriate for your input data, for example to add another column "a8 a8 a8 a6" and so on.

      For a tutorial see "Packing Text" in perlpacktut.

        Ah yes, ok I get it now! Thank you so much! I did try various "a" values (a1, a7, a6 etc) but couldn't figure it out. Thanks, I'm checking out the link :)