You did it! I just tried your first one: print "Name your friends: "; @names = split(/[ \n]+/, <STDIN>); print "I know $names[1].\n";
It works!! It'll send you to the "I know..." message if you try to skip a line, though. But the most important thing is that it'll grab an element from the user's inputted array! :-) Thank you SO much! I'm almost tempted to apply the "There's More Than One Way To Do It" philosophy to this and look for another way... (wink) -----Original Message----- From: Perry, Alan [mailto:[EMAIL PROTECTED] Sent: Wednesday, June 25, 2003 1:38 PM To: '[EMAIL PROTECTED]' Cc: Anthony Beaman Subject: RE: Array Question 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:[EMAIL PROTECTED] Sent: Wednesday, June 25, 2003 12:34 To: 'Anthony Beaman'; [EMAIL PROTECTED] 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:[EMAIL PROTECTED] Sent: Wednesday, June 25, 2003 11:01 To: [EMAIL PROTECTED] 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:[EMAIL PROTECTED] Sent: Wednesday, June 25, 2003 8:33 AM To: [EMAIL PROTECTED] 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:[EMAIL PROTECTED] Sent: 25 June 2003 13:24 To: [EMAIL PROTECTED] 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] ************************************************************************ Meteor web site http://www.meteor.ie ************************************************************************ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]