in reply to reading data ...

Try this

#! perl -slw use strict; use Data::Dumper; my @data = ( [ split ',', <DATA>], [ split ',', <DATA> ], [ split ',', + <DATA> ] ); print Dumper \@data; __DATA__ "Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov"," +Dec" 3,5,7,2,4,9,6,3,6,1 8,4,1,8,9,3,5,1,3,8

Examine what is said, not who speaks.

Replies are listed 'Best First'.
Re: Re: reading data ...
by jdporter (Paladin) on Dec 18, 2002 at 04:50 UTC
    Or even just
    my @data = map { chomp; [ split ',' ] } <DATA>;
    However, that makes the month names end up with the quote marks still on them. We can add a step to strip those off:
    my @data = map { chomp; [ map { /"(.*)"/ ? $1 : $_ } split ',' ] } + <DATA>;

    jdporter
    ...porque es dificil estar guapo y blanco.

      *scratches head* That's a little obfu. Could you break down what it does in steps? This looks interesting.

        Sure, no prob.

        First, this:
        my @data = map { chomp; [ split ',' ] } <DATA>; Can reformatted as my @data = map { chomp; [ split ',' ] } <DATA>;
        Which means, "read each line of input from <DATA>, and for each line, pass it through the map block, and what comes out the other end, assign that to @data." The map block defines what happens to each line of input. The final value evaluated in the block -- its result -- is what "comes out the other end". So here, we chomp the line, split it on commas, and stuff that resulting list into a new, anonymous array. The result of the block (for each iteration, mind you) is the reference to that array. In short: for each line in, get an array-ref out.

        The enhancement is almost identical; we're just doing a bit more inside the map block:
        my @data = map { chomp; [ map { /"(.*)"/ ? $1 : $_ } split ',' ] } <DATA>;
        Instead of just schlurping the result of the split directly into the new anonymous array, we're passing those list items through a map. The point of this map is to strip off leading/trailing quote pair, if it exists. That's what the regex is for. If the match succeeds, "keep" the part inside the quotes; if it fails, keep the original string. (By "keep", I mean "Let the map block result in". Hope that's not too confusing.)

        It seems like obfuscation, but it's really just plain, albeit dense, idiomatic perl.

        jdporter
        ...porque es dificil estar guapo y blanco.

      For Ionizer, to "un"obfuscate this a little, here's a step by step approach that basically does the same thing in a different way:
      my @data = (); while (<DATA>) { # read one line chomp; # remove end-of-line character(s) s/\"//g; # remove double quotes if present push @data, [ $_ ]; # Create an anonymous array # *reference* with the current # line, and push that reference # onto the @data array. }
      I was trying to explain the obfuscated statement made by jdporter, but it was too involved.

      HTH.
        Thanks. Now I'll make a few comments and corrections.   my @data = (); That assignment isn't necessary. Arrays start out empty!      s/\"//g; That backwhack isn't necessary. Quote marks are literal in any quoted string that uses some other delimiter.      push @data, [ $_ ]; That isn't equivalent and won't work for the OP's purpose.
        You still need to split the string on commas.
        You could do it this way:
        my @a = split /,/; push @data, \@a;

        jdporter
        ...porque es dificil estar guapo y blanco.