sorting text

Discussion in 'Perl Misc' started by jamasd@hotmail.com, Jun 16, 2004.

  1. Guest

    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
     
    , Jun 16, 2004
    #1
    1. Advertising

  2. Guest

    wrote in message news:<>...

    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.
     
    , Jun 17, 2004
    #2
    1. Advertising

  3. Paul Lalli Guest

    On Thu, 17 Jun 2004 wrote:

    > wrote in message news:<>...
    >
    > 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
     
    Paul Lalli, Jun 17, 2004
    #3
  4. 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
     
    Gunnar Hjalmarsson, Jun 17, 2004
    #4
  5. Joe Smith Guest

    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
     
    Joe Smith, Jun 18, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    1,441
    James Kanze
    Jul 6, 2010
  2. Jason
    Replies:
    0
    Views:
    390
    Jason
    Oct 4, 2006
  3. leo
    Replies:
    1
    Views:
    289
    Bob Lehmann
    Dec 5, 2005
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    476
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    161
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page