develooper Front page | perl.beginners | Postings from May 2007

Store contents of array in a variable?

Thread Next
From:
Romeo Theriault
Date:
May 17, 2007 13:39
Subject:
Store contents of array in a variable?
Message ID:
1179434362.26665.1190416031@webmail.messagingengine.com
This is probably a dumb question but is it possible to store the
contents of an array into a variable?

Something like this:

$variable = foreach $array_ref7 (@rowstash7) {
       return "@$array_ref7\n";
    }

I have this script below in which I am trying to send the contents of an
array within an email and don't quite know how to deal with getting the
contents of the array's into the email message. Suggestions are very
welcome.

Thanks,

Romeo


#!/usr/bin/perl

use warnings;
use strict;
use DBI;
use Net::SMTP;


my @rowstash7;
my @rowstash30;
my @rowstash365;
my $array_ref7;
my $array_ref30;
my $array_ref365;
my $somevariable = "test";


## Connect to the database.
my $dbh = DBI->connect( "DBI:mysql:database=antivirus;host=localhost",
    "user", "password", { 'RaiseError' => 1 } );

&success_querie(7);
&success_querie(30);
&success_querie(365);

&sendmail('antivirus_downloads@umfk.maine.edu',
'romeo.theriault@maine.edu', "Antivirus Downloads", 'mail.maine.edu',
'romeo.theriault@maine.edu', "Download Activity for the last
Week:\n$somevariable\n\nDownload Activity for the last
Month:\n$somevariable\n\nDownload Activity for the last
Year:\n$somevariable");



## Function to query the database for the info.
sub success_querie {
    my ($searchlength) = @_;
    my $select_handle =
      $dbh->prepare_cached(
'SELECT name,COUNT(*) FROM success WHERE datetime > SUBDATE(NOW(),
INTERVAL ? DAY) GROUP BY name;'
      );

    die "Couldn't prepare queries; aborting"
      unless defined $select_handle;

    $select_handle->execute($searchlength) or return 0;
    my ( $name, $count );
    $select_handle->bind_columns( \$name, \$count );

    if ( $searchlength == 7 ) {
        while ( $array_ref7 = $select_handle->fetchrow_arrayref() ) {
            push @rowstash7, [@$array_ref7];

            #print "$name, $count\n";
        }
    }
    elsif ( $searchlength == 30 ) {
        while ( $array_ref30 = $select_handle->fetchrow_arrayref() ) {
            push @rowstash30, [@$array_ref30];

            #print "$name, $count\n";
        }
    }
    else {
        while ( $array_ref365 = $select_handle->fetchrow_arrayref() ) {
            push @rowstash365, [@$array_ref365];

            #print "$name, $count\n";
        }
    }

    $select_handle->finish();

    return 1;    # Success

    ## Disconnect from the database.
    $dbh->disconnect();

}

## Function to send the mail.
sub sendmail {
    my ( $from, $to, $subject, $server, $replyto, $message ) = @_;
    my $smtp = Net::SMTP->new($server);
    $smtp->mail($from);
    $smtp->to($to);

    #$smtp->bcc($bcc, $bcc1);
    $smtp->data();
    $smtp->datasend("To: $to\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("Reply-To: $replyto\n");
    $smtp->datasend("$message");
    $smtp->dataend();

    $smtp->quit;
}

#sub messagebody {
#       Print "Download Activity for the last Week:\n";
#       foreach $array_ref7 (@rowstash7) {
#          return "@$array_ref7\n";
#       }
#       print "Download Activity for the last Month:\n";
#       foreach $array_ref30 (@rowstash30) {
#          return "@$array_ref30\n";
#       }
#       print "Download Activity for the last Year:\n";
#       foreach $array_ref365 (@rowstash365) {
#               return "@$array_ref365\n";
#       }
#}


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About