Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Perl For Each Line

Posted on 2014-02-23
6
Medium Priority
?
604 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
  • 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 85

Accepted Solution

by:
ozo earned 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

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 85

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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans

972 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