[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

@data

Posted on 2000-03-21
7
Medium Priority
?
149 Views
Last Modified: 2010-03-05
I need to fill @data with information in the following format:

@data =([date1,date2,date3,date4],[1,2,3,4],[10,20,30,40]);

using a text file which is formatted as follows:

date1,1,10
date2,2,20
date3,3,30
date4,4,40

0
Comment
Question by:benwilkinson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 2

Accepted Solution

by:
mattrope earned 200 total points
ID: 2643075
Would something like this work?

### Open file and read in the fields
open(F,"temp.txt") || die "Can't open.\n";
while($line = <F>) {
        chomp $line;
        ($field1, $field2, $field3) = split(/,/,$line);
        push @field1, $field1;
        push @field2, $field2;
        push @field3, $field3;
}
 

@data = (\@field1, \@field2, \@field3);
0
 
LVL 1

Expert Comment

by:lambda
ID: 2643080

open (FILE, "Data.txt"); # path to text file.
@File = <FILE>;
close (FILE);

foreach $line (@File)
{
      ($first, $second, $third) = split (/,/, $line);
      push (@arrFirst, $first);
      push (@arrSecond, $second);
      push (@arrThird, $third);
}

@data = (@arrFirst, @arrSecond, @arrThird);
0
 
LVL 2

Expert Comment

by:mattrope
ID: 2643094
The difference between my answer and lambda's is that my method will create a multidimensional array (an array of references to arrays) whereas lambda's will make a single array with all the data for the first field, followed by all the data for the second field, etc.  If you wanted a single dimension array, use lambda's code instead of mine.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:benwilkinson
ID: 2643103
Thanks - actually found the @data(\@1,\@2,\@3) technique seconds before the reply appeared.
Works perfectly for my needs

thnks again
0
 

Author Comment

by:benwilkinson
ID: 2643104
Thanks - actually found the @data(\@1,\@2,\@3) technique seconds before the reply appeared.
Works perfectly for my needs

thnks again
0
 
LVL 1

Expert Comment

by:lambda
ID: 2643331
mattrope :
If I had seen ur soln, I wouldn't have posted my own. Actually I saw it only after I did.
:-))
0
 
LVL 84

Expert Comment

by:ozo
ID: 2643344
while( <F> ){
   chomp;
   my $n = 0;
   for( split/,/ ){ push @{$data[$n++]},$_ }    
}
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question