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

Hi All I'm running into issue while reading and splittling the content to assign the hash and array. I picked this concept so that I can print to the console and ask the users to select services with a # and then based on option will call the options and they can select all options too by entering all.

List of Services ================== HR:1453:Recruitment:Up FS:2355:Payments:Down RS:4555:Retail:Up LD:682111:Legal:Up

That is the file content I should read and print with options something like below

Services List: ============== 1. Recruitment::Up 2. Payments:Down 3. Retail::Up 4. Legal::Up Please enter the # to call the service: Enter A for all the services
My current code is looking like this.
host(); my $it=1; my %appHash=(); if($it == 1){ open(INPUTFILE,"<Services.conf")or die("unable to Services fiel"); while (<INPUTFILE>) { chomp; my $svsc = $_; @svcArray= split(/:/,$svsc); } close (INPUTFILE); foreach my $svc (@svcArray){ $appHash{it} = $svc; print "\n\t\t\t$it. $appHash{$it}\n"; $size = $it; $it++; } print "\n"; print "\t\t\tA for All \n"; print "Please enter # for the service you're trying to call:"); chop($input=<STDIN>); $input=trim($input);# I've function written for taking the spaces of i +nput print " The input after chopping:$input\n"; }

I'd need to send the strings like Payments for example if user enter #2. I'll assign it to some temp variable...

But this is not working it is taking everything off the list i.e.services.conf file and just printing List of Services

Hasing and array concept looks great but need assistance to get this through. Thanks so much in advance

Replies are listed 'Best First'.
Re: hashing and arrays issue
by toolic (Bishop) on Apr 09, 2015 at 15:40 UTC
    It looks like you only need an array:
    use warnings; use strict; my @svcArray; while (<DATA>) { chomp; push @svcArray, (split /:/, $_, 3)[2]; } my $i = 1; for (@svcArray) { print "$i. $_\n"; $i++; } __DATA__ HR:1453:Recruitment:Up FS:2355:Payments:Down RS:4555:Retail:Up LD:682111:Legal:Up

    Prints:

    1. Recruitment:Up 2. Payments:Down 3. Retail:Up 4. Legal:Up

    But, if you need to store more data, a hash may also be suitable. Refer to perldsc

      My updated code works for single inputs and for all but planning to extend this functionality by accepting comma seperated values. Need help with this. Thanks in advance!

      host(); my $it=1; my %appHash=(); if($it == 1){ open(INPUTFILE,"<Services.conf")or die("unable to Services fiel"); while (<INPUTFILE>) { chomp; push @svcArray, (split /:/, $_, 3)[2]; } foreach my $svc (@svcArray){ $appHash{it} = $svc; print "\n\t\t\t$it. $appHash{$it}\n"; $size = $it; $it++; } print "\n"; print "\t\t\tA for All \n"; print "Please choose a number from the list above: "; chop($input=<STDIN>); $input=trim($input); print " The input after chopping:$input\n"; } my $appCmnStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><env:Envelo +pe xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"> <env:Body> <oos:req xmlns:oos=\"http: +//www.ecomm.com\">; send_svc($input); exit(0); ################# sub send_svc{ my @aArray; if(@_[0] && (lc @_[0] ne 'z')){ @aArray = (@_[0]); }else{ @aArray= (1 .. $size); } foreach $svcObjNum (@aArray){ if($appHash{$svcObjNum}){ $appsStr = $appsStr."<oos:obj class=\"$appHash{$svcObjNum}"/>"; } }

      from here I concatenate the XML lines and make a post to service. All working well for single input or z which is for all. But need some assistance how can I achieve this for multiple input values comma or space separated. With the fact that need to from those XML lines based on the options users select and then post it to my service

      On console it looks like below option

      1. Recruitment 2. Payments 3. Retail 4. Legal z. all Please choose a number from the list above:
Re: hasing and arrays issue
by GotToBTru (Prior) on Apr 09, 2015 at 15:25 UTC

    I would suggest getting familiar with the Perl debugger. You will be able to inspect your variables and see if they contain what you expect. For instance, I don't think @svcArray contains what you think it contains. You're overwriting it each time you read a service line, instead of adding each line in turn.

    Dum Spiro Spero