access variables from array and set it back

Discussion in 'Perl Misc' started by Slickuser, Apr 25, 2009.

  1. Slickuser

    Slickuser Guest

    I have these variables:
    my $path1 = 'c:\abc';
    my $filea = 'Y:\a.txt';
    my $namex = 'Dr. X';

    I want to push this into a hash array or array of variables & values.

    Later on, I want loop through these array and modify the value and set
    back to the variable.


    do a loop now
    get variable back
    change value

    $path1 = 'c:\newpath';
    $filea = 'Y:\newfilea.txt';
    $namex = 'Dr. Y';

    How can I achieve something like this? Thanks.
    Slickuser, Apr 25, 2009
    #1
    1. Advertising

  2. Slickuser <> wrote:
    >I have these variables:
    >my $path1 = 'c:\abc';
    >my $filea = 'Y:\a.txt';
    >my $namex = 'Dr. X';
    >
    >I want to push this into a hash array or array of variables & values.


    May I suggest that you get your terminology straightened out first? It
    makes communication so much easier if everyone uses the same terms for
    the same things.

    There are hashes: they are mappings from strings to scalars. In the old
    days they were also called associative arrays.
    There are arrays: they are mappings from (consecutive whole) numbers to
    scalars.
    There are variables: they can be any of scalar, array, hash, and a few
    other types.
    And there are values: they are typically stored in variables.

    Now, having said that, there are no arrays of variables.

    >Later on, I want loop through these array and modify the value and set
    >back to the variable.
    >
    >do a loop now
    >get variable back
    >change value
    >
    >$path1 = 'c:\newpath';
    >$filea = 'Y:\newfilea.txt';
    >$namex = 'Dr. Y';
    >
    >How can I achieve something like this? Thanks.


    Wild guess: maybe you are looking for a simple hash?
    %myhash = (path1 => 'c:\newpath',
    filea => 'Y:\newfilea.txt',
    namex => 'Dr. Y};

    You can loop through the entries by
    foreach (keys(%myhash) {
    print $myhash{$_};
    }
    And of course you can assign and modify the values as you please, too.

    Or are you looking for references, i.e. you want to store a reference to
    each of your variables in such a hash or an array and then work with
    those references?

    jue
    Jürgen Exner, Apr 25, 2009
    #2
    1. Advertising

  3. Slickuser

    Slickuser Guest

    I want to reference the variables and change the value to that
    reference variable.

    On Apr 24, 4:29 pm, Jürgen Exner <> wrote:
    > Slickuser <> wrote:
    > >I have these variables:
    > >my $path1 = 'c:\abc';
    > >my $filea = 'Y:\a.txt';
    > >my $namex = 'Dr. X';

    >
    > >I want to push this into a hash array or array of variables & values.

    >
    > May I suggest that you get your terminology straightened out first? It
    > makes communication so much easier if everyone uses the same terms for
    > the same things.
    >
    > There are hashes: they are mappings from strings to scalars. In the old
    > days they were also called associative arrays.
    > There are arrays: they are mappings from (consecutive whole) numbers to
    > scalars.
    > There are variables: they can be any of scalar, array, hash, and a few
    > other types.
    > And there are values: they are typically stored in variables.
    >
    > Now, having said that, there are no arrays of variables.
    >
    > >Later on, I want loop through these array and modify the value and set
    > >back to the variable.

    >
    > >do a loop now
    > >get variable back
    > >change value

    >
    > >$path1 = 'c:\newpath';
    > >$filea = 'Y:\newfilea.txt';
    > >$namex = 'Dr. Y';

    >
    > >How can I achieve something like this? Thanks.

    >
    > Wild guess: maybe you are looking for a simple hash?
    > %myhash = (path1 => 'c:\newpath',
    >                 filea => 'Y:\newfilea.txt',
    >                 namex => 'Dr. Y};
    >
    > You can loop through the entries by
    >         foreach (keys(%myhash) {
    >                 print $myhash{$_};
    >         }
    > And of course you can assign and modify the values as you please, too.
    >
    > Or are you looking for references, i.e. you want to store a reference to
    > each of your variables in such a hash or an array and then work with
    > those references?
    >
    > jue
    Slickuser, Apr 25, 2009
    #3
  4. [Re-ordered into standard reading order]
    Slickuser <> wrote:
    >On Apr 24, 4:29 pm, Jürgen Exner <> wrote:
    >> Slickuser <> wrote:
    >> >I have these variables:
    >> >my $path1 = 'c:\abc';
    >> >my $filea = 'Y:\a.txt';
    >> >my $namex = 'Dr. X';

    >>
    >> >I want to push this into a hash array or array of variables & values.

    >>
    >> May I suggest that you get your terminology straightened out first? It
    >> makes communication so much easier if everyone uses the same terms for
    >> the same things.

    [...]
    >> Or are you looking for references, i.e. you want to store a reference to
    >> each of your variables in such a hash or an array and then work with
    >> those references?


    >I want to reference the variables and change the value to that
    >reference variable.


    Then please see "perldoc perlreftut" and "perldoc perlref".

    jue
    Jürgen Exner, Apr 25, 2009
    #4
  5. Slickuser

    Slickuser Guest

    my $path1 = 'c:\abc';
    my $filea = 'Y:\a.txt';
    my $namex = 'Dr. X';

    My goal is get from the top the end.

    $path1 = 'c:\newpath';
    $filea = 'Y:\newfilea.txt';
    $namex = 'Dr. Y';


    The top will assign by manually by the user, but the bottom one will
    somehow automate re-assign the value. I don't want to do one by one.
    That's why I'm thinking of putting in a hash and loop and modify the
    value. This is where I'm stuck.

    Mostly I just do a replace and search but I don't know which variable
    to set it back to.

    How would I achieve this in a loop?
    $HASHX{'path1'} = $path1;
    $HASHX{'filea '} = $filea;



    On Apr 24, 6:20 pm, Jürgen Exner <> wrote:
    > [Re-ordered into standard reading order]
    >
    >
    >
    > Slickuser <> wrote:
    > >On Apr 24, 4:29 pm, Jürgen Exner <> wrote:
    > >> Slickuser <> wrote:
    > >> >I have these variables:
    > >> >my $path1 = 'c:\abc';
    > >> >my $filea = 'Y:\a.txt';
    > >> >my $namex = 'Dr. X';

    >
    > >> >I want to push this into a hash array or array of variables & values.

    >
    > >> May I suggest that you get your terminology straightened out first? It
    > >> makes communication so much easier if everyone uses the same terms for
    > >> the same things.

    > [...]
    > >> Or are you looking for references, i.e. you want to store a reference to
    > >> each of your variables in such a hash or an array and then work with
    > >> those references?

    > >I want to reference the variables and change the value to that
    > >reference variable.

    >
    > Then please see "perldoc perlreftut" and "perldoc perlref".
    >
    > jue
    Slickuser, Apr 27, 2009
    #5
  6. perl -le '
    $path1 = "c:\\abc";
    $filea = "Y:\\a.txt";
    $namex = "Dr. X";
    @refs = \($path1, $filea, $namex);
    @values = ("c:\\newpath", "Y:\\newfilea.txt", "Dr. Y");
    ${ $refs[$_] } = $values[$_] for 0..2;
    print for $path1, $filea, $namex;
    '
    c:\newpath
    Y:\newfilea.txt
    Dr. Y

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 27, 2009
    #6
  7. [Re-ordered AGAIN into standard reading order]
    [Please do not top post]
    Slickuser <> wrote:
    >On Apr 24, 6:20 pm, Jürgen Exner <> wrote:
    >> [Re-ordered into standard reading order]
    >> Slickuser <> wrote:
    >> >On Apr 24, 4:29 pm, Jürgen Exner <> wrote:
    >> >> Slickuser <> wrote:
    >> >> >I have these variables:
    >> >> >my $path1 = 'c:\abc';
    >> >> >my $filea = 'Y:\a.txt';
    >> >> >my $namex = 'Dr. X';

    >>
    >> >> >I want to push this into a hash array or array of variables & values.

    >>
    >> >> May I suggest that you get your terminology straightened out first? It
    >> >> makes communication so much easier if everyone uses the same terms for
    >> >> the same things.

    >> [...]
    >> >> Or are you looking for references, i.e. you want to store a reference to
    >> >> each of your variables in such a hash or an array and then work with
    >> >> those references?
    >> >I want to reference the variables and change the value to that
    >> >reference variable.

    >>
    >> Then please see "perldoc perlreftut" and "perldoc perlref".
    >>
    >> jue

    >my $path1 = 'c:\abc';
    >my $filea = 'Y:\a.txt';
    >my $namex = 'Dr. X';
    >
    >My goal is get from the top the end.
    >
    >$path1 = 'c:\newpath';
    >$filea = 'Y:\newfilea.txt';
    >$namex = 'Dr. Y';
    >
    >
    >The top will assign by manually by the user, but the bottom one will
    >somehow automate re-assign the value. I don't want to do one by one.
    >That's why I'm thinking of putting in a hash and loop and modify the
    >value. This is where I'm stuck.


    Sorry, my EPS::pSI capabilities are very limited and I am very poor at
    guessing what other people might have meant.
    Someone else will have to decode the paragraph above and translate it
    into plain English. I have no idea what you mean by that.

    >Mostly I just do a replace and search but I don't know which variable
    >to set it back to.
    >
    >How would I achieve this in a loop?
    >$HASHX{'path1'} = $path1;
    >$HASHX{'filea '} = $filea;


    I don't get it. Are you looking for references are you looking for loops
    or what? One day you are saying X and the next day you are saying Y. I
    am lost and am giving up now.

    jue
    Jürgen Exner, Apr 27, 2009
    #7
  8. Slickuser

    Slickuser Guest

    This is what I want.

    Can do I this in a loop?

    $path1 = "c:\\abc";
    $filea = "Y:\\a.txt";
    $namex = "Dr. X";
    @refs = \($path1, $filea, $namex);

    I don't want to the set the values right away. Just loop through refs
    then doing search & replace.
    And set this value to the variable.

    I'll look at this example and modify it. thank you.

    On Apr 26, 10:16 pm, Gunnar Hjalmarsson <> wrote:
    > perl -le '
    > $path1 = "c:\\abc";
    > $filea = "Y:\\a.txt";
    > $namex = "Dr. X";
    > @refs = \($path1, $filea, $namex);
    > @values = ("c:\\newpath", "Y:\\newfilea.txt", "Dr. Y");
    > ${ $refs[$_] } = $values[$_] for 0..2;
    > print for $path1, $filea, $namex;
    > '
    > c:\newpath
    > Y:\newfilea.txt
    > Dr. Y
    >
    > --
    > Gunnar Hjalmarsson
    > Email:http://www.gunnar.cc/cgi-bin/contact.pl
    Slickuser, Apr 27, 2009
    #8
  9. Slickuser wrote:
    > Gunnar Hjalmarsson wrote:
    >> perl -le '
    >> $path1 = "c:\\abc";
    >> $filea = "Y:\\a.txt";
    >> $namex = "Dr. X";
    >> @refs = \($path1, $filea, $namex);
    >> @values = ("c:\\newpath", "Y:\\newfilea.txt", "Dr. Y");
    >> ${ $refs[$_] } = $values[$_] for 0..2;
    >> print for $path1, $filea, $namex;
    >> '
    >> c:\newpath
    >> Y:\newfilea.txt
    >> Dr. Y

    >
    > This is what I want.
    >
    > Can do I this in a loop?
    >
    > $path1 = "c:\\abc";
    > $filea = "Y:\\a.txt";
    > $namex = "Dr. X";
    > @refs = \($path1, $filea, $namex);
    >
    > I don't want to the set the values right away. Just loop through refs
    > then doing search & replace.


    You have three scalar variables, so search and replace does not make
    sense IMO. Either you reassign the variables directly, or you reassign
    them indirectly via references.

    Or did I miss something?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Apr 27, 2009
    #9
    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. Joby
    Replies:
    0
    Views:
    1,627
  2. sylvia sil
    Replies:
    1
    Views:
    614
    Curt_C [MVP]
    Dec 29, 2004
  3. Replies:
    3
    Views:
    912
    Frederick Gotham
    Nov 30, 2006
  4. asdf1234234
    Replies:
    11
    Views:
    401
    wswilson
    Apr 2, 2007
  5. Replies:
    9
    Views:
    935
Loading...

Share This Page