Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

dumper hash incorrect?

by wolfie7873 (Novice)
on Oct 19, 2021 at 13:52 UTC ( [id://11137724]=perlquestion: print w/replies, xml ) Need Help??

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

It seems my hash has gone wonky. It's affecting key matching downstream. Any guesses as where to look first?
foreach my $line ( @name_lines ) { #$/ = '\n' ; 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 '' ) { $spouses{$name} = $spouse_name ; } } print Dumper %spouses ; $VAR1 = 'Jay'; ';AR2 = 'Shirley $VAR3 = 'Travis'; ';AR4 = 'Emmy $VAR5 = 'Trisha'; ';AR6 = 'Eddie $VAR7 = 'Eddie'; ';AR8 = 'Trisha $VAR9 = 'Shirley'; ';AR10 = 'Jay

Replies are listed 'Best First'.
Re: dumper hash incorrect?
by Corion (Patriarch) on Oct 19, 2021 at 13:54 UTC

    You have carriage return whitespace at the end of your input, most likely because your input file is formatted with "Windows" newlines, that is \r\n.

    Data::Dumper will show you that whitespace when you set $Data::Dumper::Useqq = 1.

    Personally, I like to, instead of chomp, use s!\s+$!!, to remove all kinds of whitespace at the end of input lines.

      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' };
      Bill
Re: dumper hash incorrect?
by BillKSmith (Monsignor) on Oct 19, 2021 at 15:46 UTC
    You must pass Dumper a scalar. Refer to both the DESCRIPTION and BUGS section of Data::Dumper.
    print Dumper \%spouses ;
    Bill
Re: dumper hash incorrect? (Data::Dump dd)
by Anonymous Monk on Oct 20, 2021 at 07:59 UTC
Re: dumper hash incorrect?
by perlfan (Vicar) on Oct 19, 2021 at 17:43 UTC
    Devel::Peek is useful for inspecting data structures; but noted already Data::Dumper works with references:
    print Dumper($scalar); # non-referential scalar print Dumper(\@array); # scalar reference to @array print Dumper(\%hash); # scalar reference to %hash
      Devel::Peek is useful for inspecting data structures

      you've obviously never used Devel::Peek to inspect data structures.

Log In?
Username:
Password:

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

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

    No recent polls found