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

Hello Wise Monks,
I come forth Seeking your Knowledge for the good of Humanity.
I need to get the values with a Single id "j" rather than many like 'i' either as CSV or space.
The only constrain I was given that I should
use only Config::IniFiles Predefined Method
to get this and store it an array.
Not REGEX or SPLIT FUNCTION or Any other third party Methods .

Please Help me with your Wise Knowledge.


This is my Code:
#! /usr/bin/perl use warnings; use strict; use Getopt::Long; use Config::IniFiles; # SAMPLE : SET CONFIGURATION SAMPLE FILE TO READ. my ($sample,%ini,$cfg,%input_config_citi); GetOptions('config=s'=>\$sample); # TIE : OBTAIN THE CONFIG FILES CONTETNS $cfg= Config::IniFiles->new(-file=>$sample); tie %ini,'Config::IniFiles', (-file=>$sample); %input_config_citi=%{$ini{'hello'}}; #my @arr=$cfg->val('hello','i'); my @arr=$cfg->val('hello','j'); print "\n\nThe values are : \n\n"; print "$_\n",for(@arr);

This is my INI file.
Code:
# # # # # # [hello] # # # # i=abc i=def i=ghi i=jkl i=mno j=abc def ghi jkl mno pqrs tuv wxyz

Update

SOLUTION FOUND BY MYSELF:

ANSWER


Dont Mind the Morons Reply down below without Any Answers, Has got not answers Just some whining

I need to get the values with a Single id "j" rather than many like 'i' either as CSV or space.


Well Instead of CSV or spaces, I used an \n with EOT.

It is so clear.
How can you not Understand..??????????

If you Already knew the Answer..??
Then What were you Waiting For...???????????????????????

Replies are listed 'Best First'.
Re: Using Config::IniFiles Module Obtain Comma Separated Values into An Array
by Discipulus (Canon) on May 27, 2016 at 07:11 UTC
    Hello,

    it seems i cannot install Config::IniFiles on my Strawberry Perl, but reading the docs

    # If you want a multi-line/value field returned as an array, just sp +ecify an array as the receiver: @values = $cfg->val('Section', 'Parameter');

    it seems to me you are doing correctly: it is not your @arr array populated? I'm missing your question?

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      Hello Discipulus,

      Thank you for taking the time to Notice and Reply :) :).

      The Previous set of population was by using
      i=abc i=def .... i=wxyz.
      This works perfectly fine.

      But there is some other Way to populate an array by using space,comma(s),dot(s) by default.

      like ...
      j=abc def ghi jkl mno pqrs tuv wxyz<br> j=abc,def,ghi,jkl,mno,pqrs,tuv,wxyz<br> j=abc.def.ghi.jkl.mno.pqrs.tuv.wxyz<br>
      I need this way of Obtaining he values in an array.

        Config::IniFiles, as many other modules, is meant as a versatile instrument and therefore allows spaces, commas, etc. as part of the configuration values. So you seem to be out of luck.

        Discipulus' hint is the best you can get, except he didn't quote the last sentence of that section:
        A multi-line/value field that is returned in a scalar context will be joined using $/ (input record separator, default is \n) if defined, otherwise the values will be joined using \n.
        So you'd have to decide beforehand which separator to use - EITHER space OR comma OR dot (OR whatever) - and go for something like
        my @arr; { local $/ = ' '; @arr=$cfg->val('hello','j'); }
        $/ is a Perl builtin, see perlvar (search for $RS), so it's not a "third party method".

        Sorry, that was wrong, describing the module's behaviour for returning a multivalue as a scalar. But what's wrong with Corion's suggestion? split is also a Perl builtin, not "third party".

        OR make the multiline field in the ini file conform to the format specified in the "FILE FORMAT" section of the module's documentation…

        Have you considered using split to split up the values?

Re: Using Config::IniFiles Module Obtain Comma Separated Values into An Array
by kcott (Archbishop) on May 28, 2016 at 07:40 UTC

    G'day perlPsycho,

    I'm not sure why you're creating both a Config::IniFiles object and a tied hash. I was able to get the output you wanted with each one, independent of the other.

    In pm_1164267_config_inifiles.pl:

    • $cfg and %ini are created in separate anonymous blocks and use different methods to get at the "abc def ..." string (i.e. the value of j= [$j_str]); however, when that string has been retrieved, all remaining code is shared (from process_j_str($j_str)).
    • &process_j_str shows how you might populate the array, i.e. transform "abc def ..." into ('abc', 'def', ...).
    • &_print_with_seps uses a localised $", aka $LIST_SEPARATOR, to output the array with the chosen separator.

    Source of pm_1164267_config_inifiles.pl:

    #!/usr/bin/env perl -l use strict; use warnings; use constant { IN_SEP => ( defined $ARGV[0] ? $ARGV[0] : ' ' ), OUT_SEP => ( defined $ARGV[1] ? $ARGV[1] : ' ' ), SECTION => ( length $ARGV[2] ? $ARGV[2] : 'hello'), PARM => ( length $ARGV[3] ? $ARGV[3] : 'j'), INI => ( length $ARGV[4] ? $ARGV[4] : 'pm_1164267_config_inifiles. +ini' ), }; use Config::IniFiles; { my $cfg = Config::IniFiles::->new( -file => INI ); my $j_str = $cfg->val( SECTION, PARM ); process_j_str($j_str); } { tie my %ini, 'Config::IniFiles', ( -file => INI ); my $j_str = $ini{+SECTION}{+PARM}; untie %ini; process_j_str($j_str); } sub process_j_str { my ($j_str) = @_; my @j_vals; _populate_j_vals_array($j_str, \@j_vals); _print_with_seps(\@j_vals); } sub _populate_j_vals_array { my ($j_str, $j_vals) = @_; my $i = 0; for (0 .. length($j_str) - 1) { my $char = substr $j_str, $_, 1; if ($char eq IN_SEP) { ++$i if length $j_vals->[$i]; } else { $j_vals->[$i] .= $char; } } } sub _print_with_seps { my ($j_vals) = @_; { local $" = OUT_SEP; print "j=@$j_vals"; } }

    pm_1164267_config_inifiles.ini starts with the same data you provided. I've added half a dozen lines to test what you described in "Re^2: Using Config::IniFiles Module Obtain Comma Separated Values into An Array".

    $ cat pm_1164267_config_inifiles.ini # # # # # # [hello] # # # # i=abc i=def i=ghi i=jkl i=mno j=abc def ghi jkl mno pqrs tuv wxyz [spaces] j=abc def ghi jkl mno pqrs tuv wxyz [commas] j=abc,def,ghi,jkl,mno,pqrs,tuv,wxyz [dots] j=abc.def.ghi.jkl.mno.pqrs.tuv.wxyz

    Here's some sample runs:

    $ pm_1164267_config_inifiles.pl j=abc def ghi jkl mno pqrs tuv wxyz j=abc def ghi jkl mno pqrs tuv wxyz
    $ pm_1164267_config_inifiles.pl ' ' j=abc def ghi jkl mno pqrs tuv wxyz j=abc def ghi jkl mno pqrs tuv wxyz
    $ pm_1164267_config_inifiles.pl ' ' , j=abc,def,ghi,jkl,mno,pqrs,tuv,wxyz j=abc,def,ghi,jkl,mno,pqrs,tuv,wxyz

    Testing the additional INI lines:

    $ pm_1164267_config_inifiles.pl ' ' : spaces j=abc:def:ghi:jkl:mno:pqrs:tuv:wxyz j=abc:def:ghi:jkl:mno:pqrs:tuv:wxyz
    $ pm_1164267_config_inifiles.pl , % commas j=abc%def%ghi%jkl%mno%pqrs%tuv%wxyz j=abc%def%ghi%jkl%mno%pqrs%tuv%wxyz
    $ pm_1164267_config_inifiles.pl . , dots j=abc,def,ghi,jkl,mno,pqrs,tuv,wxyz j=abc,def,ghi,jkl,mno,pqrs,tuv,wxyz

    — Ken

Re: Using Config::IniFiles Module Obtain Comma Separated Values into An Array
by stevieb (Canon) on May 27, 2016 at 15:41 UTC

    This one-liner suits all of your requirements (no split or regex). Getting the actual value from the config file into a string variable is your responsibility.

    perl -E '$s="abc def ghi";@a=unpack("(a)*",$s);$x;@b;for(@a){chomp;if( +ord($_)!=32){$x.=$_}else{push@b,$x;undef$x}};push@b,$x;END{say$_ for@ +b}'

    You now have an array, @b, that looks like this:

    $b[0] = 'abc'; $b[1] = 'def'; $b[2] = 'ghi';
Re: Using Config::IniFiles Module Obtain Comma Separated Values into An Array
by Anonymous Monk on May 27, 2016 at 11:22 UTC

    I'm going to wager a guess that your instructor worded the problem the way s/he did so that you develop your own version of split using the functions index and substr.

      No its Pretty Simple.
      Here's the link.

      Thanks Everyone for Discussing this topic Today:

      I hope some one finds it useful when they are stuck in the future.

      Link: EOT_STYLES
      Simple Config File Modification:

      # # # # # # [hello] # # # # i=abc i=def i=ghi i=jkl i=mno # j=<<EOT abc def ghi jkl mno pqrs tuv wxyz EOT # # #


      Unlike a few guys here
      I provide snippets of Real time Examples:
      Snippet Code :
      #! /usr/bin/perl use warnings; use strict; use Getopt::Long; use Config::IniFiles; # SAMPLE : SET CONFIGURATION SAMPLE FILE TO READ. my ($sample,%ini,$cfg,%input_config_citi); GetOptions('config=s'=>\$sample,-nomultiline => 0); # TIE : OBTAIN THE CONFIG FILES CONTETNS $cfg= Config::IniFiles->new(-file=>$sample); my @arr=$cfg->val('hello','i'); my @arr=$cfg->val('hello','j'); print "\n\nThe values are : "; print "$_\n",for(@arr);

        That is not a solution to the problem as you previously stated it:

        But there is some other Way to populate an array by using space,comma(s),dot(s) by default.

        like ...

        j=abc def ghi jkl mno pqrs tuv wxyz<br> j=abc,def,ghi,jkl,mno,pqrs,tuv,wxyz<br> j=abc.def.ghi.jkl.mno.pqrs.tuv.wxyz<br>

        I need this way of Obtaining he values in an array.

        And here:

        I cant get the space separated value. I need a way to get the j= abc def ghi jkl mno pqrs tuv wxyz in a single line of code using a method of Config::IniFiles.

        Changing the requirements (by changing the input) is unfair to those trying to help.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Using Config::IniFiles Module Obtain Comma Separated Values into An Array
by Anonymous Monk on May 27, 2016 at 07:01 UTC
    :) and what happens instead?
      Hello Anonymous Monk,

      I cant get the space separated value.

      I need a way to get the j= abc def ghi jkl mno pqrs tuv wxyz
      in a single line of code using a method of Config::IniFiles.

      like my
      @array=$cfg->arrayValBySpace('hello','j')

      Note:
      not as a single line like abc ... wxzy
      but as separate values like $arr[1]= abc , $arr\[[$#arr]=wxyz.
        Well, Config::IniFiles doesn't do that, it only supports newlines seperation, so the only way to get it, is to subclass/extend Config::IniFiles or ...
Re: Using Config::IniFiles Module Obtain Comma Separated Values into An Array
by Anonymous Monk on May 27, 2016 at 10:28 UTC
    From your various posts in this thread:

    I cannot share any information regarding my requirements. ... I would be posting a solution here... I just need to make a modification on my config file.

    Please stop wasting our time. Looking forward to your solution.

    A reply falls below the community's threshold of quality. You may see it by logging in.