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: Beginner:
array from text file

 



placey
Deleted

Apr 6, 2001, 5:07 AM

Post #1 of 3 (470 views)
array from text file Can't Post

I am trying to read in a text file and store each line in an indexable array. why can I not reference the array? The code so far is as follows:


Code
# open file / read keys 
open (LISTING, "$list_file") or die "file open error";
$character = getc (LISTING);
while ($character ne ':') {
while ($character ne "\n") {
$character = getc (LISTING);
$key[$count][$char_count] = $character;
$char_count ++;
}
while ($character ne '|' and $character ne ':') {
$character = getc (LISTING);
}
if ($character eq '|') {
print "$key[$count]";
$count ++;
$character = getc (LISTING);
$character = getc (LISTING);
}
}
print "$key[$count]";
close (LISTING);

the output gives :
ARRAY(0x1c151a4)

what's wrong / is there an easier way? please help...

Edited to make the code readable by placing it inside [pre] [/pre] tags.




(This post was edited by rGeoffrey on Apr 6, 2001, 10:01 PM)


rGeoffrey
User / Moderator

Apr 6, 2001, 11:21 PM

Post #2 of 3 (462 views)
Re: array from text file [In reply to] Can't Post

The problem is in this line...

print "$key[$count]";

If you had an array of strings you could do this, but you really have an array of arrays of characters so you are trying to print the address of the pointer to the array of characters.

To make as small a change as possible you could probably get away with changing...

$key[$count][$char_count] = $character;

to...

$key[$count] .= $character;


Also you don't need the double quotes in your print statement, as it is a single variable, it will print just fine (and quicker) without them.

And you don't delare my $char_count or reset the value to 0 inside the while, so the number will carry over from one line to the next which is probably not what you want.


But more importantly, what are you trying to do?

If you just want to get a list of the rows that are separated by "\n" this would work...


Code
open (LISTING, $list_file) or die "file open error, $!"; 
@key = <LISTING>;
close (LISTING);

It looks like you don't want comment lines that begin with a ":" so you can filter them with...


Code
open (LISTING, $list_file) or die "file open error, $!"; 
@key = grep { !/^:/ } <LISTING>;
close (LISTING);

And what is the story with the "|"?

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


placey
Deleted

Apr 10, 2001, 3:02 AM

Post #3 of 3 (446 views)
Re: array from text file [In reply to] Can't Post

Thanks rGeoffrey you were spot on with the line:

$key[$count] .= $character;

I have not come across the .= operator before so I know not how it works but it reads in a file line up to a CRLF into my (now) indexable array.

My data file format is:

nowline2
2text1
2text2
2text3
2text4
2text5|
line3
3text1
3text2
3text3
3text4
3text5|
and now for 4
first record
and second
now the third
and fourth
and last:

so the pipes '|' and colons ':' indicate end or record and end of file respectively. I am writing a user updateable text file based data area for my company intranet. (useable for itinerary / any common data areas). If anybody wants a copy post an email address and I will send it... Seeing as I am only three weeks into perl the code should be very simple..

placey


 
 


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

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