Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

export variable outside foreach

by mitchreward (Acolyte)
on Jun 19, 2014 at 08:21 UTC ( #1090417=perlquestion: print w/ replies, xml ) Need Help??
mitchreward has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

Am pretty new to perl and wanted to export the last value of $last. But it doesn't work. Could you help?

ps: I just wanted to make the equivalent of a Unix "tail -1"

use CGI qw(:standard); use strict; use utf8; my $srce = "/some/file"; my $string1 = "some-regex"; my $last; open my $fh, $srce or die "Could not open $scre: $!"; my @lines = grep /\Q$string1/, <$fh>; foreach my $line (@lines) { my @fields = split /\s+/, $line; $last= $fields[3]; } print "$last";
thanks

Comment on export variable outside foreach
Download Code
Replies are listed 'Best First'.
Re: export variable outside foreach
by Eily (Curate) on Jun 19, 2014 at 08:38 UTC

    What do you mean by "it doesn't work" ? Does it print something wrong, or nothing at all? See How do I post a question effectively?

    Since you have replaced your values and have not given any sample data, I can't tell if this is on purpose but in /\Q$string1/ $string1 is interpreted as a literal string, not a regular expression. This means for example that "a*" will match "a*b+c" and not "aaaaaaa". To have your string interpreted as a regex, do not use \Q

Re: export variable outside foreach
by Anonymous Monk on Jun 19, 2014 at 08:29 UTC
    sure it does ... if $last is empty then @fields was empty or @lines is empty

      perl -w test.pl

      Global symbol "$scre" requires explicit package name at test.pl line 12.

      Execution of test.pl aborted due to compilation errors.
        Is that a question? :D Fix your typo, copy the varible name, ctrl+f, paste variable name, enter, find the typo, fix it

        mitchreward:

        He meant that $srce is not the same as $scre.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

      @lines being empty seems unlikely, since it was grepped out of the file. @fields may not contain four fields though (Edit: whoops, @lines, can be empty, is just can't contain empty strings unless $string1 is empty)

      mitchreward, have a look at Data::Dumper. If you use Data::Dumper; and then add print Dumper \@fields; to your loop you may have more information on what is happening.

      Edit: the code you provided does compile, so you have removed the error by changing it.

        @lines being empty seems unlikely, since it was grepped out of the file.

        I don't understand this point. Why would grep-ing the contents of the file make it likely that the  @lines array must contain something? (Or does this refer to a previous version of the posted code that was changed without citation?)

Re: export variable outside foreach
by philipbailey (Chaplain) on Jun 19, 2014 at 15:36 UTC

    Perhaps your input file has a final line consisting of a newline only, or line that has no fourth field? That would give the behaviour you describe.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2015-08-01 01:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls