sorting text

J

jamasd

Here is an example of the text I am running the program on (they are
separated by tabs):

1234123 jaesdf ytkyk 345234
1264345 ghgfdf ghjhg 657658
3456765 sdasdf ytkyk 456543
1231232 assffg werwe 123454
5447454 asdqfr ytkyk 254364

I would like to create hash that contains "ytkyk" and "ghjhg". The
program needs to read the hash and search only the third column for
similar text. If the column contains the text, the line (row) needs to
be printed.

Here is what I have so far. This program just reads the entire thing
and prints lines that match:

open( File, '<', location of file.txt' ) or die "$!\n";
while ( <File> ) {
next unless ( index($_, 'ytkyk') >= 0 );
next unless ( index($_, 'ghjhg') >= 0 );
print;
}
close( File );

Thank you very much
 
J

jamasd

(e-mail address removed) wrote in message
I tried running this program and it comes up with an error. What part
of it is incorrect:

use strict;
use warnings;

my ( $buffer , @fields , $filename , %hash1 );

$filename = 'C:\Documents and Settings\vhlab\Desktop\doc.txt';
open(INPUT,"<$filename") or
die("Can't open file \"$filename\" : $!\n");

%hash1 = ( "ytkyk" => 1 , "ghjhg" => 1 );

while ( $buffer = <INPUT> ) {
chomp $buffer;
@fields = split(/\t+/,$buffer);
if ( 2 < @fields ) { # Ignore if less than 3 fields
next;
}
unless ( exists $hash1{$fields[2]} ) {
next;
} print "$buffer\n";
}
close INPUT;

Thank you.
 
P

Paul Lalli

(e-mail address removed) wrote in message
I tried running this program and it comes up with an error. What part
of it is incorrect:

use strict;
use warnings;

my ( $buffer , @fields , $filename , %hash1 );

$filename = 'C:\Documents and Settings\vhlab\Desktop\doc.txt';
open(INPUT,"<$filename") or
die("Can't open file \"$filename\" : $!\n");

%hash1 = ( "ytkyk" => 1 , "ghjhg" => 1 );

while ( $buffer = <INPUT> ) {
chomp $buffer;
@fields = split(/\t+/,$buffer);
if ( 2 < @fields ) { # Ignore if less than 3 fields
next;
}

This doesn't do what the comment says it does. This ignores if more than
2 fields. Is that the problem you're talking about?

unless ( exists $hash1{$fields[2]} ) {
next;
} print "$buffer\n";
}
close INPUT;

We're not mind readers. What error, exactly, did it come up with? (Also,
please attempt to properly indent your code when posting. If your news
reader program is mangling the whitespace, consider getting a better news
reader).

Paul Lalli
 
G

Gunnar Hjalmarsson

I tried running this program and it comes up with an error.

What kind of error? If you mean that it runs *without* errors or
warnings, even if it doesn't print anything, you should have said so
to make it easier to help you.
What part of it is incorrect:

if ( 2 < @fields ) { # Ignore if less than 3 fields

Suppose you mean:

if ( 3 > @fields ) {

or something like it.
 
J

Joe Smith

if ( 2 < @fields ) { # Ignore if less than 3 fields
next;
}

That test will ignore lines with 3, 4 or more fields; the
exact opposite of what you want. It will allow lines
with 1 or 2 fields, which is guarenteed to cause an
"uninitialized variable" error with {$fields[2]}.

I recommend that you use perl idioms instead of C.

next if @fields < 3; # Skip current line if less than 3 fields.

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top