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

Learned Ones,
I'm sure this is fairly simple, but I'd really appreciate
some help. I've got a csv file that contains data in this format.
249+69,775.83 249+67,775.93 249+65,776.11
I need to put the 1st value into an array of its own,
without the "+" sign, and the 2nd value into its array.
Thank you to all those who help me with this bit coding.
From the posts, it seems that my query was slighly obscure.
What I'm looking to do is take the before-mentioned file
and read the data into two arrays so that one array
contains
24969 24967 24965
whilst the other contains
775.83 775.93 776.11
There will be about 400 records total.
If it seems like I'm asking without due diligence, it is
only because I'm offshore at the moment, 6 weeks now,
sans books and high speed connection.
Again, sincere thanks for all the help and guidance.

Replies are listed 'Best First'.
Re: Read from file to two arrays.
by bobf (Monsignor) on Apr 15, 2007 at 04:08 UTC

    Welcome to the Monastery!

    What have you tried? The Monks here usually like to see a good faith effort*, even if the code fails horribly. :-) The following might help to get you started:

    1. use strict and warnings
    2. declare two arrays (my)
    3. open the input file
    4. read a line from the input file (see "I/O Operators" in perlop
    5. remove the newline from each line (chomp)
    6. split the line on the comma
    7. push the values from the line onto the appropriate array (I'm not sure what you meant by 'without the "+" sign'... replace it with something else or remove it entirely?)

    Give it a try, and feel free to come back when you get stuck.

    *Chances are, though, that another Monk will give you a fully working solution that you can use almost as-is. If you use it, take the time to learn how it works - you'll benefit from it more in the long run.

Re: Read from file to two arrays.
by GrandFather (Saint) on Apr 15, 2007 at 04:06 UTC

    You have a range of options depending on how typical the data you have shown is. split could be used to break up the line into the list of comma separated strings and the tr operator (look for tr/ in the Regexp Quote-Like Operators section) could be used to clean up the first field.

    What you do then depends a lot on what you want to do with the data subsequently. Shoving the values into two separate arrays is unlikely to be the most useful technique. More likely you would shove the data into a hash keyed on the first field if you want to look up values, or push data pairs to an array if you want to run through the data performing some calculation on it. Consider:

    use strict; use warnings; my @data; while (<DATA>) { chomp; my ($first, $second) = split ','; next unless defined $second; $first =~ tr/+//d; push @data, [$first, $second]; } print join "\n", map {"$_->[0], $_->[1]"} @data; __DATA__ 249+69,775.83 249+67,775.93 249+65,776.11

    prints:

    24969, 775.83 24967, 775.93 24965, 776.11

    DWIM is Perl's answer to Gödel
Re: Read from file to two arrays.
by agianni (Hermit) on Apr 15, 2007 at 04:13 UTC
    #!perl use strict; use warnings; my (@array1, @array2); while(<DATA>){ chomp; # with one regex: my ( $val1, $val2 ) = m/\A # beginning of line (\d+) # some numbers (capture) \+ # the plus sign \d+ # more numbers (don't capture) \, # the comma (escaped) ([0-9\.]+) # the second number \z # end of line /smx; push @array1, $val1; push @array2, $val2; # or you could:: # my @numbers = split /,/; # my @parts = split /\+/, $numbers[0]; # push @array1, $parts[0]; # push @array2, $numbers[1]; } print "$_\n" for @array1; print "$_\n" for @array2; __DATA__ 249+69,775.83 249+67,775.93 249+65,776.11

    Does that do the trick? I'm a little unsure about what you mean when you refer to the the first value without the "+" sign. This solution just gives you the number before the + sign.

    perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'
      my ( $val1, $val2 ) = m/\A # beginning of line (\d+) # some numbers (capture) \+ # the plus sign \d+ # more numbers (don't capture) \, # the comma (escaped) ([0-9\.]+) # the second number \z # end of line /smx;
      Why escape the comma? It isn't special in a regex.

      Anno

Re: Read from file to two arrays.
by jonadab (Parson) on Apr 15, 2007 at 12:04 UTC

    If parsing the CSV is an issue at all, you might have a look at Text::CSV. It might not be necessary, if your data are sufficiently simple, and you might get by with split as GrandFather suggests, but the first time one of the values contains an embedded comma or breaks your assumptions in some other weird way, you'll have a bug, and at that point you'll end up switching to using the CSV-parsing module to fix the bug. If that's at all likely, wouldn't it be easier to just use the module in the first place?

    Of course, if you have control over the way the data are generated, then you may be able to guarantee that will never happen, in which case split is fine and Bob is your uncle.

    -- 
    We're working on a six-year set of freely redistributable Vacation Bible School materials.