Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Combining 3 files

by garyboyd (Acolyte)
on Jun 24, 2011 at 08:22 UTC ( [id://911203]=note: print w/replies, xml ) Need Help??


in reply to Re: Combining 3 files
in thread Combining 3 files

Hmmm...still struggling with this one and getting an error :

Can't use an undefined value as an ARRAY reference at combine_primer_lists.pl line 42, <INFILE> line 1.

relating to this line:

    } @{$dataset->{$col[0]}})[0]->[2];

Could you explain what this line is doing?

#!/usr/bin/perl #22/06/2011 use strict; use warnings; use File::Slurp; use Data::Dumper; my @data; my @col; my @dataset; my $a; my $b; my @fields; my %out; open INFILE, "<Primer-Rev1" or die $!; open my $outfh, '>', "outputfile.txt" or die $!; for my $nr (1..2) { for my $line (read_file('Primer-For'.$nr)) { my @col = split(/\t/,$line); push @{$data[$nr - 1]->{shift(@col)}},\@col; } } while (<INFILE>){ @col = split(/\t+/, $_); chomp (@col); my ($header, $length, $tm, $sequence) = @col[0..3]; # expecting file3 line in @col my @results = ($col[0],$col[3]); for my $dataset (@data) { push @results,(sort { my $diff_a = $col[2] - $a->[1]; $diff_a *= -1 if $diff_a < 0; my $diff_b = $col[2] - $b->[1]; $diff_b *= -1 if $diff_b < 0; $diff_a <=> $diff_b; } @{$dataset->{$col[0]}})[0]->[2]; } }

Replies are listed 'Best First'.
Re^3: Combining 3 files
by Anonymous Monk on Jun 24, 2011 at 10:06 UTC

    That is easy, the line is confusing :)

    It is also confusing $dataset for a hashref

    perltidy

    my @results = ( $col[0], $col[3] ); for my $dataset (@data) { push @results, ( sort { my $diff_a = $col[2] - $a->[1]; $diff_a *= -1 if $diff_a < 0; my $diff_b = $col[2] - $b->[1]; $diff_b *= -1 if $diff_b < 0; $diff_a <=> $diff_b; } @{ $dataset->{ $col[0] } } )[0]->[2]; }
    is like
    my @results = ( $col[0], $col[3] ); for my $dataset (@data) { my ( $first ) = sort { my $diff_a = $col[2] - $a->[1]; $diff_a *= -1 if $diff_a < 0; my $diff_b = $col[2] - $b->[1]; $diff_b *= -1 if $diff_b < 0; $diff_a <=> $diff_b; } @{ $dataset->{ $col[0] } } push @results, $first->[2]; }
    is like
    my @results = ( $col[0], $col[3] ); for my $dataset (@data) { my @beef = @{ $dataset->{ $col[0] } }; @beef = sort { my $diff_a = $col[2] - $a->[1]; $diff_a *= -1 if $diff_a < 0; my $diff_b = $col[2] - $b->[1]; $diff_b *= -1 if $diff_b < 0; $diff_a <=> $diff_b; } @beef; push @results, $beef[0]->[2]; }

    $a and $b are globals, they're how sort works

    Is it still confusing?

      Hi anonymous monk, yes I'm still finding this confusing. You mentioned it is confusing $dataset for a hashref. I've been playing around with this for a while and just keep getting syntax errors.

        Hi anonymous monk, yes I'm still finding this confusing.

        Well hold onto your pants :)

        You mentioned it is confusing $dataset for a hashref.

        Look at the @beef equivalent example, it is this line that dies with Can't use an undefined value as an ARRAY reference

        my @beef = @{ $dataset->{ $col[0] } };
        This line is really
        my $key = $col[0] ; my $meat = $dataset->{ $key }; my @beef = @{ $meat };
        I misspoke when I said it is confusing $dataset for a hashref , in actuality, $dataset is a hashref.

        The problem is, $key is not in $dataset, so $meat is undef

        Since meat is undef, trying to treat $meat as an array by de-referencing it, triggers a warning if you have warnings on, and triggers an error, if you have strict on.

        ## no warnings or errors $ perl -e "print @{ undef() }; print 6" 6 ## a warning is issued, but 6 still gets printed $ perl -we "print @{ undef() } Use of uninitialized value in array dereference at -e line 1. 6 ## with strict the warning is fatal, 6 not printed cause program died $ perl -Mstrict -we "print @{ undef() } Can't use an undefined value as an ARRAY reference at -e line 1.
        See References quick reference

        To avoid this error, you might add

        next unless $meat;
        You don't want to disable strict :)

        Lets take apart a simpler example, one that builds @data

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://911203]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (1)
As of 2024-04-25 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found