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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: How to perform single operation to each line in file
by Corion (Patriarch) on Jan 21, 2014 at 11:38 UTC

    Your code does not compile.

    Please post code that actually compiles instead of typing it in separately.

    Also, you may want to think about what the following line is supposed to do:

    foreach(my $line = <$FH>) {

    ... maybe you want to learn about what readline resp. the diamond operator does in scalar context. Most likely, while is a better approach.

      i tried while loop also but it doesn't showing what I'm expecting
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to perform single operation to each line in file
by marto (Cardinal) on Jan 21, 2014 at 12:21 UTC

    It seems you forgot a lot of what you were shown in how read data from file. Since you ignored my other advice you'll likely ignore this pointer also.

Re: How to perform single operation to each line in file
by Anonymous Monk on Jan 21, 2014 at 12:04 UTC
    just to let us you know
    why you $/ undefining
    and also you foreaching
    when am wanting whileing
      $/ to read whole record and foreach is to read every line
        $/ tells readline or <> to read the whole file. foreach does not read anything, it just iterates a list. You give it a one member list: it contains the whole file as its only member.
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Why don't you just read the file line-by-line?

Re: How to perform single operation to each line in file
by nithins (Sexton) on Jan 21, 2014 at 12:49 UTC

    I did some changes in to your code & was able to achieve what you were looking.Here is the code

    #!/usr/bin/perl use strict; use warnings; use strict; use warnings; use diagnostics; use Modern::Perl; open FH, '<', 'Perl_file.txt' or die "Could not open file= $!"; while( <FH>) { chomp($_); my $line =$_; $line =~ s/\s+//gs; my $char_a = substr($line, 0, 2); my $char_b = substr($line,2,2); print $char_a; print $char_b; }

    Data i inserted in to Perl_file.txt was

    Raam larry marry sephali

    OUTPUT:

    Raamlarrmarrseph
      You rightly removed the line returns with chomp, you need to add them back when you print:
      print $char_a, "\n"; print $char_b, "\n";
      Additional comments: why do you have use strict and use warnings twice (actually three times since you are also using modern Perl)? Why did you change from a lexical filehandle ($FH) to a bare word filehandler (FH)?
Re: How to perform single operation to each line in file
by rammohan (Acolyte) on Jan 21, 2014 at 12:01 UTC
    sorry, I missed { this flower brackets. now i updated my question.. sorryyyy