Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > sorting text

Reply
Thread Tools

sorting text

 
 
jamasd@hotmail.com
Guest
Posts: n/a
 
      06-16-2004
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
 
Reply With Quote
 
 
 
 
jamasd@hotmail.com
Guest
Posts: n/a
 
      06-17-2004
wrote in message news:<. com>...

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.
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-17-2004
On Thu, 17 Jun 2004 wrote:

> wrote in message news:<. com>...
>
> 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-17-2004
wrote:
> 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:


<snip>

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


Suppose you mean:

if ( 3 > @fields ) {

or something like it.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      06-18-2004
wrote:

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
fired event Sorting which wasn't handled - sorting and SelectedIndexChanged Jason ASP .Net Web Controls 0 10-04-2006 02:19 PM
Controlling text in a Text Area or Text leo ASP General 1 12-05-2005 01:13 AM
sorting by multiple criterias (sub-sorting) Tom Kirchner Perl Misc 3 10-11-2003 05:16 PM
Any dedicated freeware text sorting utility for Windows? (besides Sorter from Aldra) smic Computer Support 1 08-18-2003 05:20 PM



Advertisments