473,388 Members | 1,370 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,388 software developers and data experts.

File handling doubt

89
I have a file with 100 lines. They are all alphabets of 40 in length in each line. How to grab the first character of every line. I can't use the splait function as they are no delimiters. Do i have add every line into an array and get the 0th element? Any other nicer way to do?
Thanks.
Aug 20 '08 #1
2 1089
nithinpes
410 Expert 256MB
You can make use of split function without any delimiter, so that split occurs after each character.
Expand|Select|Wrap|Line Numbers
  1. while(<FILE>) {
  2. my @chars = split // ;
  3. print $chars[0];
  4. }
  5.  
Better approach would be to use a regular expression as below:
Expand|Select|Wrap|Line Numbers
  1. while(<FILE>) {
  2. print $1 if(/^(.)/);     # take out the first character
  3. }
  4.  
Aug 20 '08 #2
KevinADC
4,059 Expert 2GB
unpack() would be the best function for this purpose:

Expand|Select|Wrap|Line Numbers
  1. $foo = "test";
  2. $first = unpack('A1',$foo);
  3. print $first;
much faster than any regexp or substr() which would have been the second best option.
Aug 20 '08 #3

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

Similar topics

10
by: Steve | last post by:
Hi all i am just starting to get back into VB and i need a little help. I am writing a program that asks a user to type in a set of numbers/letters (in this case shipping containers). Once the...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
1
by: Babu | last post by:
Hi, I am a Perl newbie and have a doubt on Perl exception handling. My understanding regarding exception handling is execute a piece of code, if any exception occurs, handle the exception and...
12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
9
by: Julien Biezemans | last post by:
Hi! Here is the problem: I'd like to restrict local filesystem stream operations to one directory just like a root jail. fopen('/file.bin') would actually open /some/path/file.bin. One goal...
7
by: yogeshnelwadkar | last post by:
Hello, i have a problem with replacing c++ exception handling with structured exception handling. How to replace the " catch(...) " in c++ exception handling with, __except , a structured...
35
by: keerthyragavendran | last post by:
hi i'm downloading a single file using multiple threads... how can i specify a particular range of bytes alone from a single large file... for example say if i need only bytes ranging from...
29
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt,...
0
by: Luc The Perverse | last post by:
I am designing an application which will upload files to a remote server (incidentally HTTP post if that makes any difference). This is a "client application" which, hopefully, hundreds of people...
2
by: Jon Skeet [C# MVP] | last post by:
On Jun 3, 8:12 am, moo <ntv1...@gmail.comwrote: <snip> I seriously doubt that it'll be doing anything useful at the moment. You're locking on "this", but a new page instance is created for...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.