Solved

@data

Posted on 2000-03-21
7
130 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
  • 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now