If the file is known to have Windows line separators, I prefer to read it same way that windows does (use the :crlf IO-layer) and process it as a normal file rather than use a DIY solution.
use strict;
use warnings;
use Autodie;
use Data::Dumper;
my $file = \do{
"Jay|Jay\@email|puppy|123 Street|Shirley\r\n"
."Travis|Travis\@email|puppy|456 Street|Emmy\r\n"
."Trisha|Trisha\@email|baby|789 Street|Eddie\r\n"
."Eddie|Eddie\@email|puppy|789 Street|Trisha\r\n"
."Shirley|Shirley\@email|baby|123 Street|Jay\r\n"
};
open my $fh, '<:crlf', $file ;
my @name_lines = <$fh>;
my %spouses;
my %people;
foreach my $line ( @name_lines ) {
chomp($line) ;
my @data = split '\|' , $line ;
#map { $_ =~ s/[^a-zA-Z0-9_\@\. ]//g } @data ;
#print Dumper \@data ;
my ( $name , $email , $wishlist , $address , $spouse_name ) =
+@data ;
$people{$name}{email} = $email ;
$people{$name}{wishlist} = $wishlist ;
$people{$name}{address} = $address ;
$people{$name}{spouse} = $spouse_name ;
#if ( $spouse_name ne '' ) {
if ( defined $spouse_name and $spouse_name ne '' ) {
$spouses{$name} = $spouse_name ;
}
}
print Dumper \%spouses ;
RESULT:
$VAR1 = {
'Shirley' => 'Jay',
'Jay' => 'Shirley',
'Eddie' => 'Trisha',
'Trisha' => 'Eddie',
'Travis' => 'Emmy'
};
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.