Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Foreach Complications

by kilinrax (Deacon)
on Dec 07, 2000 at 19:15 UTC ( #45508=note: print w/replies, xml ) Need Help??


in reply to Foreach Complications

If the right information is being sent to PRINT_INFO, then the problem probably resides within that subroutine, which you haven't included in your post (hint, hint).

I would also humbly suggest cleaning up the html your script produces (which may be the problem if you're looking at it's output through a browser), and also that you probably want to be using 'for($i=0; $i<=$#netids; $i++) { ... }' rather than 'foreach $id(@netids){ .. }' as you are iterating over three lists rather than just one:
my $i; print "<table>\n"; for ($i=0; $i<=$#netids; $i++){ print " <tr>\n <td>\n <pre>\n"; PRINT_INFO ($netids[$i],$nums[$i],$sects[$i]); print " </tr>\n </td>\n </pre>\n"; } print "</table>\n";

Replies are listed 'Best First'.
Re: Re: Foreach Complications
by ImpalaSS (Monk) on Dec 07, 2000 at 19:35 UTC
    The subroutine is rather lengthy, so let me just post the relevant parts:
    sub PRINT_INFO { my ($netid, $sitename, $sector) = @_; $DAILY_HEAD= "Date Total"; $,="\t"; #Output Field Separator $query_string="netid=$netid-$sitename-$sector&report_type=Daily+Totals +&market=PHL"; $query_string =~ s/[=&]/ /g; @query_string=split(' ', $query_string); $network_element=$query_string[1]; $report_type=$query_string[3]; $MARKET=$query_string[5]; @network_element=split(/-/, $network_element); $id=$network_element[0]; $name=$network_element[1]; $sector=$network_element[2]; ###################################################### $DAILY_PATH="/data1/$MARKET/reports"; $BUSY_PATH="/data1/$MARKET/bhreports"; $PKD_PATH="/data1/$MARKET/pkdreports"; $MISC_PATH="/data1/$MARKET/miscreports"; $MDG_PATH="/data1/$MARKET/mdgreports"; $cellname="$network_element[1] Sector $network_element[2]"; $search_string="\Q($id)"; print "$cellname\n"; @all_readable_files=grep !/\.Z/, `ls -t $DAILY_PATH | head -95`; #@all_readable_files=grep !/\.Z/, `ls -t $DAILY_PATH`; print "$DAILY_HEAD\n"; foreach $file(@all_readable_files) { chomp($file); @date = split /_/, $file; $date = $date[0]; open(FILE, "$DAILY_PATH/$file"); @DATA=<FILE>; @result = grep /$search_string/, @DATA; $hits = grep /$search_string/, @DATA; for ( $i = 0; $i < $hits; $i++ ) { @_=split(' ', $result[$i]); chomp @_; print $date[0],$_[7]; print "\n"; } close FILE;
    Now, the only problem is that $cellname isnt printing correctly. This is what i should get as the titles:
    0040 Sector 1 0040 Sector 2 5228 Sector 1 5228 Sector 2 5458 Sector 1 0473 Sector 3 5335 Sector 1 5335 Sector 2 0267 Sector 3 0314 Sector 1 0314 Sector 3 5268 Sector 1 5268 Sector 2

    This is what i do get
    0040 Sector 1 5228 Sector 1 5228 Sector 1 5228 Sector 1 5228 Sector 1 5228 Sector 1 5228 Sector 1 5228 Sector 1 5228 Sector 1 5228 Sector 1 0040 Sector 2 0040 Sector 2 0040 Sector 2
    I know the netid's that the sunroutine gets are being processed correctly, its the titles that are causing the problems. 0040 Sector 1 0040 Sector 2 5228 Sector 1 5228 Sector 2 5458 Sector 1 0473 Sector 3 5335 Sector 1 5335 Sector 2 0267 Sector 3 0314 Sector 1 0314 Sector 3 5268 Sector 1 5268 Sector 2

    Kilinrax - I also did try a for loop, but it took forever to run. this one takes about 2 mins, the other.. i dont even it remember finishing.

    arturo - to answer your question: the $index was used for the forloop, i just forgot to delete it, thanks much.

    I hope this helps.

    Dipul

      This is a followup to tilly's post, above. One place to look in this subroutine -- which is using globals -- is at your variables. The output suggests that the value set in a previous call to the sub is being re-used in subsequent calls. If you use my to declare those variables in the subroutine, this can't happen, and moreover, if anything goes wrong and you inadvertently assign undef to one of them, you'll get a warning (provided you turn on -w or use warnings. This will help you track down errors.

      Installment #157145 in the 'we were really serious when we said "use strict and warnings" series' =)

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        arturo is correct. Look particularly at your use of (the same) $i and $id in the calling code and the subroutine.

        my is your friend.

      Could could make this code a lot cleaner. Notably, that mess of splits and data assignments at the top, using `ls` when you should be using readdir, and scoping of variables.

      Here's what i came up with after five minutes:
      sub PRINT_INFO { my ($netid, $sitename, $sector) = @_; my $DAILY_HEAD = "Date Total"; $,="\t"; #Output Field Separator my $report_type = 'Daily Totals'; my $MARKET = 'PHL'; my $DAILY_PATH = "/data1/$MARKET/reports"; my $BUSY_PATH = "/data1/$MARKET/bhreports"; my $PKD_PATH = "/data1/$MARKET/pkdreports"; my $MISC_PATH = "/data1/$MARKET/miscreports"; my $MDG_PATH = "/data1/$MARKET/mdgreports"; my $cellname = "$sitename Sector $sector"; my $search_string = "\Q$netid"; print "$cellname\n"; opendir DIR, $DAILYPATH; my @all_readable_files = grep { ($_ ne '.') && ($_ ne '..') && ($_ + !~ /\.Z//) } readdir DIR; closedir DIR; print "$DAILY_HEAD\n"; my $file; foreach $file (@all_readable_files) { my $date = substr ($file, 0 , (index $file, '_')); open(FILE, "$DAILY_PATH/$file"); @DATA=<FILE>; @result = grep { (index $_, $search_string) > -1 } @DATA; for ( $i = 0; $i <= $#results; $i++ ) { my $info = (split(' ', $result[$i]))[7]; print $date , $info . "\n"; } close FILE; } }
        Good stuff - but don't code Perl like C :
        for my $i (0..$#results) { my $info = . . . . . . . }
        It took me a while to get used to it - it's almost too easy, but think of the carpel-tunnel you'll prevent ;)

        Jeff

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        F--F--F--F--F--F--F--F--
        (the triplet paradiddle)
        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://45508]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2022-05-25 17:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (90 votes). Check out past polls.

    Notices?