Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: iterative foreach loop

by zarath (Beadle)
on Nov 15, 2017 at 11:09 UTC ( [id://1203459]=note: print w/replies, xml ) Need Help??


in reply to iterative foreach loop

Hello!

For the task you are describing I would use the following code:

use strict; use warnings; use autodie; my $filename = 'Text2.txt'; my $count = 0; open my $file,'<',$filename; while (my $line = <$file>) { ++$count; # keep track of the number of lines printf "Line $count: $line"; } close $file; exit 0;

Note: when I need to open files for any reason, I tend to use autodie; so I can forget to add or die $!; to each open.

This code literally just opens your .txt-file and prints the following: "Line " number of the line ": " content of the line. It does not do much more.

But you did mention you would like to store the lines into an array. For later use perhaps?

In this case it would become this code:

use strict; use warnings; use autodie; my $filename = 'Text2.txt'; my $count = 0; my @lines; # declare the array to be used for storing open my $file,'<',$filename; while (my $line = <$file>) { ++$count; # keep track of the number of lines push (@lines,'Line ',$count,': ',$line); # fill the initially empt +y array with the needed data } close $file; # do not need this anymore for my $newline (@lines) { printf $newline; # print the contents of the filled array } exit 0;

The result of both codes is the same, but in the second version all lines (with linecount) are stored inside the array @lines so you can easily use it for whatever you want after the while-block is closed without needing to read from $file again. This would be more difficult in the first version I think.

You can of course format the lines of your array however you like it by editing the push: push (@array,'fill','this','however','you','want');

Hope this is helpful.

Replies are listed 'Best First'.
Re^2: iterative foreach loop
by hippo (Bishop) on Nov 15, 2017 at 11:29 UTC
    for my $newline (@lines) { printf $newline; # print the contents of the filled array }

    This seems confusingly verbose to me. Why not simply

    print @lines;

    There's no need for printf, for an explicit loop or for a lexical scalar. TIMTOWTDI, but there is clarity in simplicity.

      Good points, thanks for that!

      I think I picked up some bad habits by learning Perl mainly from some old scripts I've come across at work.

      Starting to think the guy who wrote them shouldn't have...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1203459]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-23 18:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found