Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl how to read line from file with interpolation 1

rob54321

Programmer
Jul 26, 2024
4
ZA
Hi
I have the following code:

open FH, "<", "file.txt";
my $line = <FH>;
print "line = " . $line . "\n";

file.txt contains one line:
one\ntwo

The string $line is printed literally. What do I have to do
to get it to print
one
two
?
I have tried opening the file with :raw :crlf :any but nothing works
Thanks for any help.
 
Hi,
try this:

rob54321.pl
Code:
use strict;
use warnings;

open FH, "<", "file.txt";
while (my $line = <FH>) {
  chomp($line);
  print "line = $line\n";
}

Output:
Code:
$ perl rob54321.pl
line = one
line = two
...

 
Hi, thanks for the reply.
I tried this code and on my computer it still prints

one\n\two\three

as a single line with no interpolation.
I'm not sure why??.
 
You say, that your file contains only one line like this:
Code:
one[highlight #FCE94F]\n[/highlight]two
But what is [highlight #FCE94F]\n[/highlight]?
It's literally the string "\n" or is it the new line character LF = 0x0A Hex (but in that case that would be two lines) ?
 
Hi Mikrom,
I have solved this issue. I think my original question was not clear.
I want to read the line one\ntwo from a file and I wanted the \n to be interpolated.
I have written a stream line editor like sed and receive commands from a file. These lines are commands for regular expressions, so I want interpolation.

Solution 1:

Perl:
   use strict;
    use warnings;
    use String::Escape qw(unbackslash);

    open FH,"<","script.st";

    # for solution 3
    %hash_strings = ('\n' => "\n", '\t' => "\t", '\r' => "\r");

    while (my $line = <FH>) {
        chomp($line);

        # solution 1
        my $decoededstring = backslash($line);

        # solution 2
        $decodedstring = eval "\"$line\"";

        # solution 3
        $line =~ s/\\n/$hash_strings{$1}/gs;
        $decodedstring = $line;

        print "decoded string = " . $decodedstring . "\n";

    }
I think when reading a line from a file, the line is put inside 'line from from' which prevents interpolation.

You can close this post as it is solved.
Thanks for you help.
Rob
 
Hi rob54321,

as i asked if the lines literally contain the string "\n", i thought that something like this could be the solution:
Code:
use strict;
use warnings;

open FH, "<", "rob54321.txt";
while (my $line = <FH>) {
  chomp($line);
  my @line_array = split(/\\n/, $line);
  foreach my $element (@line_array) {
      print "$element\n";
  }
}

now, for example when the input file contains 2 lines
Code:
one\ntwo\nthree
foo\nbar
then the output of the script above would be:
Code:
one
two
three
foo
bar
 
Hi
Yes that also works.
Thanks for all your help Mikrom.
Rob
 

Part and Inventory Search

Sponsor

Back
Top