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-07-2009, 07:28 PM   #1
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Rep: Reputation: 15
Perl - foreach loop reading only the last $scalar in a @list


Hi all..... as a means of learning Perl, I am writing a script to search for files of various extensions. Here is a little snippet to show you my issue. The output shows that only files ending in "txt" are being found.... am I over writing @files every time thus only the last $ext survives? If so, how should the script read to put all file paths of all types in @type into @files.

lol... sorry if this makes no scene.... been a VERY long day.. time for bed!

Quote:
#!/usr/bin/perl -w

use File::List;

my @type = ("html", "mp3", "txt");

foreach $ext (@type) {
$search = new File::List("/home/mine");
@files = @{ $search->find("\.$ext\$")};
}

print "\n\n";
foreach $file (@files) {
print "$file\n";
}

print "\n\n";
foreach $ext (@type) {
print "$ext\n";
}
Output is:
Quote:
/home/mcy/.mozilla/firefox/wkidzzc0.default/cert_override.txt
/home/mcy/.mozilla/firefox/wkidzzc0.default/signons3.txt
/home/mcy/.mozilla/firefox/wkidzzc0.default/urlclassifierkey3.txt
/home/mcy/readme.txt


html
mp3
txt
 
Old 08-08-2009, 12:13 AM   #2
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
Try this? It appears like you're overwriting your previous search results with the last one.

Code:
#!/usr/bin/perl -w

use File::List;

my @type = ("html", "mp3", "txt");

foreach $ext (@type) {
$search = new File::List("/home/mine");
@files = @{ $search->find("\.$ext\$")};

print "$ext\n";

print "\n\n";
foreach $file (@files) {
print "$file\n";
}

}
 
Old 08-08-2009, 09:23 AM   #3
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by gregorian View Post
Try this? It appears like you're overwriting your previous search results with the last one.

Code:
#!/usr/bin/perl -w

use File::List;

my @type = ("html", "mp3", "txt");

foreach $ext (@type) {
$search = new File::List("/home/mine");
@files = @{ $search->find("\.$ext\$")};

print "$ext\n";

print "\n\n";
foreach $file (@files) {
print "$file\n";
}

}
Thanks gregorian,

Things are looking much clearer this morning!

The idea would be to have everything in one list, but I can append it to something like @total on each pass (minus the info from @type)...... thanks for the help!
 
Old 08-08-2009, 09:57 AM   #4
OldGaf
Member
 
Registered: Feb 2008
Posts: 47

Original Poster
Rep: Reputation: 15
This works fine...... thanks again!
Quote:

#!/usr/bin/perl -w

use File::List;

my @type = ("html", "mp3", "txt");

my @total=();

foreach $ext (@type) {
$search = new File::List("/home/mine");
@files = @{ $search->find("\.$ext\$")};

push(@total, @files);

}
 
  


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
PERL - foreach array not clearing michael.barnes Programming 8 08-21-2007 09:37 PM
Perl DBD::mysql - Clean a scalar, keep running after error. exodist Programming 1 02-07-2007 06:52 AM
[Perl] array in scalar noir911 Programming 2 12-10-2006 04:52 PM
Perl: Where am I in a foreach loop? jrtayloriv Programming 3 01-30-2005 10:43 PM
perl:foreach katana Programming 3 07-24-2001 01:05 AM

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

All times are GMT -5. The time now is 03:30 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