Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: School assignment

by karlgoethebier (Abbot)
on Sep 20, 2014 at 12:21 UTC ( [id://1101373]=note: print w/replies, xml ) Need Help??


in reply to School assignment

I would do it like this:

#!/usr/bin/env perl + use strict; use warnings; use IO::All; my $file = shift || die qq(No file specified!); my @lines; my $line = 1; my @found = grep { $_->name =~ /.+\/$file$/i } io(q(.))->all; die qq($file not found) unless @found; @lines = map { $_ = $line++ . qq( $_) } io( $found[0] )->slurp; print for @lines; __END__ 1 #!/usr/bin/env perl 2 3 use strict; 4 use warnings; 5 use IO::All; 6 7 my $file = shift || die qq(No file specified!); 8 my @lines; 9 my $line = 1; 10 11 my @found = grep { $_->name =~ /.+\/$file$/i } io(q(.))->all; 12 13 die qq($file not found) unless @found; 14 15 @lines = map { $_ = $line++ . qq( $_) } io( $found[0] )->slurp; 16 17 print for @lines; 18 19 __END__ 20 21

Please see also IO::All, grep, map and TMTOWTDI.

Regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-24 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found