Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Reading fixed length records

by tigervamp (Friar)
on May 25, 2001 at 17:10 UTC ( [id://83287]=perlquestion: print w/replies, xml ) Need Help??

tigervamp has asked for the wisdom of the Perl Monks concerning the following question:

The problem is that I am trying to read a fixed length of test from a file, say the first 200 characters, I tried the following:
$count=200; $/ = \$count; open(FILE1,"a.txt"); $record=<FILE1>; close(FILE1); print $record;
and the following:
$/ = \2000; open(FILE1,"a.txt"); $record=<FILE1>; close(FILE1); print $record;
but I keep getting the whole file, not a record. I tried this for Linux and Windows. What am I doing wrong?

Thanks,

tigervamp

Replies are listed 'Best First'.
(tye)Re: Reading fixed length records
by tye (Sage) on May 25, 2001 at 18:27 UTC

    What am I doing wrong?

    Trying to use this feature on a version of Perl that doesn't support it.

    Update: I'm not sure why, but I decided to expand on this. If you have Perl prior to version 5.6, then setting $/ to \20 will cause <> to read until it finds the string "SCALAR(0xbadf00d)" (approximately).

            - tye (but my friends call me "Tye")
Re: Reading fixed length records
by clintp (Curate) on May 25, 2001 at 17:16 UTC
    $/=\10; open(F, "foo.txt") || die; $rec=<F>; print length $rec, "\n"; close(F);
    Always results in 10, for files of at least 10 bytes. Are you sure your file is 2K long? That's a mighty-big record.
      Using your exact code, and a file of 52164 bytes, the beginning of which is:

      This is a test.
      This is a test.
      This is a test.
      This is a test.This is a test.
      This is a test.
      This is a test.
      ...

      I always get 52164, not 10.

      tigervamp

Re: Reading fixed length records
by Trimbach (Curate) on May 25, 2001 at 17:15 UTC
      I know that I could use pack/unpack, but I would like to know what I am doing wrong in this case, since there is a very similiar example in "Programming Perl" (pg. 666) that claims to work as described.

      tigervamp

Re: Reading fixed length records
by Vynce (Friar) on May 25, 2001 at 17:22 UTC
    What am I doing wrong?

    using <> instead of read.

    (Golf score: 24)

      Whilst I'd probably use read in this case, there's no reason why to can't use <>. Setting $/ to a reference to a number will set the number of bytes returned each time.

      --
      <http://www.dave.org.uk>

      "Perl makes the fun jobs fun
      and the boring jobs bearable" - me

Re: Reading fixed length records
by princepawn (Parson) on May 25, 2001 at 22:59 UTC

      Is it just me, or do the terms "Parse" and "FixedLength" seem to be at odds with each other? ;)

              - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://83287]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found