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

Try:

while(<$FILE>) { chomp ($line = $_); # @columns = $line =~ m/\s*(-?\d+)/g; @columns = $line =~ m/\s*(-?[\d,]+)/g; }

Update: thanks to Lotus1 for the correction. I had it right on my computer, but failed to update the post.

But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

  • Comment on Re: problem with splitting file on whitespace: how to circumvent inconsistent formatting through file
  • Download Code

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

    Hi GotToBTru, \d does not match commas so your program splits up the numbers. If you add the comma in a character class it works.

    use warnings; use strict; while(<DATA>) { chomp (my $line = $_); my @columns = $line =~ m/\s*(-?\d+)/g; print "@columns\n"; } __DATA__ 15,567 -25,324-45,234 15,567-25,324-45,234 -13,345 53,562 13,452 -7,521-22,454-54,671
    Output: 15 567 -25 324 -45 234 15 567 -25 324 -45 234 -13 345 53 562 13 452 -7 521 -22 454 -54 671

    Here it is with the comma added.

    use warnings; use strict; while(my $line = <DATA>) { my @columns = $line =~ m/\s*(-?[,\d]+)/g; print "@columns\n"; } __DATA__ 15,567 -25,324-45,234 15,567-25,324-45,234 -13,345 53,562 13,452 -7,521-22,454-54,671
    Output: 15,567 -25,324 -45,234 15,567 -25,324 -45,234 -13,345 53,562 13,452 -7,521 -22,454 -54,671
      Yes, exactly, this works correctly. Thank you both though, I really appreciate the help and explanations, this is an amazing website.