Solved

@data

Posted on 2000-03-21
7
133 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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