CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Arrays

 



Jono
stranger

Aug 17, 2001, 7:58 AM

Post #1 of 2 (474 views)
Arrays Can't Post

Hi,
I want to do this sort of thing:

open INFILE2, "$prf" or die "cannot open address.txt: $!\n";

#Array will hold pointers to arrays, ie:
#@struct = (['name1', 'addr1'],
# ['name2', 'addr2'],
# etc... );
my @records;

while(INFILE2)
{
my ($name, $addr);
#Grab 3 lines and store them, quitting on EOF:
($name, undef, $address) = (<INFILE2>, <INFILE2>, <INFILE2>) or last;

#Push new data into array:
push @records, [$name, $address];

#Skip lines until we see a divider.
while(<INFILE2>)
{
last if /----------/;
}

print "<TR><TD>$name</TD><TD>$address</TD></TR>";

}


close INFILE2;

for each record for an undefined number of records. Each one is separated by "----------" and the file I'm reading is basically just a text file, each record can be any length, but the first three lines contain Name, ??, E-mail address of each person the record exists for.
Once I've got the first three lines I want to move onto the next record and just ignore the rest of the lines in the record. Does that make sense?

Basically I want to store this data in some array (of arrays?) hence the @struct part in the code...and then use the data later in the program. Like just run through the arrays of the three lines, use the last element (e-mail address) then move onto the next one.

This code here only displays the data from the first record, Any ideas why?

I would like to print out the data for each record, for the number of records in each file (I'm running through lots of files) there can be between 1 and ~1000 records in each file. Please try to help if you can...:)

Cheers,
Jono




rGeoffrey
User / Moderator

Aug 20, 2001, 2:38 PM

Post #2 of 2 (452 views)
Re: Arrays [In reply to] Can't Post

Here is my input file (I used the same data in both files as I am lazy)...


Code
Fred 
Wilma
fflintstone@Bedrock.org
some
text
here
----------
George
Jane
gjetson@SpacelySprockets.com
some
text
here
----------
Homer
Marge
doh@MrPlow.com
some
text
here

Here is the code...


Code
#!/usr/local/bin/perl 

use strict;

my @files = ('readrecord.txt', 'readrecord2.txt');
my @results;

foreach my $prf (@files) {
my @records;
open (INFILE2, $prf) or die "cannot read from $prf: $!\n";

local $/ = "----------\n";
while (my $record = <INFILE2>) {
chomp $record;
my @parts = split ("\n", $record, 4);
push (@records, [$parts[0], $parts[2]]);
}
close INFILE2;
push (@results, \@records);
}

print <<EOD;
Content-type: text/html

<html><head>
<title>See Test Results</title>
</head>
<body>
<table border="1">

EOD

foreach my $list (@results) {
print "<tr><td colspan='2'>File had ", scalar @{$list}, " records</td></tr>\n";
foreach (@{$list}) {
print ("<tr><td>", join ("</td><td>", @{$_}), "</td></tr>\n");
}
}

print "\n</table></body></html>\n";

It takes a list of files and reads each one in turn. Inside each file it reads one record and collects the 1st and 3rd line. And the @records from each file are placed in @results.

Note that I have changed $/ inside the foreach loop so the change won't affect other parts of the program later.
The split will make an array of 4 parts, so everything after the 3rd line will stay together in the 4th part.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives