in reply to Re^2: Loop through array or filehandle
in thread Loop through array or filehandle
Just a quick example:
This prints out this:use warnings; use strict; open my $fh, '<', 'file.txt' or die $!; my @lines = ( "this is line 1; foo \n", "this is line 2: bar \n", "this is line 3: foobar \n", ); sub create_iter { my $arg = shift; if (ref $arg eq 'GLOB') { return sub {<$arg>}; } elsif (ref $arg eq 'ARRAY') { my $index = 0; return sub {$arg->[$index++]} } else { die "Unknown type\n"} } print "PRINTING FROM FH\n"; parse_line($fh); print "\n\nPRINTING FROM ARRAY\n"; parse_line(\@lines); sub parse_line { my $arg = shift; my $iter = create_iter($arg); while (my $val = $iter->()) { print $val if $val =~ /foo/; } }
PRINTING FROM FH this is line 1; foo this is line 3: foobar this is line 4; foobaz PRINTING FROM ARRAY this is line 1; foo this is line 3: foobar
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Loop through array or filehandle
by markdibley (Sexton) on Sep 30, 2016 at 09:47 UTC | |
by Laurent_R (Canon) on Sep 30, 2016 at 11:48 UTC | |
|
Re^4: Loop through array or filehandle
by markdibley (Sexton) on Sep 30, 2016 at 13:55 UTC | |
|
Re^4: Loop through array or filehandle
by markdibley (Sexton) on Sep 30, 2016 at 15:42 UTC | |
by Laurent_R (Canon) on Sep 30, 2016 at 16:57 UTC | |
by AnomalousMonk (Archbishop) on Sep 30, 2016 at 17:34 UTC | |
by Laurent_R (Canon) on Sep 30, 2016 at 22:13 UTC |