in reply to passing string to sub and string split

You claim a "working script", but I see no evidence of that. I see no use of sub{} in your code. Pass the subroutine a list of values. These can be interpreted by subroutine as simple list or a hash (key,value) pairs. Both ways are shown below. There are ways to pass references to hashes or references to arrays, but that appears to be beyond the scope of what is required here.
#!usr/bin/perl -w use strict; my %hash = ( 'NAME' => 'name222', 'COLOR' => 'yellow', 'SIZE' => 'big', ); Some_subroutine(%hash); sub Some_subroutine { my @list = @_; print "This shows that %hash is just a list of key,value pairs\n"; print " when passed like sub(%hash)\n"; print "list=@list\n\n"; my %hash = @_; print "This shows that assigning default list (@_) \n"; print " to %hash results in hash (key,value) assignments\n\n"; foreach my $key (keys %hash) { print "key $key \tvalue is $hash{$key}\n"; } } __END__ PRINTS: This shows that %hash is just a list of key,value pairs when passed like sub(%hash) list=SIZE big COLOR yellow NAME name222 This shows that assigning default list (SIZE big COLOR yellow NAME nam +e222) to %hash results in hash (key,value) assignments key COLOR value is yellow key SIZE value is big key NAME value is name222

Replies are listed 'Best First'.
Re^2: passing string to sub and string split
by BioLion (Curate) on Aug 02, 2009 at 10:33 UTC

    Marshall's advice can also be applied to when the OP wants to pass multiple arg hashes they can be passed as refs and looped over (avoiding the nasty splitting of args!):

    #!usr/bin/perl -w ## shamelessly copied from Marshall use strict; use Data::Dumpe qw/Dumper/; ## pass two anon hash refs to the sub Some_subroutine( { 'NAME' => 'name222', 'COLOR' => 'yellow', 'SIZE' => 'big', }, { 'NAME' => 'name444', 'COLOR' => 'yellow444', 'SIZE' => 'big444', }, ); sub Some_subroutine { ## collect the refs passed to the sub my @list = @_; ## just to see what we got print Dumper \@list; ## loop through each ref passed and process as before for my $ref (@list){ foreach my $key (keys %$ref) { print "key $key \tvalue is $ref->{$key}\n"; } } }
    Just a something something...
      This is more than Just a something something.. Your code shows a certain sophistication that you are: Getting it!.

Re^2: passing string to sub and string split
by BioLion (Curate) on Aug 02, 2009 at 10:32 UTC

    Multiple post... Reap me!

    Just a something something...