in reply to Arrays and Strings in Hashes

Hashes associate strings with scalars. References to arrays are scalars, so that's no problem.

my %hash = ( url => 'http://www.example.org/', newArray => [], lastArray => [], );

Mind you, newArray and lastArray are awful names for variable.

Replies are listed 'Best First'.
Re^2: Hashes and String Variables
by mr_p (Scribe) on Jun 14, 2010 at 00:33 UTC
    Thanks for your help. I won't use newArray and lastArray...lol..curLinks and prevLinks.

      I tried the following. Can you tell me what I am doing wrong here? Thank You.

      my @files = qw( http://use.perl.org/ http://search.cpan.org/ http://jobs.perl.org/ ); my @hash = ( url => '', curArray => [], prevArray => [], ); foreach my $url2 ( @files ) { #push (@hash{url}, $url2); $hash{url} = $url2; }
        Can you tell me what I am doing wrong here?
        • You should use strict and warnings because they will give you clues to your problems.
        • $hash{url} = $url2; will keep overwriting the last value of the literal key 'url' inside your for loop.
        • Read perldsc.
        use warnings; use strict; my @files = qw( http://use.perl.org/ http://search.cpan.org/ http://jobs.perl.org/ ); my %hash = ( curArray => [], prevArray => [], ); foreach my $url2 ( @files ) { push @{ $hash{url} }, $url2; } use Data::Dumper; print Dumper(\%hash); __END__ $VAR1 = { 'prevArray' => [], 'curArray' => [], 'url' => [ 'http://use.perl.org/', 'http://search.cpan.org/', 'http://jobs.perl.org/' ] };
Re^2: Hashes and String Variables
by mr_p (Scribe) on Jun 14, 2010 at 15:07 UTC
    Hello again,

    Can anyone help me with my original question. I am trying everything I can think of but not working.

    Thanks.