develooper Front page | perl.beginners | Postings from June 2003

RE: Array Question

Thread Previous | Thread Next
From:
Perry, Alan
Date:
June 25, 2003 10:37
Subject:
RE: Array Question
Message ID:
4188788C3E1BD411AA60009027E92DFD0EEAD35B@loisexc2.loislaw.com
Oops, the second batch of code has a problem...  It should read:

print "Name your friends: ";
$friends = <STDIN>;
chomp($friends);
@names = split(/ +/, <STDIN>);
print "I know $names[1].\n";

Sorry about that...

- Alan

-----Original Message-----
From: Perry, Alan [mailto:aperry@loislaw.com]
Sent: Wednesday, June 25, 2003 12:34
To: 'Anthony Beaman'; beginners@perl.org
Subject: RE: Array Question


Unlike others that are suggesting that you forget about user input, I
thought you might want to see how it can work on Windows.  I know that it
would bug me as to how to do this if I were in your position...

print "Name your friends: ";
@names = split(/[ \n]+/, <STDIN>);
print "I know $names[1].\n";

The second line is the "magic".  The "split" function takes a string and
splits it up into pieces (a list) which you can put into an array.

The first parameter is a regular expression to search for.  Assuming you
have not gotten to regular expressions yet, the parameter I used, "/[
\n]+/", say to the split function to search for one or more spaces or
end-of-line characters and that is what I want to split on.

The second parameter is the string you want to search, in this case, the
input you typed in.

The only problem with the above approach is that you will wind up with an
extra item.  Using your names, the array would contain:

$names[0] = "Sam"
$names[1] = "Joe"
$names[2] = "Sally"
$names[3] = ""

The extra null-string at the end is because of the end-of-line character at
the end of the string you typed in.  If you wanted to not have that at the
end, you could change your code to something like this:

print "Name your friends: ";
$friends = <STDIN>;
chomp($friends);
@names = split(/[ +/, <STDIN>);
print "I know $names[1].\n";

The "chomp" line above removes the end-of-line character, so you will only
have three elements in your array.

There are a lot of other tricks you can do, but I have probably confused you
enough for now...  :)

- Alan

-----Original Message-----
From: Anthony Beaman [mailto:AnthonyBeaman@trginc.com]
Sent: Wednesday, June 25, 2003 11:01
To: beginners@perl.org
Subject: RE: Array Question


The plot thickens! I can get it to work if I have a data in the array, such
as my script below:

print "Here are your friends: ";
@friends = qw (Sam Joe Sally);
print "@friends\n";
print "I know $friends[1]";

I still can't grab the element of an array that requests user input though. 


		-----Original Message-----
		From:	Derek Byrne [mailto:Derek.Byrne@meteor.ie]
		Sent:	Wednesday, June 25, 2003 8:33 AM
		To:	beginners@perl.org
		Subject:	RE: Array Question

		Hi Anthony,

		grabbed this from O'Reilly's Learning Perl :

		@rocks = qw/ bedrock slate lava /;
		foreach $rock (@rocks) {
		  $rock = "\t$rock";              # put a tab in front of
each element of
		@rocks
		  $rock .= "\n";                  # put a newline on the end
of each
		}
		print "The rocks are:\n", @rocks; # Each one is indented, on
its own line

		Still learning how to grab input from user via chomp, but
this should help
		you work out how to print out all entries in an array using
the foreach
		thingy :o)

		DerekB

		-----Original Message-----
		From: Anthony Beaman [mailto:AnthonyBeaman@trginc.com]
		Sent: 25 June 2003 13:24
		To: beginners@perl.org
		Subject: Array Question


		Hi! I'm still wallowing in Chapter 3 (Arrays. Why can't I
get it?!?!?!?
		ARGH!!!!!) of Learning Perl on Win32 Systems. I'm trying to
create an
		exercise but I'm not getting the results that I want. Here's
what I'm trying
		to do:

		I'm asking for a list of names:

		 print "Name your friends: ";
		 @names = <STDIN>;

		Then I want to pretend that I know the one of the friends.
In this case,
		I'll choose the 2nd one and here's where I'm not getting
what I want:

		  print "I know $names[1].\n";

		The output shows "I know ." 

		Isn't "$names[whatever]" what I'm supposed to use to get an
element of the
		array? I've tried this with numbers and have gotten the same
results. What
		am I doing wrong? Thanks! :-)


		-- 
		To unsubscribe, e-mail: beginners-unsubscribe@perl.org
		For additional commands, e-mail: beginners-help@perl.org


	
************************************************************************
		Meteor web site http://www.meteor.ie
	
************************************************************************


		-- 
		To unsubscribe, e-mail: beginners-unsubscribe@perl.org
		For additional commands, e-mail: beginners-help@perl.org
		

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About