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

Why is part of slice duplicated?

by the_0ne (Pilgrim)
on May 28, 2002 at 17:05 UTC ( [id://169838]=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks, haven't been on in awhile and haven't posted in an even longer while. Having trouble with a hash slice. Somehow I'm duplicating a slice of data, but have no idea how I'm doing it. Here's the relevant code...
$to = "foo\@bar.com"; $subject = "New CDs now"; $file_name = "AAAAA.txt"; $from = "that\@person.com"; $last_dir = "Spam"; $spam->{$to} = $to; $spam->{$to}->{$subject} = $subject; $spam->{$to}->{$subject}->{$file_name} = $file_name; $spam->{$to}->{$subject}->{$file_name}->{'path'} = $last_dir; $spam->{$to}->{$subject}->{$file_name}->{'from'} = $from; $to = "foo2\@bar.com"; $subject = "New CDs now"; $file_name = "BBBBB.txt"; $from = "that2\@person.com"; $last_dir = "Spam"; $spam->{$to} = $to; $spam->{$to}->{$subject} = $subject; $spam->{$to}->{$subject}->{$file_name} = $file_name; $spam->{$to}->{$subject}->{$file_name}->{'path'} = $last_dir; $spam->{$to}->{$subject}->{$file_name}->{'from'} = $from; print "\n"; for my $email_slice (sort keys %{$spam}) { print "email_slice - $email_slice\n"; for my $subject_slice (sort keys %{$email_slice}) { print "subject_slice - $subject_slice\n"; for my $fn_slice (sort keys %{$subject_slice}) { print "fn_slice - $fn_slice\n"; } } print "\n"; }
Sorry for the sloppy code, however, this comes from a very large script and just to figure this part out I wrote this code into a separate smaller script for debugging.
Most of you should be able to just paste this code into a vi session and run it, nothing special. Here's the output...
email_slice - foo2@bar.com subject_slice - New CDs now fn_slice - AAAAA.txt fn_slice - BBBBB.txt email_slice - foo@bar.com subject_slice - New CDs now fn_slice - AAAAA.txt fn_slice - BBBBB.txt
Notice how the fn_slice is duplicated? What I'm trying to get is...
email_slice - foo2@bar.com subject_slice - New CDs now fn_slice - BBBBB.txt email_slice - foo@bar.com subject_slice - New CDs now fn_slice - AAAAA.txt
Notice how the AAAAA.txt and BBBBB.txt are separated by their respective email addresses? I can not imagine how the fn_slice is getting duplicated? I think it has something to do with the $subject since it's the same, however, the email_slice should take care of that and separate it.

Thank you monks for your help, AGAIN! :)

Replies are listed 'Best First'.
Re: Why is part of slice duplicated?
by Abigail-II (Bishop) on May 28, 2002 at 17:18 UTC
    $spam->{$to} = $to; $spam->{$to}->{$subject} = $subject; $spam->{$to}->{$subject}->{$file_name} = $file_name; $spam->{$to}->{$subject}->{$file_name}->{'path'} = $last_dir; $spam->{$to}->{$subject}->{$file_name}->{'from'} = $from;

    You are doing some very fishy stuff here, which use strict; would not have allowed you to. First you set $spam->{$to} to $to, which is a string. But then, in the next line, with $spam->{$to}->{$subject}, you are treating it as a hashreference! And later on, you are doing the same trick with $subject and $file_name.

    You should use Data::Dumper and display the context of $spam. You will be surprised what it contains (or rather, what it doesn't contain).

    Remember, unless you know what you are doing, always use strict;.

    Abigail

      Ok, I see what you mean. I added 'use strict;' and I get...
      Can't use string ("foo@bar.com") as a HASH ref while "strict refs" in +use at ./test.pl line 14.
      How can I fill the values of this structure then?...
      # Data Structure. #$spam_hash = { # 'email@address.com' => # { # 'Subject1' => { # cache_name1 => { # path => 'path', # from => 'from' # }, # cache_name2 => { # path => 'path', # from => 'from' # } # }, # 'Subject2' => { # cache_name1 => { # path => 'path', # from => 'from' # }, # cache_name2 => { # path => 'path', # from => 'from' # } # } #};
      Until I came across a duplicated $subject, this script was working perfectly. (I guess I was getting lucky.) I need to dynamically fill the subject and cache_names to becomes slices of the email address. I'm confused on how to change my structure to one that will be functional.
        Well, I could give an answer and that will work, until you run into the situation you describe:
        Until I came across a duplicated $subject, this script was working perfectly.

        What do you want to do if you hit a duplicate subject?

        Abigail

Re: Why is part of slice duplicated?
by danger (Priest) on May 28, 2002 at 17:16 UTC

    You are accidentally using symbolic references and do not have the structure you think you have --- using strict would have told you something was wrong right away. You can see a similar problem and discussion here.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-25 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found