BrowserUK should have a look at Ikegami's post and the perldoc entry on:
$/
If by
"each record is 164 characters long", our friend Phil really meant that each record is
164 bytes long, than Browser's solution would be fine. If on the other hand, Phil's file had a wide character in it, (that is, a single logical character that requires more than one byte of storage, for example, the pound sign £ or the trademark sign ™), he'd be smoked.
The most general way for Phil to feed fixed width fields from a file is as follows.
use strict;
my $length = 164;
my $file = 'path/to/filename.txt';
open(my $F, '<:encoding' , $file)
or die "cant open $file\n$!\n";
# you will supply the right value for 'encoding'.
# one common example is 'utf8'
while( read( $F, my $record, $length ) ){
# do something with $record
}
If Phil was sure that his text file contained no wide characters, he could omit the ':encoding' portion of the open mode;
read operates on bytes unless otherwise informed by the status of the filehandle in question.
A related issue:
To test if an in-memory scalar contains wide-characters, use the
bytes pragma and the following trick:
my $c = 'some_scalar_data';
test_for_wide_chars: {
require bytes;
if ( bytes::length($c) > length($c) ||
($] >= 5.008 && $c =~ /[^\0-\xFF]/)) {
print "i found a wide character!"
}
no bytes;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.