Solved

Perl For Each Line

Posted on 2014-02-23
6
584 Views
Last Modified: 2014-02-24
I am trying to eventually do more with each line, but at the moment, I am trying to print each line of a file. I seem to be doing something wrong though, as nothing gets printed. I was trying to load the file into an array, and then use each line to do various tasks.

$filename = "file.log";

my @lines = do {
    open my $fh, "<", $filename
        or die "could not open $filename: $!";
    <$fh>;
};

my @lines = split /\n/, $str;
foreach my $line (@lines) {
print "$line \n";
}
0
Comment
Question by:stakor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 4

Expert Comment

by:pcmghouse
ID: 39880546
$retval = open(myinputfile, "<file.log");
if($retval ne 1)
{
      printf(logfile "Unable to open the file\n");
      exit
}

while(!eof(myinputfile))
{
      $mychar = getc(myinputfile);
      if($mychar eq "\n")
      {
        print "$line \n"
                  }
        else
      {
        $line = join('',$line,$mychar)
      }
}
close(myinputfile)
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39880565
my @lines = do {
    open my $fh, "<", $filename
        or die "could not open $filename: $!";
    <$fh>;
};
foreach my $line (@lines) {
print "$line \n";
}

#or

my $str = do {
    local $/;
    open my $fh, "<", $filename
        or die "could not open $filename: $!";
    <$fh>;
};
my @lines = split /\n/, $str;
foreach my $line (@lines) {
print "$line \n";
}

#or

open my $fh, "<", $filename
        or die "could not open $filename: $!";
for( <$fh> ){
  print;
}

# or

open my $fh, "<", $filename
        or die "could not open $filename: $!";
while( <$fh> ){
  print;
}

#or

{local @ARGV=($filename);
  while( <> ){
     print;
  }
}
0
 

Author Comment

by:stakor
ID: 39881155
When I used the first example, it opened the file, and read the contents, but when it printed the results out, it seemed to do so all on one line. When I added extra 'new lines' to the end of the print statement, it was still the same.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 39881432
Sorry, I didn't notice the "\n", which would be necessary in the
my @lines = split /\n/, $str;
version, but not in the other versions.
0
 

Author Comment

by:stakor
ID: 39881829
Sorry that comment was for pcmghouse's solution. I am using:

my @lines = do {
    open my $fh, "<", $filename
        or die "could not open $filename: $!";
    <$fh>;
};
foreach my $line (@lines) {
print "$line \n";
}
0
 
LVL 84

Expert Comment

by:ozo
ID: 39881839
print "$line \n";
is fine if you want to add a extra space and newline after each line, otherwise, just
print $line;
should suffice.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl, group, sort, count question 6 158
Which version of C should I use when Perl is too slow? 13 200
Bash one liner, start and end time calculation 13 125
iSeries PERL Scripts 7 207
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question