define an array in perl

Discussion in 'Perl Misc' started by jeniffer, Jul 31, 2007.

  1. jeniffer

    jeniffer Guest

    Hi
    I am a newbie in perl. I have an array block_list :

    push ( @block_list ,$word); # this word is read from a file.
    $list_name = $block_list[$#block_list]; # i extract the last element
    ie $word in this case
    now i want to define an array with the name $list_name


    like ,
    my @"$list_name";

    But this is giving me errors...
    sorry for the stupid question,,,please help me out ,,,,
    jeniffer, Jul 31, 2007
    #1
    1. Advertising

  2. jeniffer wrote:
    > I have an array block_list :
    >
    > push ( @block_list ,$word); # this word is read from a file.
    > $list_name = $block_list[$#block_list]; # i extract the last element
    > ie $word in this case
    > now i want to define an array with the name $list_name
    >
    > like ,
    > my @"$list_name";
    >
    > But this is giving me errors...


    It can be done, but is not recommended. Consider this solution instead:

    my $word = 'Perl';
    my %lists; # declare a HoA

    push @{ $lists{$word} }, $word;

    print @{ $lists{Perl} }, "\n"; # prints 'Perl'

    You may want to read the FAQ entry

    perldoc -q "variable name"

    about why what you tried to do is not recommended.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jul 31, 2007
    #2
    1. Advertising

  3. jeniffer

    -berlin.de Guest

    jeniffer <> wrote in comp.lang.perl.misc:
    > Hi
    > I am a newbie in perl. I have an array block_list :
    >
    > push ( @block_list ,$word); # this word is read from a file.
    > $list_name = $block_list[$#block_list]; # i extract the last element


    Why don't you use $word itself? If you still have the variable, there's
    no need to access the array. If $word is no longer available, the
    last element of an array is better accessed as

    $block_list[-1];

    > ie $word in this case
    > now i want to define an array with the name $list_name
    >
    >
    > like ,
    > my @"$list_name";


    Bad plan. You're aiming for symbolic references. See "perldoc -q
    'variable as a variable name'" for why this is not a good idea.

    > But this is giving me errors...
    > sorry for the stupid question,,,please help me out ,,,,


    Use a "hash of arrays" %h where the keys are the prospective variable
    names and the values are references to the arrays. The array for
    $list_name would be

    @{ $h{ $list_name} }

    and the $n-th element of that array can be accessed through

    $h{ $list_name}->[ $n]

    See "perldoc perldsc" and "perldoc perlreftut" for more on that.

    Anno
    -berlin.de, Jul 31, 2007
    #3
  4. jeniffer

    Guest

    On 31 , 14:43, jeniffer <> wrote:
    > Hi
    > I am a newbie in perl. I have an array block_list :
    >
    > push ( @block_list ,$word); # this word is read from a file.
    > $list_name = $block_list[$#block_list]; # i extract the last element
    > ie $word in this case
    > now i want to define an array with the name $list_name
    >
    > like ,
    > my @"$list_name";
    >
    > But this is giving me errors...
    > sorry for the stupid question,,,please help me out ,,,,


    arrays starts its name from @. you cant define an array with the name
    $list_name, it looks like a string, member of array, but not the
    array. You may want to split $list_name into array. So use split().
    Example: my @arr=split('-',$list_name). It splits the string
    $list_name into an array with '-' as a delimiter.
    my $str="abc-def-123-456";
    my @array=split('-',$str);
    , Jul 31, 2007
    #4
  5. jeniffer

    mattsteel Guest


    > Hi
    > I am a newbie in perl. I have an array block_list :
    >
    > push ( @block_list ,$word); # this word is read from a file.
    > $list_name = $block_list[$#block_list]; # i extract the last element
    > ie $word in this case
    > now i want to define an array with the name $list_name
    >
    >
    > like ,
    > my @"$list_name";
    >
    > But this is giving me errors...
    > sorry for the stupid question,,,please help me out ,,,,


    This is an eval task: instead of
    my @"$list_name";
    I'd use
    eval "my \@$list_name";

    M.
    mattsteel, Aug 1, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. theotyflos
    Replies:
    3
    Views:
    466
    Thomas Matthews
    Feb 19, 2004
  2. robin liu
    Replies:
    3
    Views:
    821
    Robin Liu
    Apr 21, 2006
  3. Replies:
    23
    Views:
    846
    Chris Thomasson
    Aug 29, 2007
  4. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    460
    Brian Takita
    Jan 23, 2006
  5. Geoff Cox

    how define array of array?

    Geoff Cox, Sep 14, 2005, in forum: Javascript
    Replies:
    5
    Views:
    77
    Geoff Cox
    Sep 14, 2005
Loading...

Share This Page