Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

getting numeric data from a file into an array

by jmccaslin (Initiate)
on Nov 01, 2011 at 17:21 UTC ( #935161=perlquestion: print w/ replies, xml ) Need Help??
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.

Comment on getting numeric data from a file into an array
Download Code
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 (Canon) 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 (Abbot) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://935161]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (4)
As of 2014-08-31 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls