467,926 Members | 1,747 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,926 developers. It's quick & easy.

File handling doubt

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?
Aug 20 '08 #1
  • viewed: 979
2 Replies
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. }
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. }
Aug 20 '08 #2
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

Post your reply

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

Similar topics

9 posts views Thread by Hans-Joachim Widmaier | last post: by
1 post views Thread by Babu | last post: by
12 posts views Thread by Christian Christmann | last post: by
9 posts views Thread by Julien Biezemans | last post: by
7 posts views Thread by yogeshnelwadkar | last post: by
35 posts views Thread by keerthyragavendran | last post: by
29 posts views Thread by list | last post: by
reply views Thread by Luc The Perverse | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.