Creatnig Dynamic Arrays

Posted on 2004-11-29
Last Modified: 2010-03-05
I have a string which I split into an array delimited by the underscore symbol;

my $string = d1156295_012424_PA01dc_ftxcv_ux12b1x0xxxxxxxxx_trctag_marchc_fy_cs_018
my @stringElements = split(/_/,$string);

I now whish to dynamically create an array with the name made up of two of the elements of @stringElements, i.e. $stringElements[0] and $stringElements[5] the array should be called @d1156295trctag.

I tried soing this:
my @{@stringElements[0]}.{@stringElements[5]}...but then how do I refer to it later??

I intend to fill this array with data and would like to eventually access the data by writing something like:

print @d1156295trctag[0]

How do I go about doing this and what it stuff referred to?

Thanks, - Troyh
Question by:Troyh
    LVL 5

    Accepted Solution

    What you look for is called "Symbolic References". It can be source of strange bugs, so use strict makes it illegal.
    If you're sure that that is what you want, here is an example:

    use strict;
    use warnings;

    # Your code
    my $string = "d1156295_012424_PA01dc_ftxcv_ux12b1x0xxxxxxxxx_trctag_marchc_fy_cs_018";
    my @stringElements = split(/_/,$string);

    my $name = "$stringElements[0]$stringElements[5]";

    print "$name\n";

    # you have to disable "refs" strictness" in oreder to use
    # symbolic reverences
    no strict "refs";
    push @{$name}, "Hello";
    push @{$name}, "World";
    ${$name}[2] = "Bye";

    print "${$name}[0] ${$name}[1]. ${$name}[2].\n";

    # re-enable "refs" strictnes as soon as you can
    use strict "refs";
    LVL 84

    Assisted Solution

    #It's probably safer to
    use strict;
    my %name;
    my $name=$stringElements[0].$stringElements[5];
    @{$name{$name}} = qw(Hello World Bye);
    print "@{$name{$name}}[0,1]. $name{$name}->[-1].\n";
    LVL 5

    Expert Comment

    It's the ongoing debate between safety and readablity.

    Oh, and ny the way, Troyh, you probably mean
    # printing Scalar
    print $d1156295trctag[0]

    # printing slice
    print @d1156295trctag[0]

    When printing single-element slice there's no visible difference, but there is.


    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now