http://qs1969.pair.com?node_id=571945

sam_0056 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: read the file and assign each line to a hash????
by Fletch (Bishop) on Sep 08, 2006 at 12:36 UTC
Re: read the file and assign each line to a hash????
by b10m (Vicar) on Sep 08, 2006 at 12:48 UTC
    my %hash; open FH, "<", "/path/to/file" or die "Can't open file: $!"; while(<FH>) { chomp; if($_ =~ m/^([^:]+): (.*)$/) { $hash{$1} = $2; } } close FH;
    --
    b10m

    All code is usually tested, but rarely trusted.
Re: read the file and assign each line to a hash????
by radiantmatrix (Parson) on Sep 08, 2006 at 14:50 UTC

    You mean, other than using Email::Simple?

    #!/usr/bin/perl use strict; use warnings; open my FILE, '<', $ARGV[0] or die "Can't read $ARGV[0]: $!"; my %header; while (<FILE>) { chomp; # trim trailing newline my ($k, $v) = split(': ', $_, 2); $header{$k} = $v; } # now $header{To} eq 'san<sanjay@36.212.176.07>' &etc.
    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: read the file and assign each line to a hash????
by davorg (Chancellor) on Sep 08, 2006 at 13:02 UTC

    You don't want a regex. You want split.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: read the file and assign each line to a hash????
by graff (Chancellor) on Sep 09, 2006 at 01:23 UTC
    What was wrong with the code snippet I posted on your other thread (which seems to involve the same data as this one)?

    Have you really tried running any code on your own? What have you tried? If you did try the snippet I provided earlier, and had trouble with it, why didn't you post a reply there? Be explicit and clear about what you tried: the actual script, the actual input data, the actual output data, how that actually fell short of your intentions.

    Please give some relevant feedback to the suggestions you've been given, so we can help you move forward.

Re: read the file and assign each line to a hash????
by swampyankee (Parson) on Sep 08, 2006 at 18:23 UTC

    One major flaw in your concept will be exposed by files which contain "To:" more than once: Perl hash keys must be unique, which means that the hash's value will be overwritten each time "To:" shows up.

    Unless this is a rather bizarre (and poorly specified) homework assignment (it isn't, is it?), you should:

    1. Do a supersearch here, and search on CPAN for ways to parse email. You've had several pointers; follow them and pay attention.
    2. Be more forthcoming with the context of what you're trying to do. There are monks with expertise in just about all programming-related disciplines; there is a very good chance one has solved a remarkably similar problem.

    emc

    At that time [1909] the chief engineer was almost always the chief test pilot as well. That had the fortunate result of eliminating poor engineering early in aviation.

    —Igor Sikorsky, reported in AOPA Pilot magazine February 2003.
Re: read the file and assign each line to a hash????
by rsriram (Hermit) on Sep 08, 2006 at 13:34 UTC

    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.

      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.