Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

push @array value to $var 2

Status
Not open for further replies.
May 3, 2003
90
0
0
CA
Hi All,

simple question.
I have an array and i want to store its entire value to a var.

my @a = ('1', '2', '3');

want to store @a to $c,
so $c should be = '123'

how should i proceed?
 
If you want to simply concatenate the contents of the array and store them as a single scalar, follow TomThumb's example above. Further, if you want to maintain the array and give a more convinient handle (so-to-speak), you can make a reference to the array and then use the reference.
Code:
#!perl
use strict;

my @array = ('1','2','3','blue','red');

my $array_reference = \@array; # a reference to the array

&print_array($array_reference);

# print 'blue'
print "COLOR: $$array_reference[3]\n";

sub print_array {
	my $reference = shift;
	foreach (@$reference) { print "IN THE SUB: $_\n"; }
}

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
I thank you both,

Goboating your example was what I was looking for.

Thanks again.
 
Can you please explain what is going on in this? I am lost on how references work in this:

Code:
use strict;

my @array = ('1','2','3','blue','red');

my $array_reference = \@array; # a reference to the array

&print_array($array_reference);

# print 'blue'
print "COLOR: $$array_reference[3]\n";

sub print_array {
    my $reference = shift;
    foreach (@$reference) { print "IN THE SUB: $_\n"; }
}
 
I will explain it to the best of my ability:

use strict;

my @array = ('1','2','3','blue','red');

my $array_reference = \@array; # a reference to the array

-- After the line above, $array_reference will hold a scalar value that points to the location of @array in memory.


&print_array($array_reference);

-- This line says to call the print_array sub and pass it the value of $array_reference. $array_reference is of course the memory location of @array as noted above.

# print 'blue'
print "COLOR: $$array_reference[3]\n";

-- This line says to print the scalar value found in element #3 of the array that is stored in memory at the location that is stored in $array_reference. To put it another way, think of it as these instructions:
1) Go to the memory location $array_reference
2) This is an array, so get the 4th element
3) Print the contents of the 4th element

sub print_array {
my $reference = shift;

--Remember that the contents of $array_reference was passed to this sub as an argument. Thus $reference now holds the memory location of @array.

foreach (@$reference) { print "IN THE SUB: $_\n"; }

-- For each of the elements of the array found at memory location $reference, do this.

}
 
Code:
#!perl
use strict;

# this creates a simple list named 'array'
my @array = ('1','2','3','blue','red');

# this creates a pointer or reference to the list 
# named 'array'.  This new reference (a variable) 
# is named 'array_reference'.
my $array_reference = \@array;

# Conceptually, you might imagine that the variable 
# is named 'array_reference' and it holds the value 
# 'array'.(see NOTE below)

# So, just as you would refer to the original array as 
# @array you can also refer to it as an array whose name 
# is 'array'.
# or,..... @$array_reference
#          |     |
#       an array |
#                |
#               whose name is the contents of $array_reference

# So, just as you can print 'blue' like
print "BLUE: $array[3]\n";

# You can also print 'blue' like.
print "COLOR: $$array_reference[3]\n";

# So, why is this useful?  
# A simple example.......
# You don't have to create the real array multiple times.  
# This becomes significant if the array is huge.  If you did 
# not use a reference, you would have to pass the entire 
# array into a sub routine which would duplicate it.

&print_array(@array);

sub print_array {
  # we just duplicated the array, it now exists as 
  # @array (the original)  and as @_ (local to this sub)
  foreach (@_) { print "ELEMENTS OF A SECOND ARRAY: $_\n"; }
}

&print_array_by_ref($array_reference);

sub print_array_by_ref {
  # we did not duplicate the array.
  # instead we passed a pointer to the original array
  # and de-reference the reference to get at the
  # original array.
    my $reference = shift;
    foreach (@$reference) { print "ELEMENTS OF THE ORIGINAL ARRAY: $_\n"; }
}

# NOTE: In reality it does not, but conceptually this helps 
# when trying to figure out what a reference is.
# What it really does is create of pointer to the memory
# location of the original array.  Actually, I think it 
# points to the symbol table, but that is beyond this 
# discussion.....

The usefullness of references extends well beyond this trivial example. Perl uses references to build/manage it multi-dimensional memory structures. See "Programming Perl" by Wall, Christiansen, and Schwartz for more. ;-)

'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top