Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl For Each Line

Posted on 2014-02-23
6
Medium Priority
?
596 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 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

664 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