LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-30-2006, 09:48 AM   #1
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Rep: Reputation: 15
Perl: printing array's index and value


Is there a way to print a out an array's index AND value using a foreach loop?

I have an array similar to this
my @numbers = (2,5,4,2,6,3,7);

I want to have printout on screen like this:
0 2
1 5
2 4
3 2
4 6
5 3
6 7

I can get the values to print out themselves, but I can not get the indexes to print.

Thanks.
 
Old 08-30-2006, 10:12 AM   #2
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
well the most obvious solution would be to have a loop counter. start it at 0 and print and increment every time through the loop
 
Old 08-30-2006, 10:24 AM   #3
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
I could do that but I was wondering if there was a way to obtain the index since it is already "available". Is there away to grab it from the array?
 
Old 08-30-2006, 10:30 AM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 61
Using the built in $. but, it starts at 1 not 0 .
Code:
perl -ne 'print "$.. $_"' file.txt
 
Old 08-30-2006, 10:57 AM   #5
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
Bummer, I was realy hoping there would be a way to capture the index number like the way you can with a hash (keys function).
 
Old 08-30-2006, 11:53 AM   #6
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by homey
Using the built in $. but, it starts at 1 not 0 .
Code:
perl -ne 'print "$.. $_"' file.txt
but that only works for lines read from an opened file handle, it has nothing to do with an index of an array.
 
Old 08-30-2006, 01:26 PM   #7
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi.

Similar to keys:
Code:
#!/usr/bin/perl

# @(#) p1       Demonstrate index with array.

use warnings;
my @numbers = (2,5,4,2,6,3,7);

foreach my $i ( 0..$#numbers ) {
        print "$i $numbers[$i]\n";
}
Resulting in:
Code:
% ./p1
0 2
1 5
2 4
3 2
4 6
5 3
6 7
 
Old 08-30-2006, 03:29 PM   #8
craig467
Member
 
Registered: Jun 2005
Location: Maine
Distribution: Red Hat 9
Posts: 65

Original Poster
Rep: Reputation: 15
Makyo,

I am just a beginner at Perl and I have not seen this syntax (0..$#numbers) before so I will have to read up on that, but it works great and I really appreciate it!

Thanks!!!
 
Old 08-30-2006, 03:50 PM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
Hi, craig467.

You're welcome.

As you will see, the ".." operator means range from least to greatest, so in the script an array of those values is generated -- you need to be careful of that, so that you don't declare an array of a few million -- although that may be a good experiment so that you can see the result.

Because perl handles so many details for you, it knows all about the basic data structures, including specific array lengths. The "$#name" construct is a way that perl provides the value of the index of the last element -- essentially the length-1.

If you are going to do serious programing in perl, I recommend Perl Best Practices, D Conway. Just keep on writing code -- one way is to check forums daily for questions and answers, and try to solve problems that look interesting.

Best wishes ... cheers, makyo

Last edited by makyo; 10-01-2006 at 03:38 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Array's.... Making An array bigger??? Tru_Messiah Programming 11 08-17-2016 01:45 PM
C - Printing ELF String Table Index nixnoobie Programming 3 04-01-2005 06:10 AM
Perl - printing features satimis Programming 6 11-14-2004 11:45 AM
perl printing pantera Programming 1 06-02-2004 02:07 PM
error printing array's phek Programming 2 06-05-2002 06:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration