Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

passing arrays

by ScOut3R (Sexton)
on Apr 19, 2008 at 17:26 UTC ( #681674=perlquestion: print w/replies, xml ) Need Help??

ScOut3R has asked for the wisdom of the Perl Monks concerning the following question:

Hey There! I'm quite a newbie with Perl and i'm stuck with subroutines and arrays.
#!/usr/bin/perl use strict; use warnings; use Getopt::Std; my $cimzett; my $domain; my $date; my @data; getopts ('m:d:s:'); use vars qw ($opt_m $opt_d $opt_s); $date=$opt_d; $domain=$opt_s; sub csv { open(BEJOVO, ">>bejovo.csv"); open(KIMENO, ">>kimeno.csv"); while (my $in = @data) { my @mails = readlog($in); #CSV fejlec # print BEJOVO ";BEJOVO LEVELEK;\n"; # print BEJOVO "Idopont;Felado;Cimzett\n"; # print KIMENO ";KIMENO LEVELEK;\n"; # print KIMENO "Idopont;Felado;Cimzett\n"; #fajlba iras if ( $mails[9] =~ /$domain/i ) { print KIMENO "$mails[2];$mails[9];$mai +ls[11]\n"; } elsif ( $mails[9] =~ /@/ ) { print BEJOVO "$mails[2];$mails[9];$mai +ls[11]\n"; } if ( $mails[8] =~ /$domain/i ) { print KIMENO "$mails[2];$mails[8];$mai +ls[10]\n"; } elsif ( $mails[8] =~ /@/ ) { print BEJOVO "$mails[2];$mails[8];$mai +ls[10]\n"; } #csv fajlok e-mail mellekletkent [cimzett]-nek #mpack program kell hozza! # system("mpack -s kimeno-`date -I` kimeno.csv $cimzett"); # system("mpack -s bejovo-`date -I` bejovo.csv $cimzett"); # system('rm bejovo.csv'); # system('rm kimeno.csv'); } close(KIMENO); close(BEJOVO); } sub readlog { my $line = $_[0]; chomp($line); my @mails; my $key = "<"; my $key2 = ">"; #Naplofajlok beolvasasa #amavis bejegyzesek kinyerese if ( $line =~ /amavis/ ) { #szokozonkent szetvalasztas es tombbe illesztes @mails = split ('$line'); #csak a kivalasztott napot vegye figyelembe if ( $mails[1] == $date ) { #"><" karakterek levagasa if ( $mails[9] =~ /@/ ) { $mails[9] =~ s/$key//g; $mails[9] =~ s/$key2//g; $mails[11] =~ s/$key//g; $mails[11] =~ s/$key2//g; } else { $mails[8] =~ s/$key//g; $mails[8] =~ s/$key2//g; $mails[10] =~ s/$key//g; $mails[10] =~ s/$key2//g; } } } return @mails; } while (<>) { push @data, $_; } if ( $opt_m eq "csv" ) { csv; }
The script is just half functional but useable. I think the passing of arrays between the subroutines aren't wokring properly. Thank You for your help!

Replies are listed 'Best First'.
Re: passing arrays
by grep (Monsignor) on Apr 19, 2008 at 17:38 UTC
    You aren't passing anything into the sub csv

    my @array = ( 1, 2, 3, 4, 5 ); csv( @array ); # pass array into csv sub csv { my @passed_array = @_; # reteive array from @_ foreach (@array) { print "$_\n"; } }
    Check out perlsub

    Also:
    Please reduce your question to the smallest example of your problem. This not only saves us from having to dig through irrelevant code to find your real problem, but a good portion of the time you'll answer it yourself.

    Update: added Also section

    grep
    One dead unjugged rabbit fish later...
      I know grep! :) I'm passing the the $in variable to readlog() from csv() and returning the @mails array from it.
      while (my $in = @data) { my @mails = readlog($in);
      But i think nothing is coming back.
        Ahhhh... this is why you should reduce your code to the problem. I did not see that buried in the rest of your code.

        you want a foreach

        foreach my $in (@data) { my @mails = readlog($in);
        grep
        One dead unjugged rabbit fish later...
Re: passing arrays
by elmex (Friar) on Apr 19, 2008 at 17:46 UTC

    Somehow this line looks a bit weird, I didn't know there is a one-argument form of split?

    @mails = split ('$line');

    Did you maybe mean: split ("$line") or even split ($line) or even: split (/;/, $line)?

      Actually all three arguments to split [pattern[, expression[, limit]]] are optional. Pattern defaults to whitespace splitting (' '). Expression defaults to $_. Limit defaults to 0 - that is, no limit.


      Perl is environmentally friendly - it saves trees
        Actually all three arguments to split [pattern;[, expression, limit]] are optional.

        I find that statement misleading. See the 'template' at the top of spilt:

        split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/ split

        This implicitly (and rightly) excludes:

        split EXPR

        Which is exactly what the OP tries to do in the line:

        @mails = split ('$line');

        This is why the OP's code generates the following error:

        Use of uninitialized value $_ in split at XX.pl line YY.

        IOW, the 'expression' argument is optional only if the 'pattern' argument is present.

        -1 is no limit. 0 is no limit and discard empty trailing fields.
      My bad. I wanted to write split (' ', $line);.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (2)
As of 2021-09-27 01:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?