Re: any ruby programmers who are perl users?

2008-06-06 Thread Octavian Rasnita
- Original Message -  Whee! Inflammatory topic!
 
 1) If you are interested in learning to program for the sake of
 programming (vs getting XYZ done), Perl probably isn't the best first
 choice. (Not that its the last choice or anything, but not first.) I'd
 personally advocate C/C++ or maybe Java. Maybe make Perl a third
 choice ;)

Why?
I use perl as my primary programming language and I've made web apps with it, 
desktop apps for Windows, portable desktop apps for Windows and Linux. If I 
don't need to work in a very big team of programmers and I don't need the 
programming language to force us all to use the same style and syntax, why 
should I use C++ or Java?

Octavian



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: any ruby programmers who are perl users?

2008-06-06 Thread Dr.Ruud
Octavian:
 Yitzle:

 1) If you are interested in learning to program for the sake of
 programming (vs getting XYZ done), Perl probably isn't the best first
 choice. (Not that its the last choice or anything, but not first.)
 I'd personally advocate C/C++ or maybe Java. Maybe make Perl a third
 choice ;)

 Why?
 I use perl as my primary programming language and I've made web apps
 with it, desktop apps for Windows, portable desktop apps for Windows
 and Linux. If I don't need to work in a very big team of programmers
 and I don't need the programming language to force us all to use the
 same style and syntax, why should I use C++ or Java?

Yitzle, who was dearly missing from the attribution section in your
posting, mentioned Perl as not being his first choice for *learning* to
program. Now read his reply again.

-- 
Affijn, Ruud

Gewoon is een tijger.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: sprintf format question

2008-06-06 Thread April
On Jun 4, 8:00 am, [EMAIL PROTECTED] (Rob Dixon) wrote:
 April wrote:
  sprintf( %s%$Fmt%s, (%$Fmt=|, $TestStr, |))

  This is in Perl for Dummies, 4th ed, p160.

  I'm trying to understand this ...

  the first part, %s%$Fmt%s, my understanding is the format part,
  which specifies the formats for the second part, thelist part, (%
  $Fmt=|, $TestStr, |): %s for %$Fmt=|, %$Fmt for $TestStr, and %s
  for |, respectively.  Is this correct?

  Then what is %$Fmt, it seems a % for format and then a variable $Fmt,
  the book did not mention any format string like this ...

  Anyone can shed some light?  Thanks!

 Hi April

 The value of $Fmt will be interpolated into the double-quoted sting before it 
 is
 used as a format. It needs to be one of the single-character format 
 conversion.
 If you're not clear what I mean then try running this piece of code.

 for my $Fmt ('s', 'd') {
   print %s%$Fmt%s, \n;

 }

 HTH,

 Rob- Hide quoted text -

 - Show quoted text -

I understand now Rob, thanks!


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Grep question

2008-06-06 Thread Dr.Ruud
David Gilden schreef:

 my $Comments = param('(Comments');

There is an extra '(' in there that you might not want.

Do you use Capitalization to show that the variable is initialised with
the value of a parameter?
Consider $p_comments or $param_comments or $param{Comments}.

-- 
Affijn, Ruud

Gewoon is een tijger.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: any ruby programmers who are perl users?

2008-06-06 Thread Octavian Rasnita
From: Dr.Ruud [EMAIL PROTECTED] Octavian:
 Yitzle:
 
 1) If you are interested in learning to program for the sake of
 programming (vs getting XYZ done), Perl probably isn't the best first
 choice. (Not that its the last choice or anything, but not first.)
 I'd personally advocate C/C++ or maybe Java. Maybe make Perl a third
 choice ;)

 Why?
 I use perl as my primary programming language and I've made web apps
 with it, desktop apps for Windows, portable desktop apps for Windows
 and Linux. If I don't need to work in a very big team of programmers
 and I don't need the programming language to force us all to use the
 same style and syntax, why should I use C++ or Java?
 
 Yitzle, who was dearly missing from the attribution section in your
 posting, mentioned Perl as not being his first choice for *learning* to
 program. Now read his reply again.
 
 -- 
 Affijn, Ruud

Oh yes I'm sorry. I didn't understand it well. The problem is not the 
learning part, but using it for the sake of programming only.
Perl it is also very good for learning, unless the programmer intends to learn 
some other programming languages.

If someone starts with perl is very fine, but if that person will want to learn 
other programming languages, he might consider them all very strange and 
different, so Yitzle is right.

Octavian





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Testing for a condition immediately after require

2008-06-06 Thread Travis Thornhill
I want to be able to tag a module with one or more tags that can be tested for 
after requiring it.
nbsp;
require 'module';
lt;test for existence of tags in 'module'gt;
nbsp;
Any ideas on the best way to accomplish this?
nbsp;
Thanks in advance
nbsp;


  

TCP apllications

2008-06-06 Thread Arun
Hi,
This is Arun here, i am new to Perl so i
was just thinking of programming the TCP apllications or working. So
how do i do that, just needed guidance from you.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: TCP apllications

2008-06-06 Thread Rob Dixon
Arun wrote:
 Hi,
 This is Arun here, i am new to Perl so i
 was just thinking of programming the TCP apllications or working. So
 how do i do that, just needed guidance from you.


A lot depends on how you want to use TCP, but take a look at

  perldoc -f socket

and

  perldoc Socket

HTH,

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: TCP apllications

2008-06-06 Thread Jeff Peng
On Fri, Jun 6, 2008 at 7:29 PM, Arun [EMAIL PROTECTED] wrote:
 Hi,
 This is Arun here, i am new to Perl so i
 was just thinking of programming the TCP apllications or working. So
 how do i do that, just needed guidance from you.


Licoln Stein's book Network Programming with Perl is great for you.

-- 
Jeff Peng - [EMAIL PROTECTED]
Professional Squid supports in China
http://www.ChinaSquid.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: any ruby programmers who are perl users?

2008-06-06 Thread Richard Lee

zentara wrote:

On Thu, 05 Jun 2008 21:16:42 -0400, [EMAIL PROTECTED] (Richard Lee)
wrote:

  
Most likely just out of curiosity factor, I picked up a book Learning 
to program  the facets of ruby series..



See what some experienced Perl programmers say about Ruby
http://perlmonks.org?node_id=689996

zentara


  

Hi, I read that and yes it is very helpful.
I was just listening to podcasting and I heard Chad Fowler(my job went 
to india: 52 ways to save your job) talking about programming language

in general.
One of the quotes he says that I felt like it was for me was that, You 
really need to get out of your comfort zone time to time or else your 
comfort zone will

be one of your limiting factor

now I am still a beginner at perl but I have been using perl(I know no 
other programming languages) and I am really attached to the perl and 
wanted to
get some other perspective and since I have been hearing so much about 
ruby..


perl  is SO useful that (hmm how should I say, language itself and also 
the community) sometimes I lose sight on science of programming.
I guess perl could be a bad language as first programming experince 
because perl just works so well and now that I have spend close to year, 
I find myself
going backwards to learn some of whys and hows  But now that I have 
spend close to year, I am really happy :-)


I am not sure if learning another language while I am still newbie at my 
first language though.
But giving that perl touches up on just too many areas, I feel like I 
should include another language just to get a fresh views...










--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: any ruby programmers who are perl users?

2008-06-06 Thread Rob Dixon
yitzle wrote:

 1) If you are interested in learning to program for the sake of
 programming (vs getting XYZ done), Perl probably isn't the best first
 choice. (Not that its the last choice or anything, but not first.) I'd
 personally advocate C/C++ or maybe Java. Maybe make Perl a third
 choice ;)

I wonder why you say that?

I have been programming in C and C++ for twenty years and in Perl for ten years.
I also know a dozen other languages well, and have taught programming both in
class and privately. Unless someone needs to know another language for a
specific purpose I always recommend Perl, as most people - especially those who
have never come across a programming language before - find it more intuitive
than other languages.

The obvious advantages for a learning language are

- You write the code and run it: there is no compile/link/run cycle

- It has a built-in debugger if you need one

- Memory allocation is invisible, so there is no need to presize data structures

- It handles all of the complex ideas like references and object orientation in
  with simple syntax conventions, and gives intelligible warnings instead of
  memory access violations

- Finally, and above all, the Perl parser is highly oriented towards DWIM, which
  is ideally suited to people who learn programming

- Perl puts you in control, and is friendly. When I teach C I find myself
  apologizing for the language and brusque error messages. Programmers are
  slapped for knowing only 95% of the language, and there is always confusion
  over whether an error is a compile, link, or run time fault.

  In contrast my Perl students feel encouraged by the compiler's error
  messages, as if it was the next step on a treasure trail.

 2) If you are still picking up Perl and want to get into OOP, its
 probably better to stick with Perl and learn OOP rather than starting
 in on Ruby. The best way to learn OOP IMHO would be to come up (or get
 someone else to) with some sort of OOP project and then go ahead and
 implement it.

I agree. Perl copies the way most languages implement object orientation but has
very loose boundaries so that a mix of OO and procedural programming works fine.
Most importantly, improper semantics often /do/ something instead of just
throwing an error; that lets you see call chains and data flow, which leads to
better understanding, even in faulty code.

Perl has everything in place to allow people to use encapsulation, modularity,
polymorphism and inheritance, but the mechanism is open and visible and that
helps people understand what they are dealing with in similar circumstances in
other languages.

 3) Nope. Never did any Ruby.

And there is the problem with improper quoting. Please include just as much of
the post that you are responding to so that your own post makes sense in 
isolation.

Rob

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Reg. Directory listing program

2008-06-06 Thread Gunwant Singh
Hi,

Let me thank you for your help. I think you guys are doing a great work, its
really appreciative.
Thanks to David and John. Now that I changed my *perl* code to the
following, its actually working

*use strict;
use warnings;
use File::stat;

opendir (DH, subcode) or die $!;
chdir(C:/Documents and Settings/Myself/Desktop/code/subcode);

foreach my $file(readdir DH)
{
my $value=stat($file);
my $perm=$value-mode  0;
printf \n$file\t%04o\n, $perm;
}

closedir (DH);*


Leading to a question, why did we 'AND' *mode* to *0*. What does each
bit stand for and why there  are 5 bits as compared to UNIX's
User-Group-Others bits. So actually 3 questions :)

Thank you.
-- 
Gunwant Singh


History:
---

Hi,

I am new to this mailing list and I am very new to PERL. So, please bear
with me for my questions.
I wrote a code that lists files in a directory with the permissions. I am
coding for MS-Windows.
Here is my code:

1 use strict;
2 use warnings;
3 use File::stat();
4
5 opendir (DH, subcode) or die $!;
6 foreach my $file(readdir DH)
7 {
8 my $perm=stat($file)-mode;
9 print $file\t$perm\n;
10 }
11 closedir (DH);

I am getting the listing of file just fine if I dont add line 8. But as soon
as I try to display the permissions, I get this:

Can't call method mode without a package or object reference at
C:\Documents a
nd Settings\Myself\Desktop\code\dir-op.pl line 8.

What am I doing wrong!

Thanks.

-- 
Gunwant Singh
---

 -Original Message-
 From: Gunwant Singh [mailto:[EMAIL PROTECTED]
 Sent: Wednesday, June 04, 2008 10:02
 To: beginners@perl.org
 Subject: Reg. Directory listing program

 Hi,

 I am new to this mailing list and I am very new to PERL. So,
 please bear
 with me for my questions.
 I wrote a code that lists files in a directory with the
 permissions. I am
 coding for MS-Windows.
 Here is my code:

 *1 use strict;
 2 use warnings;
 3 use File::stat();
 4
 5 opendir (DH, subcode) or die $!;
 6 foreach my $file(readdir DH)
 7 {
 8 my $perm=stat($file)-mode;

   you need something like:
   my $filestat = stat($file);
   my $perm=$filestat-mode;
 but this will not work correctly unless you are already positioned on
the right directory location. So either you can do a chdir to the folder
you are doing or a concatenate of subcode . $file to get the file
status correctly.

Wags ;)

 9 print $file\t$perm\n;
 10 }
 11 closedir (DH);

 *I am getting the listing of file just fine if I dont add
 line 8. But as
 soon as I try to display the permissions, I get this:

 *Can't call method mode without a package or object reference at
 C:\Documents a
 nd Settings\Myself\Desktop\code\dir-op.pl line 8.

 *What am I doing wrong!

 Thanks.

 --
 Gunwant Singh
---

 -Original Message-
 From: Wagner, David --- Senior Programmer Analyst --- WGO
 [mailto:[EMAIL PROTECTED]
 Sent: Wednesday, June 04, 2008 11:08
 To: Gunwant Singh; beginners@perl.org
 Subject: RE: Reg. Directory listing program

  -Original Message-
  From: Gunwant Singh [mailto:[EMAIL PROTECTED]
  Sent: Wednesday, June 04, 2008 10:02
  To: beginners@perl.org
  Subject: Reg. Directory listing program
 
  Hi,
 
  I am new to this mailing list and I am very new to PERL. So,
  please bear
  with me for my questions.
  I wrote a code that lists files in a directory with the
  permissions. I am
  coding for MS-Windows.
  Here is my code:
 
  *1 use strict;
  2 use warnings;
  3 use File::stat();
  4
  5 opendir (DH, subcode) or die $!;
  6 foreach my $file(readdir DH)
  7 {
  8 my $perm=stat($file)-mode;

   you need something like:
 my $filestat = stat($file);
 my $perm=$filestat-mode;
   I was looking at Programming Perl and if you want to make sense
of the mode, then should 'and' with 0 and print out using %04o (
octal output ), so something like:
   my $perm=$filestat-mode  0;

  but this will not work correctly unless you are already positioned on
 the right directory location. So either you can do a chdir to
 the folder
 you are doing or a concatenate of subcode . $file to get the file
 status correctly.

 Wags ;)

  9 print $file\t$perm\n;

   Then change print to
   printf $file %04o\n, $perm;

Wags ;)

  10 }
  11 closedir (DH);
 
  *I am getting the listing of file just fine if I dont add
  line 8. But as
  soon as I try to display the permissions, I get this:
 
  *Can't call method mode without a package or object reference at
  C:\Documents a
  nd Settings\Myself\Desktop\code\dir-op.pl line 8.
 
  *What am I doing wrong!
 
  Thanks.
 
  --
  Gunwant Singh
 

 **
 This message contains information that is confidential and
 proprietary to FedEx Freight or its affiliates.  It is
 intended only for the recipient named and for the 

Re: Can not get reliable output [WAS Reg. Directory listing program]

2008-06-06 Thread John W. Krahn

Michelle Konzack wrote:

Hello,


Hello,


I have a similar problem but with mode which output weird permissions:

[ '~/bin/.perl_test_001' ]--
#!/usr/bin/perl

use strict;
use warnings;
use File::stat;

opendir(DH, /home/michelle.konzack/bin) || die $!;

my @dir=readdir DH;

foreach (@dir)
{
  my $FILE=$_;
  my $FILESTAT=stat(/home/michelle.konzack/bin/ . $_);
  my $MODE=$FILESTAT-mode;
  printf %-30s%s\n, $FILE, $MODE;
}


and the output is something like:

[ command '.perl_test_001' ]
snip
.perl_test_00133279


[snip]


where perl_test_001 has mode 0777.

Whats wrong here?


$ perl -le'printf %o\n, 33279'
100777


33279 is the decimal representation of a number, and 0777 is the octal 
representation of a number.  If a number has a leading zero it is 
usually displayed in octal representation.


$ perl -le'print for 0777, 777'
511
777

So 0777 is the octal representation of the decimal number 511.


Perl can display numbers in either decimal, hexadecimal, octal or binary 
formats:


$ perl -le'printf %1\$#d%1\$#x%1\$#o%1\$#b\n, 777'
7770x309014110b111001

Perl can also use literal numbers represented in either decimal, 
hexadecimal, octal or binary formats:


$ perl -le'printf %s%s%s%s\n, 777, 0x309, 01411, 0b111001'
777777777777



What I do not understand is:

[ perldoc 'File::stat' ]
 BUGS
As of Perl 5.8.0 after using this module you cannot use the
implicit $_ or the special filehandle _ with stat() or
lstat(), trying to do so leads into strange errors.  The
workaround is for $_ to be explicit

my $stat_obj = stat $_;


That is not related to your problem.


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Reg. Directory listing program

2008-06-06 Thread John W. Krahn

Gunwant Singh wrote:

Hi,


Hello,


Let me thank you for your help. I think you guys are doing a great work, its
really appreciative.
Thanks to David and John. Now that I changed my *perl* code to the
following, its actually working

*use strict;
use warnings;
use File::stat;

opendir (DH, subcode) or die $!;
chdir(C:/Documents and Settings/Myself/Desktop/code/subcode);

foreach my $file(readdir DH)
{
my $value=stat($file);
my $perm=$value-mode  0;
printf \n$file\t%04o\n, $perm;
}

closedir (DH);*


Leading to a question, why did we 'AND' *mode* to *0*. What does each
bit stand for and why there  are 5 bits as compared to UNIX's
User-Group-Others bits. So actually 3 questions :)


man 2 stat
[snip]
   The following flags are defined for the st_mode field:

  S_IFMT 017   bitmask for the file type bitfields
  S_IFSOCK   014   socket
  S_IFLNK012   symbolic link
  S_IFREG010   regular file
  S_IFBLK006   block device
  S_IFDIR004   directory
  S_IFCHR002   character device
  S_IFIFO001   FIFO
  S_ISUID0004000   set UID bit
  S_ISGID0002000   set-group-ID bit (see below)
  S_ISVTX0001000   sticky bit (see below)
  S_IRWXU00700 mask for file owner permissions
  S_IRUSR00400 owner has read permission
  S_IWUSR00200 owner has write permission
  S_IXUSR00100 owner has execute permission
  S_IRWXG00070 mask for group permissions
  S_IRGRP00040 group has read permission
  S_IWGRP00020 group has write permission
  S_IXGRP00010 group has execute permission
  S_IRWXO7 mask for permissions for others (not 
in group)

  S_IROTH4 others have read permission
  S_IWOTH2 others have write permission
  S_IXOTH1 others have execute permission


So mode  0 displays the permission bits as well as SUID, SGID and 
the sticky bit.




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.-- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: TCP apllications

2008-06-06 Thread asmith9983

Hi Arun
You didn't say what you really wanted to achieve. Do yo want to dabble and 
learn from that experience what using network modules is all about ?


--
Andrew
Edinburgh,Scotland


On Fri, 6 Jun 2008, Arun wrote:


Hi,
This is Arun here, i am new to Perl so i
was just thinking of programming the TCP apllications or working. So
how do i do that, just needed guidance from you.





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: pie multiline replace

2008-06-06 Thread Gunnar Hjalmarsson

Richard Lee wrote:

Gunnar Hjalmarsson wrote:


perldoc -q insert a line


Within that basic form, add the parts that you need to insert, change,
or delete lines.

To prepend lines to the beginning, print those lines before you enter
the loop that prints the existing lines.

   open my $in,  '',  $file  or die Can't read old file: $!;
   open my $out, '', $file.new or die Can't write new file: $!;

   print # Add this line to the top\n; # --- HERE'S THE MAGIC

   while( $in )
   {
   print $out $_;
   }

  close $out;

I was just going over this... but shouldn't it be

print $out # Add this line to the top\n; # ??? or did I miss 
something?


I think you are right. I'd suggest that you send a note to Brian d'Foy 
([EMAIL PROTECTED]), who is maintaining the FAQ.


--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: pie multiline replace

2008-06-06 Thread Richard Lee

Gunnar Hjalmarsson wrote:


  close $out;

I was just going over this... but shouldn't it be

print $out # Add this line to the top\n; # ??? or did I miss 
something?


I think you are right. I'd suggest that you send a note to Brian d'Foy 
([EMAIL PROTECTED]), who is maintaining the FAQ.



done, and thanks for confirming.
I was just afraid that I have missed something while reading that faq 
lines..


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Looping through an anonymous array of arrays

2008-06-06 Thread Rodrick Brown
my $arrayRef = [ 1, 2, 3, ['a', 'b', 'c', [Hello] ]];

I have no problem returning single elements but how would one walk this list
of elements with say a for loop?


Re: Looping through an anonymous array of arrays

2008-06-06 Thread Jeff Peng
On Sat, Jun 7, 2008 at 9:31 AM, Rodrick Brown [EMAIL PROTECTED] wrote:
 my $arrayRef = [ 1, 2, 3, ['a', 'b', 'c', [Hello] ]];

 I have no problem returning single elements but how would one walk this list
 of elements with say a for loop?


Try this code:

use strict;

my $arrayRef = [ 1, 2, 3, ['a', 'b', 'c', [Hello] ]];

lookinto($arrayRef);

sub lookinto {
my $obj = shift;
if (ref $obj) {
for (@$obj) {
lookinto($_);
}
} else {
print $_\n;
}
}

-- 
Jeff Peng - [EMAIL PROTECTED]
Professional Squid supports in China
http://www.ChinaSquid.com/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Looping through an anonymous array of arrays

2008-06-06 Thread Chas. Owens
On Fri, Jun 6, 2008 at 9:31 PM, Rodrick Brown [EMAIL PROTECTED] wrote:
 my $arrayRef = [ 1, 2, 3, ['a', 'b', 'c', [Hello] ]];

 I have no problem returning single elements but how would one walk this list
 of elements with say a for loop?


You can treat an array reference like an array by prefixing it with an
@ (this is called dereferencing):

for my $item (@$arrayRef) {
if (my $type = ref $item) {
print item is a $type reference\n;
} else {
print item is $item\n;
}
}

If you just want to print out the items held in the data structure you
can use a recursive function:

sub print_it {
for my $item (@_) {
if (ref $item) {
print_it(@$item);
next;
}
print $item ;
}
}

print_it($arrayRef);

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: Looping through an anonymous array of arrays

2008-06-06 Thread Gunnar Hjalmarsson

Rodrick Brown wrote:

my $arrayRef = [ 1, 2, 3, ['a', 'b', 'c', [Hello] ]];

I have no problem returning single elements but how would one walk this list
of elements with say a for loop?


One way:

my $level = 0;
breakdown( $arrayRef );

sub breakdown {
my $ref = shift;
foreach my $elem ( @$ref ) {
if ( ref($elem) eq 'ARRAY' ) {
$level++;
breakdown( $elem );
$level--;
} else {
print \t x $level, $elem\n;
}
}
}

But as usual when we are dealing with nested data structures, 
Data::Dumper comes in handy:


use Data::Dumper;
print Dumper $arrayRef;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/