Solved

Perl For Each Line

Posted on 2014-02-23
6
430 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 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now