Welcome to Perlmonks.
Is this what the actual code looks like?
#!/usr/bin/perl
$string = 'test';
$inputs = 'test.txt';
open(TABLE, $inputs) or die "can't open $inputs.\n";
while ( $line = <IN> ) {
if ( $_ =~ $string ) { print $_; }
}
close(IN);
( See
Writeup Formatting Tips for info on getting your code into the node. )
If that's the case, then I see at least these problems at a glance:
- you're not reading the file you are opening. You're certainly not closing the one you're opening. open and close will tell you about what those items you're passing to open() and close() do.
-
You're also assigning the readline (<filename>) operator's results to $line, then working on $_ which is the default variable which would have otherwise taken that value. perlvar can help you figure that out.
-
You then test a scalar variable against a scalar variable without anything other than the binding operator. You need a regular expression match. perlre and Regexp Quote Like Operators operator, regexp should help with that.
You might try the below code (untested):
#!/usr/bin/perl
use strict;
use warnings;
my $string = 'test';
my $inputs = 'test.txt';
# see perlfunc
open( my $in, '<', $inputs) or die "can't open $inputs.\n";
while ( <$in> ) { # see perlsyn, perlop
if ( $_ =~ m/^$string(.*)/ ) { # see perlre
print $1; # see perlvar, perlre
}
}
close( $in ); # see perlfunc
If, on the other hand, you want to just split on the first space instead of actually matching:
#!/usr/bin/perl
use strict;
use warnings;
while ( <> ) {
# takes filenames on the command line or STDIN if none
print ( ( split )[1] );
}
perlvar,
perlop,
perlre,
perlfunc are recommended reading. Also, if you're new to programming, consider
Learning Perl. If you're a programmer new to Perl, consider
Programming Perl.
BTW, Perl is a wonderful tool, but you might also want to 'man 1 cut' if you're using a Unixy OS.
Update: The code was missing a couple of $ sigils. Thanks to ikegami for pointing it out to me.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.