Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^2: Splitting a long row with multiple delimiters.

by dipit (Sexton)
on Jan 19, 2018 at 14:19 UTC ( [id://1207525]=note: print w/replies, xml ) Need Help??


in reply to Re: Splitting a long row with multiple delimiters.
in thread Splitting a long row with multiple delimiters.

Thank you Buddy! This solution almost worked well, but found problem with first value and then all values simultaneously, output as : Here, eab12345 should be null as it has no value, but it comes as key-value pair with "id"

KEY = id, VALUE = eab12345 KEY = pgrp, VALUE = 00000 KEY = groups, VALUE = abcdefgh KEY = home, VALUE = abcdefgh KEY = shell, VALUE = /home/eab12345 KEY = gecos, VALUE = /usr/bin/ksh KEY = auditclasses, VALUE = AB/C/Y0000/ABC/XYZ RTYUI, LMNOP *CONTRACTO +R* (AS 00000) Use of uninitialized value $key in concatenation (.) or string at samp +le.pl line 63. Use of uninitialized value $value in concatenation (.) or string at sa +mple.pl line 63. KEY = , VALUE = KEY = su, VALUE = true KEY = rlogin, VALUE = true KEY = daemon, VALUE = true KEY = admin, VALUE = true

Replies are listed 'Best First'.
Re^3: Splitting a long row with multiple delimiters.
by jahero (Pilgrim) on Jan 19, 2018 at 14:34 UTC

    Is it safe to assume that keys not followed by = sign are null? If so, how would you define a key?

    For example: key1=abcd key2 key3=123

    What is actually expected output? This one?

    key1=abcd key2 key3=123
    Or this one?
    key1=abcd key2=null key3=123
    How do you recognize null keys inside the string? Should key2 be treated as key, or is it actually part of value to be stored in key1?

      yes the keys which donot have "=" (in this case only first value) or have "key=<whitespace>",value will be considered as null or undef.

        Do all the keys which do not have "=" appear at the beginning of the string ?

        poj
Re^3: Splitting a long row with multiple delimiters.
by hippo (Bishop) on Jan 19, 2018 at 14:42 UTC

    Since this looks nothing like the output obtained from my code above, please provide an SSCCE. Thanks.

      Buddy, this is your code only, i have just inserted Print statement.

      use strict; use warnings; #use Test::More tests => 1; my $in = 'eab12345 id=00000 pgrp=abcdefgh groups=abcdefgh home=/home/e +ab12345 shell=/usr/bin/ksh gecos=AB/C/Y0000/ABC/XYZ RTYUI, LMNOP *CON +TRACTOR* (AS 00000) auditclasses=general,files,TCPIPlogin=true su=tru +e rlogin=true daemon=true admin=false sugroups=ALL admgroups= tpath=n +osak ttys=ALL expires=0 auth1=SYSTEM auth2=NONE umask=00 registry=AD +SYSTEM=AD logintimes= loginretries=5 pwdwarntime=5 account_locked=fal +se minage=0 maxage=13 maxexpired=0 minalpha=1 minother=1 mindiff=1 ma +xrepeats=2 minlen=8 histexpire=13 histsize=8 pwdchecks= dictionlist=/ +abc/def/ghi/jkl default_roles= fsize=-1 cpu=-1 data=-1 stack=65536 co +re=000000 rss=65536 nofiles=2000 time_last_login=1512632113 time_last +_unsuccessful_login=1505304923 tty_last_login=ssh tty_last_unsuccessf +ul_login=ssh host_last_login=0.000.000.000 host_last_unsuccessful_log +in=0.000.000.000 unsuccessful_login_count=0 roles= '; my %rec; my $key; for my $term (split /=/, $in) { my ($value, $newkey) = ($term =~ /(.*?) (\S+)$/); $rec{$key} = $value if defined $key; $key = $newkey; print "KEY = $key, VALUE = $value\n"; }
        $rec{$key} = $value if defined $key; $key = $newkey; print "KEY = $key, VALUE = $value\n";
        put that print statement one line earlier, like this:
        $rec{$key} = $value if defined $key; print "KEY = $key, VALUE = $value\n"; $key = $newkey;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1207525]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-19 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found