Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Regex's and Multidimensional Arrays

by Mad_Mac (Beadle)
on Sep 15, 2010 at 15:16 UTC ( #860230=perlquestion: print w/ replies, xml ) Need Help??
Mad_Mac has asked for the wisdom of the Perl Monks concerning the following question:

I'm still learning Perl, and my current effort is trying to learn how to manipulate elements within a multidimensional array. Specifically, I'd like to loop over a number of arrays, performing the same operation on all the elements of all the arrays. For a test problem I have 2 arrays:

@days=qw(mon tues wed thurs fri sat sun); @cols=qw(red orange yellow green blue indigo violet);
and I want to make all of the letter "e"'s upper case. (e->E)

So, I tried this:

#! /usr/bin/perl @days=qw(mon tues wed thurs fri sat sun); @cols=qw(red orange yellow green blue indigo violet); @vars=( @days, @cols); s/e/E/g for @vars; foreach (@vars) { print "$_\n"; } print "@days\n"; print "@cols\n";
but, obviously it only updates the values in the new array @vars and not the @days or @cols arrays.

Then I tried using array references @vars=( \@days, \@cols);
but that didn't work either (prob 'cause I don't really understand array references, yet).

Is there a simple way to do this?

Thanks

Comment on Regex's and Multidimensional Arrays
Select or Download Code
Re: Regex's and Multidimensional Arrays
by toolic (Chancellor) on Sep 15, 2010 at 15:24 UTC
    A for loop can operate on multiple arrays. This will update both arrays:
    s/e/E/g for @days,@cols;
Re: Regex's and Multidimensional Arrays
by umasuresh (Hermit) on Sep 15, 2010 at 15:26 UTC
    try this
    s/e/E/g for @vars, @cols, @days;
Re: Regex's and Multidimensional Arrays
by kennethk (Monsignor) on Sep 15, 2010 at 15:28 UTC
    When you use the assignment @vars=( @days, @cols);, you flatten both arrays into a single list, which gets copied to @vars, as you have observed. The assignment @vars=( \@days, \@cols);, on the other hand, creates an array of array references, with the power to modify the original arrays as desired so long as you take the extra depth into account. You can visualize this data with the core module Data::Dumper (See How can I visualize my complex data structure?). The basic way to accomplish this would be to simply use nested foreach loops:

    for my $array_ref (@vars) { s/e/E/g for @$array_ref; } foreach (map @$_, @vars) { print "$_\n"; }

    For details on using references, give perlreftut a read.

      I think that kennethk nailed the diagnosis. Of course, the question now is, are you really wanting an array of references to arrays? If not, perhaps you can describe what you're trying to do and someone can point you in the right direction. I could be wrong, but I suspect that you really didn't want array of references to arrays.

      If you're wanting to learn more about multidimensional arrays, you might want to checkout perllol, which is on 'List of Lists' or also known as multidimensional arrays.

        Also take a look at the Data Structures Cookbook perldsc.

Re: Regex's and Multidimensional Arrays
by suhailck (Friar) on Sep 15, 2010 at 18:16 UTC
    You can also use tr operator

     tr/e/E/ for @days,@cols;

    See Transliteration
Re: Regex's and Multidimensional Arrays
by planetscape (Canon) on Sep 15, 2010 at 19:58 UTC
Re: Regex's and Multidimensional Arrays
by changma_ha (Sexton) on Sep 16, 2010 at 04:46 UTC

    hi Mad Mac .... I think this will help you.

    #! /usr/bin/perl use strict ; use warnings; my @days=qw(mon tues wed thurs fri sat sun); my @cols=qw(red orange yellow green blue indigo violet); foreach (@days ){ tr/e/E/; print "$_\n"; } foreach (@cols){ tr/e/E/; print "$_\n"; }
Re: Regex's and Multidimensional Arrays
by gnosti (Friar) on Sep 16, 2010 at 05:11 UTC
    While it appears that the OP needs to learn more about references to use multidimensional arrays, he raises a good question about iterating over multidimensional or other complex data structures. I'll just briefly put in a plug for Brad Bowman's excellent module, Data::Rmap, which offers several ways to accomplish this. In this instance:
    use Data::Rmap; @days=qw(mon tues wed thurs fri sat sun); @cols=qw(red orange yellow green blue indigo violet); @vars = (\@days, \@cols); rmap {s/e/E/g} @vars; print Dumper(\@vars); -- $VAR1 = [ [ 'mon', 'tuEs', 'wEd', 'thurs', 'fri', 'sat', 'sun' ], [ 'rEd', 'orangE', 'yEllow', 'grEEn', 'bluE', 'indigo', 'violEt' ] ];

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://860230]
Approved by kennethk
Front-paged by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-09-23 10:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (218 votes), past polls