in reply to using sub routines and solving an issue i have with passing variables

Sounds like the sort of place I'd use really light weight objects:

use strict; use warnings; my $fileStr = <<FILE; =wibble The wibble section =plonk the plonk section =foo the foo section FILE open my $fin, '<', \$fileStr; my $obj = bless {fin => $fin}; $obj->parseStructure($fin); $obj->parseBlock('foo'); $obj->parseBlock('wibble'); $obj->parseBlock('bar'); sub parseStructure { my ($self) = @_; my $currBlock; while (1) { my $start = tell $fin; my $line = <$fin>; next if defined $line && $line !~ /^=(\w+)/; $self->{blocks}{$currBlock}{length} = $start - $self->{blocks}{$currBlock}{start} if defined $currBlock; $currBlock = $1; $self->{blocks}{$currBlock}{start} = $start if defined $line; last if eof $fin; } } sub parseBlock { my ($self, $blockType) = @_; if (!exists $self->{blocks}{$blockType}) { print "No '$blockType' blocks in file\n"; return; } seek $self->{fin}, $self->{blocks}{$blockType}{start}, 0; read $self->{fin}, my $block, $self->{blocks}{$blockType}{length}; print "$blockType:\n$block\n"; }

Prints:

foo: =foo the foo section wibble: =wibble The wibble section No 'bar' blocks in file
Perl is the programming world's equivalent of English

Replies are listed 'Best First'.
Re^2: using sub routines and solving an issue i have with passing variables
by james28909 (Deacon) on Oct 01, 2014 at 00:02 UTC
    i just figured out something else i didnt know. you can name an array on the fly say for instance if you read a files name and want to make the filename the name of the array... take this into code consideration (which most of you probably already know this) i know this is off topic, but is there an easier method of labeling arrays?

    my $string = "awesome_array"; my $other_string = "another_awesome_array"; push (@$string, 1, 2, 3, 4, 5 ); foreach my $element(@awesome_array){ print "$element\n"; push (@$other_string, $element) } print @another_awesome_array;
    This rigth here is something that will help me alot on the script i am working on right now actually :) glad i figured this little tid bit out.

      Try that code with strictures (which you should always use: use strict; use warnings; - see The strictures, according to Seuss). It works without strictures because of some magic. That magic can make it very hard to write correct code and find errors when things go wrong.

      You can achieve similar results by replacing the magic with a hash:

      use strict; use warnings; my %arrays; push @{$arrays{awesome_array}}, 1 .. 5; for my $element(@{$arrays{awesome_array}}){ print "$element\n"; push @{$arrays{another_awesome_array}}, $element; } for my $arrayName (keys %arrays) { print "$arrayName: @{$arrays{$arrayName}}\n" }

      Prints:

      1 2 3 4 5 awesome_array: 1 2 3 4 5 another_awesome_array: 1 2 3 4 5
      Perl is the programming world's equivalent of English
Re^2: using sub routines and solving an issue i have with passing variables
by james28909 (Deacon) on Sep 30, 2014 at 21:22 UTC
    i do alot of files inside of other files work. so i need to be able to maybe disassemble the file in one sub, send each file to its own sub for further examining-ing. thanks for taking yoru time and writing up something, every bit helps :)