Solved

@data

Posted on 2000-03-21
7
131 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pipe to sed or perl, please help 5 127
Perl script not writing to .csv file 6 106
.properties file to call function/method 9 59
Control Number of Log Files -Perl 7 54
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…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

911 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

19 Experts available now in Live!

Get 1:1 Help Now