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

Printing a Variable outside of the block

by trenchwar (Beadle)
on Apr 15, 2008 at 16:12 UTC ( #680553=perlquestion: print w/replies, xml ) Need Help??

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

I understand why it doesn't print outside the block, just not clear on how to make $part1 or $part2 print outside the block.
Sorry for my newbie questions....
#!/usr/bin/perl use warnings; use strict; my @l; open (FH3, ">lessons.txt"); print FH3 "Perl*Lesson1\n"; print FH3 "Perl*Lesson2\n"; print FH3 "Perl*Lesson3\n"; print FH3 "Java*Lesson1\n"; print FH3 "Java*Lesson2\n"; print FH3 "Java*Lesson3\n"; print FH3 "PHP*Lesson1\n"; print FH3 "PHP*Lesson2\n"; print FH3 "PHP*Lesson3\n"; close (FH3); my $l="lessons.txt"; open FH3, "<", $l or die "$l: $!\n"; @l=<FH3>; for my $line (@l){ chomp $line; my ($part1, $part2) = split /\*/,$line; print "$part1 $part2\n"; }

Replies are listed 'Best First'.
Re: Printing a Variable outside of the block
by oko1 (Deacon) on Apr 15, 2008 at 16:56 UTC

    Since others have so competently answered your original question, I'll just pick on the rest. :)

    ### *Always* check the return when you open a file! # open (FH3, ">lessons.txt"); open FH3, ">lessons.txt" or die "lessons.txt: $!\n"; print FH3 "Perl*Lesson1\n"; print FH3 "Perl*Lesson2\n"; print FH3 "Perl*Lesson3\n"; print FH3 "Java*Lesson1\n"; print FH3 "Java*Lesson2\n"; print FH3 "Java*Lesson3\n"; print FH3 "PHP*Lesson1\n"; print FH3 "PHP*Lesson2\n"; print FH3 "PHP*Lesson3\n"; ### Lots of wasted typing, there. Doing it as a single line was OK - o +r ### you could use a 'heredoc'. print FH3 <<'Text_Block'; Perl*Lesson1 Perl*Lesson2 [etc...] Text_Block ### Or you could even generate it - since it's all repetitive. for my $lng (qw/Perl Java PHP/){ print "$lng*Lesson$_\n" for 1..3 };
    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
Re: Printing a Variable outside of the block
by moritz (Cardinal) on Apr 15, 2008 at 16:17 UTC
    What do you want the output to be?
      Thanks for the quick response moritz. Same as it is inside the block.
      I would like it to print outside the block just as it does inside the block. So that later in this homework script I can ask a user to "Enter L for a list of classes and Lessons" and have L equal to "$part1$part" or whatever.
      Im just not clear on how to define and pull the variable out of the block.
Re: Printing a Variable outside of the block
by olus (Curate) on Apr 15, 2008 at 16:24 UTC

    What block are you referring to? Your code prints correctly all those lines without the '*'. What and where do you want that stuff being printed?

      For example, if I wanted to do it like below.
      #!/usr/bin/perl use warnings; use strict; my @l; open (FH3, ">lessons.txt"); print FH3 "Perl*Lesson1\n"; print FH3 "Perl*Lesson2\n"; print FH3 "Perl*Lesson3\n"; print FH3 "Java*Lesson1\n"; print FH3 "Java*Lesson2\n"; print FH3 "Java*Lesson3\n"; print FH3 "PHP*Lesson1\n"; print FH3 "PHP*Lesson2\n"; print FH3 "PHP*Lesson3\n"; close (FH3); my $l="lessons.txt"; open FH3, "<", $l or die "$l: $!\n"; @l=<FH3>; for my $line (@l){ chomp $line; my ($part1, $part2) = split /\*/,$line; } print "$part1 $part2\n";
        You can do that if you declare $part1 and $part2 outside the block:
        my ($part1, $part2); for my $line (@l){ chomp $line; ($part1, $part2) = split /\*/,$line; } print "$part1 $part2\n";

        But of course that prints only one line (the last), because each iteration of the block overrides the variables.

        If you don't like that, use an array and push the values onto the array.

        Aside from what moritz said, do you really need to have $part1 and $part2? Since you are parsing an array you'll have to go through the process of running all its values. There are quite a few ways to do that. You can

        use the for loop to build the final string to be printed, and print it outside the block

        @l=<F3>; my $text = ''; for my $line (@l){ chomp $line; my ($part1, $part2) = split /\*/,$line; $text .= "$part1 $part2\n"; } print $text;

        create the single string right from the array values by joining them

        @l=<F3>; my $text = join '', @l; $text =~ s/\*/ /g; print $text;

        use a different kind of block

        @l=<F3>; print map {s/\*/ /;$_;} @l;
Re: Printing a Variable outside of the block
by Roy Johnson (Monsignor) on Apr 15, 2008 at 16:35 UTC
    You need to declare your variables in the outermost scope you want to use them in. Since you want to use them outside the block, you should declare them outside (before) the block.

    Caution: Contents may have been coded under pressure.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2021-10-21 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (84 votes). Check out past polls.

    Notices?