Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: read the file and assign each line to a hash????

by rsriram (Hermit)
on Sep 08, 2006 at 13:34 UTC ( [id://571962]=note: print w/replies, xml ) Need Help??


in reply to read the file and assign each line to a hash????

When you are handling text, I would recommend that you use regular expressions instead of commands like slice. Slice will be more appropriate when working with arrays

Below is the code using regex for what you requested.

#Assuming that you pass the file name as a parameter when executing th +e script. my %table; open (F1, "<$ARGV[0]") || die ("Can't open the file $ARGV[0]. $!\n"); while(<F1>) { chomp; if($_ =~ /([^:]+): ([^\n]+)/) { $table{$1} = $2; } } close F1; #printing the table. for (keys %table) { print "$_\t$table{$_}\n"; }

Using regex, I am picking two parts of the string. The first part of the string until a colon is encountered and the second part after the colon followed by a word space until a carriage return is encountered.

Replies are listed 'Best First'.
Re^2: read the file and assign each line to a hash????
by Fletch (Bishop) on Sep 08, 2006 at 13:39 UTC

    Great. And then he's going to try and use this to parse an RFC822 mail header which has continuation lines in it and it's going to miss parts of a header and then he'll be back here asking the same frelling question another time.

Log In?
Username:
Password:

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

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

    No recent polls found