insta.gator has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. Is it possible to use a defined variable in defining another variable? See example below, which is not working.

$ecounter=0; my @file_line_contents = split (/\|/,$_); while "$ecounter < 45" { $e$ecounter=length($file_line_contents[$ecounter]); $ecounter+=1; }

I tried concatenating the variables and that did not work either. Code is probably ugly but should convey basically what I am trying to do.

Thanks in advance

Update

I am doing validation of data. I have a file of records with 46 elements in each record. I need to validate the length of each element as well as do some content validation. This is why I was creating a variable containing the length for each. Is there a better way?

Thanks for pointing out the syntax errors. I'm not surprised. I did not check that before posting question. Just wanted to get the idea across.

Replies are listed 'Best First'.
Re: Using a variable to define a variable
by ww (Archbishop) on May 08, 2015 at 16:30 UTC

    Don't fail to check syntax before posting! It doesn't get the idea across at all well.

    Don't delete your original content (even when unclearly written as your OP was1); use <strike> ...content you wish to disavow (and possibly wish you had never written)... </strike> and add fresh content with a Update: .... or Note: ....

    Poorly written, non-syntax-checked questions rarely draw as many wise responses as you've received. They're more apt to yield flames (this is not one) and downvotes. IOW, make it easy for us to help you, and you'll be almost certain to get good advice.

    Update: As a convenience, the deleted original content1 follows:

    Hi all. Is it possible to use a defined variable in defining another variable? See example below, which is not working.

    $ecounter=0; my @file_line_contents = split (/\|/,$_); while "$ecounter < 45" { $e$ecounter=length($file_line_contents[$ecounter]); $ecounter+=1; }

    I tried concatenating the variables and that did not work either. Code is probably ugly but should convey basically what I am trying to do.

    Thanks in advance

    check Ln42!

    Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
    1. code
    2. verbatim error and/or warning messages
    3. a coherent explanation of what "doesn't work actually means.

      Thanks for the advice. I'm new to Perl and to this site and still learning. I'm sure to improve with time.

Re: Using a variable to define a variable
by hippo (Archbishop) on May 08, 2015 at 15:51 UTC

    Possible? Yes. Advisable? No. Perl has arrays which would be ideal for this instead. eg.

    my $ecounter = 0; my @e = (); my @file_line_contents = split (/\|/,$_); while ($ecounter < 45) { # Note brackets, not quotation marks! $e[$ecounter] = length($file_line_contents[$ecounter]); $ecounter++; }

    But there are probably more perlish ways to achieve the same result and which would be more fault-tolerant into the bargain.

Re: Using a variable to define a variable
by AnomalousMonk (Archbishop) on May 08, 2015 at 16:01 UTC

    A "more Perlish" way to do this is something like (untested):

    my @file_line_contents = ...; my @line_lengths = map length($_), @file_line_contents;
    See map.

    Update: Or if  @file_line_contents is likely to be more than 45 elements and you want the length array to be limited to 45 (untested):

    my @file_line_contents = ...; my @line_lengths = map length($_), @file_line_contents[ 0 .. 44 ];
    See Slices in perldata.


    Give a man a fish:  <%-(-(-(-<

Re: Using a variable to define a variable
by CountZero (Bishop) on May 08, 2015 at 19:05 UTC
    You are fairly new to our Monastery, so you are yet allowed to err in your ways and learn from being reminded about your mistakes.

    It is considered very bad form to delete (parts of) your posts. Most of the comments now make no sense anymore and your node has no more usefulness for its readers.

    Please restore the original content.

    And welcome to the Monastery: may your voyage to Perl wisdom be smooth and swift.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Using a variable to define a variable
by aaron_baugher (Curate) on May 08, 2015 at 16:02 UTC

    Possible? Yes:

    $foobar = 'yes'; $key = 'bar'; print ${'foo'.$key};

    But don't do that. There is always a better way to do whatever you're trying to do. If you use strict as you always should, it won't let you.

    Aaron B.
    Available for small or large Perl jobs and *nix system administration; see my home node.

Re: Using a variable to define a variable
by edimusrex (Monk) on May 08, 2015 at 15:49 UTC
    What's the rest of the code look like? Also you have some syntax errors

    Should look like this

    $ecounter=0; my @file_line_contents = split (/\|/,$_); while ($ecounter < 45) { $ecounter=length($file_line_contents[$ecounter]); $ecounter+=1; }

    I suppose the code will work but what are you trying to accomplish with it?