473,378 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problem reading a text file.

Hi,

I'm facign a strange problem in reading a text file.

The contents of my text file is:

A1;B1;C1;D1
A2;B2;C2;D2
A3;B3;C3;D3


i want to split the contents based on the ";" (semi-colon).

I use the following code for it:

Expand|Select|Wrap|Line Numbers
  1.  
  2. open (OUT, "report.txt") || die "Can't open the File $!\n";
  3. my $data = <OUT>;
  4.  
  5.  
  6. my @values = split ";", $data;
  7. my $num = 0;
  8.  
  9. while($num < 12)
  10. {
  11.  
  12.     print $values[$num++];
  13.     print "<br>";
  14.  
  15. }
  16. close(OUT);
  17.  
  18. print "DONE!";
  19.  
  20.  
Im getting the following output:
--------------------------------
A1
B1
C1
D1







DONE!
---------------------------------

What surprises me, is why the remaining contents of $data is not stored in the "$values" array.

Another Question: if i try to split $data like this:

Expand|Select|Wrap|Line Numbers
  1. my @values = split "\n", $data;
  2.  
then, the output is:
------------------------------
A1;B1;C1;D1


DONE!
------------------------------

Can any1 tell me what could be the reason for the text after D1 not being displayed in both the cases.


Any help is appreciated.


Thanks,


Ravi
Jun 17 '08 #1
2 1462
KevinADC
4,059 Expert 2GB
all this does is read the first line of the file:

Expand|Select|Wrap|Line Numbers
  1. my $data = <OUT>;
thats all your code ever does, is read the first line, process the first line, and ends.

You can do this to read the entire file into an array and then loop through the array to process the lines:

Expand|Select|Wrap|Line Numbers
  1. my @data = <OUT>;
  2.  
  3. for (@data) {
  4.    .....
  5. }
or process the file line by line:

Expand|Select|Wrap|Line Numbers
  1. while (my $data = <OUT>) {
  2.    .....
Jun 17 '08 #2
Hey Kevin,

It works and fits my requirement too.

Thanks a lot.


Ravi
Jun 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
6
by: Bill Nguyen | last post by:
I'm reading a CSV file with the date colum formatted as "YYMMDD" -"070310" when viewed in notepad or similar trext editor. However, in my app, using ODBCReader, the column value becomes "70310"...
5
by: Neil Crighton | last post by:
I'm using the zipfile library to read a zip file in Windows, and it seems to be adding too many newlines to extracted files. I've found that for extracted text-encoded files, removing all instances...
1
by: stoogots2 | last post by:
I have written a Windows App in C# that needs to read a text file over the network, starting from the end of the file and reading backwards toward the beginning (looking for the last occurrence of a...
27
by: Jeff | last post by:
Im trying to figure out why I cant read back a binary file correctly. I have the following union: #define BITE_RECORD_LEN 12 typedef union { unsigned char byte; struct { unsigned char type;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.