Variables in an array

C

Cosmic Cruizer

I would like to create something similar to the following:

my $sm1 = "apple";
my $sm2 = "orange";
my $sm3 = "lemon";

my @sm_list = qw($sm1 $sm2 $sm3);

foreach (@sm_list) {
print $_;
}

I would like the output to be: apple orange lemon

I understand why this does not work, but is there some way I can change the
print statement to print get my desired output?
 
C

Cognition Peon

I would like to create something similar to the following:

my $sm1 = "apple";
my $sm2 = "orange";
my $sm3 = "lemon";

my @sm_list = qw($sm1 $sm2 $sm3);

foreach (@sm_list) {
print $_;
print "$_ ";
}

I would like the output to be: apple orange lemon

I understand why this does not work, but is there some way I can change the
print statement to print get my desired output?

The following print statement can be used instead of foreach

print @sm_list;

print statement will join list of elements with ' ' before printing.
 
J

Jürgen Exner

Cosmic said:
I would like to create something similar to the following:

my $sm1 = "apple";
my $sm2 = "orange";
my $sm3 = "lemon";

my @sm_list = qw($sm1 $sm2 $sm3);

foreach (@sm_list) {
print $_;
}

I would like the output to be: apple orange lemon

I understand why this does not work, but is there some way I can
change the print statement to print get my desired output?

Unfortunately there is, please check the FAQ for "How can I use a variable
as a variable name?"
A much better solution is to use a more appropriate data structure:

use strict;
use warnings;
my %fruit = (sm1 => 'apple',
sm2 => 'orange',
sm3 => 'lemon');
my @sm_list = qw(sm1 sm2 sm3);
foreach (@sm_list) {
print $fruit{$_};
}

jue
 
C

Cosmic Cruizer

use strict;
use warnings;
my %fruit = (sm1 => 'apple',
sm2 => 'orange',
sm3 => 'lemon');
my @sm_list = qw(sm1 sm2 sm3);
foreach (@sm_list) {
print $fruit{$_};
}

Thanks Jürgen. As hard as I try, the various data structures are a challenge
for me. I was trying to use the Perl Cookbook to come up with a solution,
but finally gave up.

Thanks again.
 
U

Uri Guttman

CP> print "$_ ";

CP> The following print statement can be used instead of foreach

CP> print @sm_list;

and how does that solve the OP's problem?

CP> print statement will join list of elements with ' ' before printing.

and that is wrong on 2 levels. print @foo will print nothing between its
elements. and if you do want something you set $,.

as for the OP, learn perl data structures. you said they are too hard
but the path you are going down (symrefs) is even harder. your choice.

read these:

perldoc perlreftut
perldoc perldsc
perldoc perllol

uri
 
T

Tore Aursand

my $sm1 = "apple";
my $sm2 = "orange";
my $sm3 = "lemon";

my @sm_list = qw($sm1 $sm2 $sm3);

foreach (@sm_list) {
print $_;
}

I would like the output to be: apple orange lemon

I understand why this does not work, but is there some way I can change the
print statement to print get my desired output?

Read the documentation:

perldoc perlop

It says that 'qw( ... )' denotes a word list which _won't_ get
interpolated, thus it'll just hold whatever - literally - you insert into
it.

You might be better of with this solution:

my @sm_list = ('apple', 'orange', 'lemon');
print join( "\n", @sm_list );
 
C

Cosmic Cruizer

CP> print "$_ ";


CP> The following print statement can be used instead of foreach

CP> print @sm_list;

and how does that solve the OP's problem?

CP> print statement will join list of elements with ' ' before printing.

and that is wrong on 2 levels. print @foo will print nothing between its
elements. and if you do want something you set $,.

as for the OP, learn perl data structures. you said they are too hard
but the path you are going down (symrefs) is even harder. your choice.

read these:

perldoc perlreftut
perldoc perldsc
perldoc perllol

uri

Uri, I agree with what you say about learning. Unfortunately, there are some
things that I have difficulty in learning. The good thing is, I generally
only ask a specific type of question once, then can continue to use the
answer I get for later projects. Each time I get an answer I copy it to my
folder of Perl snippits. That is exactly what I did with the answer that jue
posted. I realized it might appear like a hard way to learn, but it seems to
be the most productive way I am able to make progress.

Thanks
 
U

Uri Guttman

CP> The following print statement can be used instead of foreach
you didn't answer that question.


CC> Uri, I agree with what you say about learning. Unfortunately,
CC> there are some things that I have difficulty in learning. The good
CC> thing is, I generally only ask a specific type of question once,
CC> then can continue to use the answer I get for later projects. Each
CC> time I get an answer I copy it to my folder of Perl snippits. That
CC> is exactly what I did with the answer that jue posted. I realized
CC> it might appear like a hard way to learn, but it seems to be the
CC> most productive way I am able to make progress.

you are solving problems with fixed answers which is very bad in the
programming world. you need to learn how to learn and also be more
flexible. one solution is never right for all problems. a good rule to
learn is solve in the general and apply in the specific. a library of
perl snippets means you are not understanding the code and you are just
copying it. you should understand the code well enough to be able to
recreate the snippet on demand and customize it to the current
situation. i can't help you learn better but i can tell you of bad ways
to code and that is one of them.

uri
 
T

Tad McClellan

Cosmic Cruizer said:
@nwrddc01.gnilink.net>:


[ snip: use a hash instead of symrefs ]

As hard as I try, the various data structures are a challenge
for me.


Keep at it.

Choosing the correct data structure can make the code easier
to write and maintain.
 
T

Tore Aursand

you need to learn how to learn and also be more flexible. one solution
is never right for all problems. a good rule to learn is solve in the
general and apply in the specific.

Wise words. Your words somehow remind me of something Bruce Lee once
wrote (and which I frequently use in my signature);

"A teacher is never a giver of truth - he is a guide, a pointer to
the truth that each student must find for himself. A good teacher
is merely a catalyst."
 
U

Uri Guttman

TA> Wise words. Your words somehow remind me of something Bruce Lee once
TA> wrote (and which I frequently use in my signature);

TA> "A teacher is never a giver of truth - he is a guide, a pointer to
TA> the truth that each student must find for himself. A good teacher
TA> is merely a catalyst."

try this one from the famous rabbi hillel (circa 2k years ago):

i learned a lot from my teachers,
more from my peers
and the most from my students.

i espouse that often and it is so true. try teaching and you will learn
more about the subject than you could imagine.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,382
Latest member
MichaleStr

Latest Threads

Top