CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
printing split array without files

 



regex2012
User

Jan 31, 2017, 7:29 AM

Post #1 of 5 (1743 views)
printing split array without files Can't Post

I am trying to create this same portion of a script using only arrays. below is the current portion:



Code
open my $fh, '<', "/prj/accounting/reports/list.txt" or die "unable to open file '$file' for reading : $!"; 
open my $fh1, '>', "/prj/accounting/reports/activity.txt" or die "unable to open file '$file' for reading : $!";
while (<$fh>) {
my @fields = split(',', $_);
local $" = ',';
if ($fields[2] eq '1' || $fields[2] eq '0') {
print $fh1 "@fields[4]\n" if /MONTHLY/ && !/,-,/;
}
}
close $fh;
close $fh2;


It works fine. I tried to do this but it is not working as it will not print anything like the portion above will:


Code
while (@list) { 
my @fields = split(',', $_);
local $" = ',';
if ($fields[2] eq '1' || $fields[2] eq '0') {
print "@fields[4]\n" if /MONTHLY/ && !/,-,/;
}
}

So there is already an array, @list, which is equivalent to
/prj/accounting/reports/list.txt.
From there I am trying to split it and then print. I'm confused on how I might do this, any suggestions? I usually work with input/output files but am trying to learn to use arrays so there aren't so many files hanging around.


Laurent_R
Veteran / Moderator

Jan 31, 2017, 10:15 AM

Post #2 of 5 (1738 views)
Re: [regex2012] printing split array without files [In reply to] Can't Post

The short answer is this. Change:

Code
while (@list) {

to:

Code
for (@list) {

Or you could do this:

Code
while (my $item = shift @list) {  
# play with $item
}

but this is probably not as good, because it will empty the @list array, which you may need further down in your code.


BillKSmith
Veteran

Jan 31, 2017, 1:35 PM

Post #3 of 5 (1731 views)
Re: [regex2012] printing split array without files [In reply to] Can't Post

Laurent has already pointed out your main problem. However, you do have another. If you used strict and warnings, you would have received a warning about your use of @list[4]. Perl suggest using $list[4]. Your use of $" suggests that this is not what you mean. Perhaps you mean @list[0..4] or even @list. I cannot tell without a sample input and expected output.

You should define special variables in the smallest possible scope. In this case, that would be inside the if-block. (It is possible to restrict the scope even move with a do-block, but this is probably overkill)

If you check the documentation for the function 'split', you will find that the first argument is regex, not a string. In this case, it did not matter. Get in the habit of doing it right to avoid a subtle bug in the future.


Code
use strict; 
use warnings;
my @list = ("MONTHLY,xxx,1,xxx,foo,fum", "YEARLY,xxx,0,xxx,foo,fum");
foreach (@list) {
my @fields = split /,/;
if ( $fields[2] eq '1' || $fields[2] eq '0' ) {
print do {local $" = ','; "@fields[0..4]\n"} if /MONTHLY/ && !/,-,/;
}
}


OUTPUT:
MONTHLY,xxx,1,xxx,foo

Good Luck,
Bill


FishMonger
Veteran / Moderator

Jan 31, 2017, 2:12 PM

Post #4 of 5 (1727 views)
Re: [regex2012] printing split array without files [In reply to] Can't Post


Quote

Code
if ($fields[2] eq '1' || $fields[2] eq '0') {



Quote
That would be better written as:

Code
if ($fields[2] == 1 || $fields[2] == 0) {



regex2012
User

Feb 1, 2017, 8:54 AM

Post #5 of 5 (1712 views)
Re: [FishMonger] printing split array without files [In reply to] Can't Post

Thanks, all of you for suggestions, now it works great!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives