Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

reading data ...

by Anonymous Monk
on Dec 18, 2002 at 03:52 UTC ( #220757=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to read data into an array from a text file ( data.txt ). What does the @data actualy contain in the example below ( ref's to anonymous arrays ) ?

Once I read in the 3 lines, how do I push them onto @data correctly ?

data.txt contains 3 lines : "Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct","Nov","Dec"

use GD::Graph::lines; use GD::Graph::Map; print STDERR "Processing sample 5-1\n"; open IN, "<data.txt"; @mydata = <IN>; close IN; # # I would like to read the 3 lines in the data.txt file into data arra +y below. # The same data would be in the text file as in the data array below. # @data = ( ["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep", +"Oct","Nov","Dec"], [ (3,5,7,2,4,9,6,3,6,1) ], [ (3,5,7,6,4,9,6,3,9,1) ] ); $my_graph = new GD::Graph::lines(); $my_graph->set( x_label => 'Month', y_label => 'Measure of success', title => 'A Simple Line Graph', y_max_value => 10, y_min_value => 0, y_tick_number => 10, y_label_skip => 1, box_axis => 0, line_width => 5, ); open PNG, ">sample51.png"; binmode PNG; #only for Windows like platforms print PNG $my_graph->plot(\@data)->png; close PNG; $map = new GD::Graph::Map($my_graph, info => '%l'); open HTML, ">sample51.html"; print HTML "<HTML><BODY BGCOLOR=white>\n". ($map->imagemap("sample51.png", $ref)). "</BODY></HTML>"; close HTML; __END__
Ginder Singh.

Comment on reading data ...
Download Code
Replies are listed 'Best First'.
Re: reading data ...
by BrowserUk (Pope) on Dec 18, 2002 at 04:17 UTC

    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.

      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>;

      ...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.

        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.


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2016-05-04 08:42 GMT
Find Nodes?
    Voting Booth?