Solved

Perl For Each Line

Posted on 2014-02-23
6
572 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Merge files & delete row based on criteria using Perl. 1 104
Using Perl to parse rows 7 96
what are these modules in perl script 4 99
Regex rule to match two different url 5 74
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…

825 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