-3

This is my data:

4601 000000000002950 000000600537060DB000000000000000 000000600537060DB 46004375010000009000282959900028252890002825280253563562 V4120 20132352013235 1003NN 10 0`.

and my Perl program

my$string = "4601 000000000002950 000000600537060DB000000000000000 000000600537060DB 46004375010000009000282959900028252890002825280253563562 V4120 20132352013235 1003NN 10 0";
   my $char = substr($string, 0, 2);
   print $char;

it prints 46.

If I have that data in a file /home/jack/Desktop/Perl_file.txt how can I achieve the same result using this file data.

4
  • What are you trying to get? substr($string, 0, 2) gets the first 2 characters from the input, so that is obviously 46. What else do you expect to happen?
    – PMF
    Jan 18, 2014 at 8:42
  • Why would you expect it to do anything other than print the substring of the first two characters? Jan 18, 2014 at 8:42
  • How get this output using filehandling.that is only my question Jan 18, 2014 at 8:44
  • Ah, you want to get the same output when reading the data from a file? Then please edit your question to make this clear.
    – PMF
    Jan 18, 2014 at 8:45

1 Answer 1

1

I think you're just asking how to read from a file in Perl?

# open file for reading
open my $fh, '<', "/home/jack/Desktop/Perl_file.txt" or die $!;
# iterate line by line 
while (my $line = <$fh>) {
    # now you can operate on $line. E.g., print, substr, whatever...
    print $line;
    # or like you have in your example
    my $char = substr($line, 0, 2);
    print $char, "\n";
}
close $fh;
2
  • 4
    Downvote? This is exactly how you should read from a file in Perl
    – chrsblck
    Jan 18, 2014 at 9:01
  • +1, but you should always: use strict; use warnings;.
    – Kenosis
    Jan 19, 2014 at 0:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.