http://www.perlmonks.org?node_id=935161

jmccaslin has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple file in the format

TIME number of steps: 13 time values: 2.83506E+03 2.83697E+03 2.83911E+03 2.84117E+03 2.84331E+03 2.84544E+03 2.84750E+03 2.84958E+03 2.85172E+03 2.85383E+03 2.85588E+03 2.85804E+03 2.86012E+03

and I would like to take the time values and store them in an array. I'm sure this is very simple, but I'm not sure how to do it using Perl... Any help is much appreciated.

Replies are listed 'Best First'.
Re: getting numeric data from a file into an array
by runrig (Abbot) on Nov 01, 2011 at 17:40 UTC
    ...I'm not sure how to do it

    What have you tried so far? Just so you know, we're usually happy to help, but this is not a code writing service or a place to get homework answers.

    And welcome to PerlMonks!

      I should have been more specific. I was suspicious that matching reg exp would do the trick. Here is what I have so far:

      # read in times open MYFILE, "<$dir/structs/times"; my @temptimes = <MYFILE>; close(MYFILE); print "$#temptimes\n"; my @strtimes; for (my $i = 2;$i <= $#temptimes;$i++){ if ($temptimes[$i] =~ m/(\d+.\d+)/){ print "$1\n"; } }
      I'm having trouble reading the other two values and storing them in an array. I figured I could just multiply by 10^3 to account for the "E+03", as I didn't know how to deal with the "+" sign. Sorry if it seemed like I'm looking for homework solutions - in fact this is not homework. I can do this in other languages, I just want to learn Perl.
Re: getting numeric data from a file into an array
by roboticus (Chancellor) on Nov 01, 2011 at 17:43 UTC

    jmccaslin:

    Here's a quickie example:

    use strict; use warnings; while (my $line = <DATA>) { chomp; print "INPUT LINE: $line\n"; # Read all the times from the line and place into array my @times = ($line=~m/[-+0-9.E]+/g); # Print the values print $_*1000, "\n" for @times; } __DATA__ 2.83506E+03 2.83697E+03 2.83911E+03 2.84117E+03 2.84331E+03 2.84544E+03 2.84750E+03 2.84958E+03 2.85172E+03 2.85383E+03 2.85588E+03 2.85804E+03 2.86012E+03

    This gives me:

    $ perl foo.pl Use of uninitialized value $_ in scalar chomp at foo.pl line 5, <DATA> + line 1. INPUT LINE: 2.83506E+03 2.83697E+03 2.83911E+03 2835060 2836970 2839110 Use of uninitialized value $_ in scalar chomp at foo.pl line 5, <DATA> + line 2. INPUT LINE: 2.84117E+03 2.84331E+03 2.84544E+03 2841170 2843310 2845440 Use of uninitialized value $_ in scalar chomp at foo.pl line 5, <DATA> + line 3. INPUT LINE: 2.84750E+03 2.84958E+03 2.85172E+03 2847500 2849580 2851720 Use of uninitialized value $_ in scalar chomp at foo.pl line 5, <DATA> + line 4. INPUT LINE: 2.85383E+03 2.85588E+03 2.85804E+03 2853830 2855880 2858040 Use of uninitialized value $_ in scalar chomp at foo.pl line 5, <DATA> + line 5. INPUT LINE: 2.86012E+03 2860120

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks for the response, that seems to do what I need.

Re: getting numeric data from a file into an array
by NetWallah (Canon) on Nov 02, 2011 at 03:05 UTC
    Here is a more "perlish" Warning-free way: (Deliberately avoiding regular expressions)
    use strict; use warnings; my @values; my $special_string_found = 0; my $special_string = 'time values:'; while (defined( my $line= <DATA>)) { if (not $special_string_found ){ next unless $special_string eq substr($line,0,length($special_s +tring)); $special_string_found = 1; $line = substr($line,length($special_string)+1); } push @values, split(' ' ,$line); } print qq|$_\n| for @values; __DATA__ TIME number of steps: 13 time values: 2.83506E+03 2.83697E+03 2.83911E+03 2.84117E+03 2.84331E+03 2.84544E+03 2.84750E+03 2.84958E+03 2.85172E+03 2.85383E+03 2.85588E+03 2.85804E+03 2.86012E+03

                "XML is like violence: if it doesn't solve your problem, use more."

      I appreciate seeing the warning-free solution, and I must admit it's a little comforting to see it done without reg exp.