Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Some Basic Help.

by invaderzard (Acolyte)
on Sep 24, 2012 at 07:53 UTC ( #995306=perlquestion: print w/ replies, xml ) Need Help??
invaderzard has asked for the wisdom of the Perl Monks concerning the following question:

use Modern::Perl open trained , 'D://ARP//libsvm-3.12//families//antioxidant//training. +scale'; open predict , 'D://ARP//libsvm-3.12//families//antioxidant//testing.s +cale'; my $counter = 0; while (<trained>) { my $train = $_; while (<predict>) { #print $_; my $predict = $1; if ($train = $predict) { #print $train; $counter = $counter + 1 } } } print $counter;

Sup guys. There's something that I need to clarify. for the line "my $predict = $1;", i want to define it as the current line of <predict> in the while function. I tried $_ and it gave me the line from <trained,> but when I tried $1 it was initialized.

What should I do to get the line from <predict>?

Comment on Some Basic Help.
Download Code
Replies are listed 'Best First'.
Re: Some Basic Help.
by nemesdani (Friar) on Sep 24, 2012 at 07:57 UTC
    $1 is the first bracketed match in a regular expression, you don't want that.

    Why didn't you write simply my $predict = $_?

    Another suggestion: use the 3 argument form of open.

    if ($train = $predict) is also wrong: You have to use == when checking.

    I'm too lazy to be proud of being impatient.
Re: Some Basic Help.
by kcott (Canon) on Sep 24, 2012 at 08:15 UTC

    G'day invaderzard,

    I see a number of issues here:

    • use Modern::Perl has no terminating semicolon.
    • Your code for opening files is not the best. Take a look at open.
    • $1 holds data you've captured with a regexp. See perlre.
    • If you follow the advice in the open documentation, you can write your while loops as while (my $line = <$file_handle>) { ... } which should solve your main problem.

    -- Ken

Re: Some Basic Help.
by Jenda (Abbot) on Sep 24, 2012 at 09:01 UTC
    while (my $train = <trained>) { while (my $predict = <predict>) { ... } seek predict, 0, 0; // need to return to the start of the predict fil +e!!! }

    Also, the condition is wrong! It should be if ($train == $predict) if you want to compare numbers or if ($train eq $predict) if you want to compare strings. Perl is happy to convert values from one form to the other, but you have to tell it which one you want. Also in case the files contained floating point numbers the comparison using == would most probably not "work". The two numbers would have to be absolutely equal which is (also due to limited precision computation) very unlikely. You will want to test whether they are within a certain range from each other instead.

    You may also want to consider reading the files just once. Choose the smaller one, read it into an array or hash (as you seem to want to test for equality a hash would work better) and then read the other file and check whether the value is present in the array/hash.

    Enoch was right!
    Enjoy the last years of Rome.

      What can you tell me about hashes? Thanks.

        Here's a quick intro.

        In your case you would not have any values in the hash, just keys. You'd read the smaller file and add the lines to a hash as keys (the values are irrelevant, use 1 for example). Then as you would read the other file you'd just check whether exists $hash{$line} and you'd know right away whether such line was in the first file or not.

        Enoch was right!
        Enjoy the last years of Rome.

Re: Some Basic Help.
by 2teez (Priest) on Sep 24, 2012 at 09:13 UTC

    just to add to others mastery advice:

    "...i want to define it as the current line of ..."

    do like so:

    ... while(defined(my $trained = <trained>)){ chomp $trained; while(defined(my $predict = <predict>)){ chomp $predict; ... } ... }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Some Basic Help.
by Anonymous Monk on Sep 24, 2012 at 08:59 UTC
    doubling up slashes in "D://" buys you nothing, it just looks odd

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995306]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2015-12-01 06:46 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (795 votes), past polls