Solved

@data

Posted on 2000-03-21
7
145 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 50 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

691 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