CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [adamjazz1] Creating a hash and assigning this has its keys and values: Edit Log


Jul 19, 2009, 9:51 AM

Views: 8365
Re: [adamjazz1] Creating a hash and assigning this has its keys and values

In Reply To
KevinR, thanks it is working great. I do have a couple questions though.

my ($key, $value) = split(/\s+/,$line,2);

In this code above how exactly does this split statement work with the two variables. If I understand it correctly, it splits the lines by more than one white space.
What I don't understand is why do you have 2 vars declared beforehand. and after the match, why is there $line and 2? Why is 2 there?

Lets say I wanted to take the value from this hash and compare it to another piece of data later in the text to confirm that it is the correct #.

Thank you for your help KevinR.

The lines of your file look like this:

T0### ### ## ens###

as the code reads the file it assigns each line to $line

the fields in the lines are seperated by a space (or possibly by spaces) so you can use the split() function to split the lines into 2 fields using\s+ as the pattern to split the lines.
The number 2 tells the split function to only split the line once (which breaks it into 2 tokens) so its split on the first instance of \s+ found in the line and each token that the split function returns is assigned respectively to the list on the left side:

my ($key, $value) = split(/\s+/,$line,2);

Your second question is a bit vague. But to compare bits of data you use == for numbers or eq for strings (assumes you are comparing for equality and not inequality).

If you want to elaborate more on that part of your question maybe I can be more helpful.

(This post was edited by KevinR on Jul 19, 2009, 9:55 AM)

Edit Log:
Post edited by KevinR (Veteran) on Jul 19, 2009, 9:55 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives