By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,372 Members | 1,950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,372 IT Pros & Developers. It's quick & easy.

File handling doubt

P: 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
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
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
Expert 2.5K+
P: 4,059
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.