hello, i'm having trouble printing arrays to file.
each time i run this code, i get only two results max.
if the array has 2 genres, i get them but no languages.
if the array has 1 genre i get it and only 1 language.
if the array has more than 2 genres, i get only 2 and no languages.

help will be greatly appreciated.

sub WriteLineToFile{
    my $MTitle = shift;
    my $MYear = shift;
    my $MDirectors = shift;
    my $MRatings = shift;
    my $MVoters = shift;
    my $MLength = shift;
    my $MAward = shift;
    my @MGenres = shift;
    my @MLanguages = shift;

    my $commas = 0;
    my $arrLen = 0;
    my $Genre;
    open(FH,">>","movieOutput.csv") or die "cannot... nohave\n";#append rows to file

        print FH $MTitle,",", $MYear,",",$MDirectors,",",$MRatings,",",$MVoters,",",$MLength,
                    ",",$MAward,",";


        for $element (@MGenres){
            print FH $element.",";
        }

        for $element (@MLanguages){
            print FH $element.",";
        }
        print FH "\n";
        close FH;
}#end sub

Recommended Answers

All 2 Replies

#!/usr/bin/perl
#PrintArraysBad.pl
use 5.006;
use strict;
use warnings;

my @genres = qw(Action Adventure Animation Biography Bollywood Comedy Crime Documentary Drama);
my @languages = qw(English French Spanish Greek German Dutch Esperanto);

print_line(@genres, @languages);

sub print_line {
    # @_ now contains the following list:
    print "You passed the following list of scalar values to the subroutine:\n @_ \n";
    
    my @MGenres    = shift; #Assign the first scalar item from @_ to @MGenres
    my @MLanguages = shift; #Assign the next scalar item from @_ to @MLanguages

    print "\n\n The genres array now contains: \n";
    for my $element (@MGenres) { 
        print $element . ",";
    }

    print "\n\n The languages array now contains: \n";
    for my $element (@MLanguages) {
        print $element . ",";
    }
    print "\n";
    
}    #end sub

The Perl model for function call and return values is simple: all functions are passed as parameters one single flat list of scalars, and all functions likewise return to their caller one single flat list of scalars. Any arrays or hashes in these call and return lists will collapse, losing their identities--but you may always use pass-by-reference instead to avoid this.

from http://perldoc.perl.org/perlsub.html
For example:

#!/usr/bin/perl
#PrintArraysBetter.pl
use 5.006;
use strict;
use warnings;

my @genres = qw(Action Adventure Animation Biography Bollywood Comedy Crime Documentary Drama);
my @languages = qw(English French Spanish Greek German Dutch Esperanto);

print_line(\@genres, \@languages); # The '\' prefix converts an array into an array reference.

sub print_line {
    # @_ now contains the following list:
    print "You passed the following list of scalar values to the subroutine:\n @_ \n";
    my ($array_ref1, $array_ref2) = @_; # Assign the two array references to scalar variables
    my @MGenres    = @{$array_ref1}; #Dereference 1st array ref 
    my @MLanguages = @{$array_ref2}; #Dereference 2nd array ref

    print "\n\n The genres array now contains: \n";
    for my $element (@MGenres) { 
        print $element . ",";
    }

    print "\n\n The languages array now contains: \n";
    for my $element (@MLanguages) {
        print $element . ",";
    }
    print "\n";
    
}    #end sub

For information about creating and using references see http://perldoc.perl.org/perlreftut.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.