Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^18: Addional "year" matching functionality in word matching script

by bms9nmh (Novice)
on Jul 01, 2016 at 15:13 UTC ( [id://1167004]=note: print w/replies, xml ) Need Help??


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

I can see that my  $value = $csv2hash{$csv2}; creates a variable from the words in titles stored in the csv2hash. What I'm not clear on is why is the hash is now written with a $ sign (denoting that it is a variable) rather than a % sign? Also why is $csv2 written in the curly brackets? When the hash was created it applied specifically to csv2, so why is there a need to define csv2 again in this line of code? Thanks

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

    See http://perl101.org/hashes.html, for example. (There are many other webpages that one can visit to get similar tutorials. This one was #3 in the list of search results when I Googled "hashes perl".)

    Hashes. They are simple. I give it a key, I get back a value. Very fast.

      I have been testing some code on a smaller scale and something is confusing me. Why does the  print "$csv2\n"; command at the end of this script print each full line of the csv. $csv2 has been populated with the key data of csv2hash, so I'm confused as to why it would print each line? Is each line considered a key in this instance?
      #!/bin/perl my @csv2 = (); # Creates new empty array @csv2 open CSV2, "<csv2" or die; # Creates the file handle CSV2, input +s the data from csv2 into CVS2 @csv2=<CSV2>; # populates @csv2 array with data from file handle + CSV2 close CSV2; my %csv2hash = (); # Creates empty hash csv2hash for (@csv2) { # for data in @csv2 array #chomp; my ($title) = $_ =~ /^.+?,\s*([^,]+?),/; #/define the data whic +h is the title #Indicate that title data will input into csv2hash $csv2hash{$_} = $title; } foreach my $csv2 (keys %csv2hash) { # iterates over all the keys in %c +sv2hash, assigning each one to $csv2 my $value = $csv2hash{$csv2}; print "$csv2\n"; # This prints the data line by line in the csv2

        The keys of %csv2hash are the lines:

        for (@csv2) { # for data in @csv2 array my ($title) = $_ =~ /^.+?,\s*([^,]+?),/; $csv2hash{$_} = $title; }

        Here, $_ (the current line) is used as a key into %csv2hash.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-03-29 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found