in reply to Re^9: Addional "year" matching functionality in word matching script
in thread Addional "year" matching functionality in word matching script

ok, now that i know how to search the year in each title, I'm now looking into integrating this into my original script, my issue now is I don't know what every piece of my original script does (as I said I was helped putting it together). Does anyone know what the csv2hash bit does? There are a few #comments in the script which explain what each section does, but there are other bits that are blank which i don't fully understand. Could anyone break it down with comments just so I can get my head round it a bit easier?
  • Comment on Re^10: Addional "year" matching functionality in word matching script

Replies are listed 'Best First'.
Re^11: Addional "year" matching functionality in word matching script
by Cow1337killr (Monk) on Jun 28, 2016 at 16:16 UTC

    %csv2hash is a hash.

    The programmer is loading the record into the key of the hash, %csv2hash. The value of the hash is the title.

    The test program below demonstrates just that small task for one record. The programmer (i.e., the programmer that wrote this program originally) populates %csv2hash with all of the records in the file named csv2.

    #!/usr/bin/perl # csv2hash.pl perl csv2hash.pl A test program. # From http://www.perlmonks.org/?node_id=1166649 use strict; use warnings; my %csv2hash = (); + my $record = q[12278788, TV & SATELLITE WEEK 11 MAY 2013 GILLIAN ANDER +SON DOCTOR WHO NOT RADIO TIMES , http://www.example.co.uk, 12]; $_ = $record; my ($title) = $_ =~ /^.+?,\s*([^,]+?),/; #/ match the title + $csv2hash{$_} = $title; use Data::Dumper; print Dumper(%csv2hash); __END__
      Ok cheers, last question for a while while I try and learn some of this stuff, I'm currently watching some youtube videos on hash function, but what does the {$_} bit do in the  $csv2hash{$_} = $title part?

        See http://perlmaven.com/the-default-variable-of-perl, for example. (There are many other webpages that one can visit to get similar tutorials. This one was at the top of the Google search results.)

        Also, you can Google "perl $_".

        The short answer is it is a special variable in Perl.

        Let us say, it is a special special variable in Perl.

        For example, it can come into play when reading files.

        Some beginners jump through hoops just to avoid using it.

        In the case of your program, the record that is read from the file ends up in $_. I had to mimic that behavior in my test program. I put the data (i.e., the one record) into a variable called $record just because I like the descriptive name $record. I could have named it $milkshake but I didn't. Then I said Oh The program expects this data to be in the special variable $_, so I put $record into $_. Otherwise, the rest of the code is from your program. I just took a section of your code out and made another program and tested it to make sure that it does what I think it does.