FAQ
The program below works, but I don't believe I have to make the
reference # my $svgs = \@svgs; to get it into the hash;

I think I should be able to do something like

$folders{$folder} = @svgs;
print "$folders{Ztyx}->[3]\n";

But can't get my head around how to do it (The error message ...
Can't use string ("4") as an ARRAY ref while "strict refs" in use ..)
should tell me something, but I'm not sure what.

Where am I wrong?


TIA


Owen



=========================================
#!/usr/bin/perl

use strict;

my @folders = grep -d, <*>; # 1000 odd directories
my %folders;

foreach my $folder (@folders) {
opendir( my $DB_DIR, $folder ) or die "Can't open $folder $!\n";
my @svgs =
grep { /\.svg?/ } readdir($DB_DIR);
close $DB_DIR;

my $svgs = \@svgs;
$folders{$folder} = $svgs;

}
print "@{$folders{Ztyx}}[3] \n"; #

Search Discussions

  • Uri Guttman at Feb 13, 2010 at 6:48 am
    "O" == Owen <rcook@pcug.org.au> writes:
    O> The program below works, but I don't believe I have to make the
    O> reference # my $svgs = \@svgs; to get it into the hash;

    believe it.

    O> I think I should be able to do something like

    O> $folders{$folder} = @svgs;

    why do you think that should work? a hash element can only hold one
    value. an array can be many values. also using an array in scalar
    context (as above) is defined to return its count.

    O> print "$folders{Ztyx}->[3]\n";

    O> But can't get my head around how to do it (The error message ...
    O> Can't use string ("4") as an ARRAY ref while "strict refs" in use ..)
    O> should tell me something, but I'm not sure what.

    it tells you that you assigned an array and not a reference to the hash
    element. this is all well covered in the various perl documents on
    references including several tutorials:

    perlreftut
    perllol
    perldsc
    perlref

    read those and it will (should) all make sense to you. if you still need
    help, come back here with more questions.

    uri

    --
    Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Dermot at Feb 13, 2010 at 11:31 am

    On 13 February 2010 06:42, Owen wrote:
    The program below works, but I don't believe I have to make the
    reference  #  my $svgs = \@svgs;     to get it into the hash;

    I think I should be able to do something like

    $folders{$folder} = @svgs;
    print "$folders{Ztyx}->[3]\n";

    You want a array reference there so

    $folders{$folder} = \@svgs;

    Saving you from having to make this assignment;

    my $svgs = \@svgs;
    $folders{$folder} = $svgs;

    HTH,
    Dp.
  • Shawn H Corey at Feb 13, 2010 at 12:34 pm

    Dermot wrote:
    On 13 February 2010 06:42, Owen wrote:
    The program below works, but I don't believe I have to make the
    reference # my $svgs = \@svgs; to get it into the hash;

    I think I should be able to do something like

    $folders{$folder} = @svgs;
    print "$folders{Ztyx}->[3]\n";

    You want a array reference there so

    $folders{$folder} = \@svgs;

    Saving you from having to make this assignment;

    my $svgs = \@svgs;
    $folders{$folder} = $svgs;
    You can also do both at once:

    $folders{$folder} = my $svgs = \@svgs;



    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Programming is as much about organization and communication
    as it is about coding.

    I like Perl; it's the only language where you can bless your
    thingy.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedFeb 13, '10 at 6:42a
activeFeb 13, '10 at 12:34p
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase