Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How can I use 'split' with unknown amount of variables?

by bladx (Chaplain)
on Dec 30, 2001 at 08:29 UTC ( #135204=perlquestion: print w/ replies, xml ) Need Help??
bladx has asked for the wisdom of the Perl Monks concerning the following question:

How can I use the split function to find how many variables to split specified data from a slice from a database with data such as: key|title|date|author|news? I know how to do it with the known columns needed for splitting lines of data, but how can I do this when I don't know how many variables I will need for a line from a given database? Thank you for any help!

Andy Summers

Comment on How can I use 'split' with unknown amount of variables?
Select or Download Code
(shockme) Re: How can I use 'split' with unknown amount of variables?
by shockme (Chaplain) on Dec 30, 2001 at 08:34 UTC
    Split it into an array:

    (@variables) = split/whatever01/whatever02/;

    P.S. Are you THE Andy Summers?

    Update: bladx is not the Andy Summers.....poop.

    If things get any worse, I'll have to ask you to stop helping me.

Re: How can I use 'split' with unknown amount of variables?
by rbc (Curate) on Dec 30, 2001 at 08:43 UTC
    try something like ...

    my $s = "one|two|three|four"; (@items)=split /\|/, $s; print "NI = $#items+1\n@items\n";

    ... I hvae been drinking cheap wine so you
    better double check :)
    --
    Its like a dog that can sing and dance.
    It's remarkable because it can do it.
    Not that it can do it well.

      I think bladex may be referring to the split behavior many people forget about: by default it strips null fields off the end. So if one line is: one|two|three|four but the next is five|six|| then the first split gets a 4 element array, the second gets a two element array. This can cause problems if the application always wants 4 fields regardless. The way around that is to use the seldom used 3rd arrgument to split, LIMIT. If you know how many fields you'll always have, set it to that number. Otherwise set it to a negative value to always keep null fields in each split line. See the docs for details or I can elaborate further.

        steves is right. I use split's LIMIT argument from time to time. It's quite handy.

        This illustration prints each element on separate line, showing the nulls:

        #!/usr/bin/perl -w use strict; my $item1 = "1,2,3,4,5"; my $item2 = "a,b,c,,"; my @split1 = split( /,/, $item1, 5); my @split2 = split( /,/, $item2, 5); # better yet, to set the size of the array at run-time... # my @split2 = split( /,/, $item2, ($#split1 + 1)); print "item1 has " . ( $#split1 + 1) ." elements:\n"; foreach(@split1){ print "'$_'\n"; } print "\nitem2 has " . ( $#split2 + 1) ." elements\n"; foreach(@split2){ print "'$_'\n"; }

Re: How can I use 'split' with unknown amount of variables?
by marmot (Novice) on Dec 30, 2001 at 23:02 UTC
    It's hard to tell exactly what you're asking. Do you just want to know how many elements are returned to you? Easy. Do as others have suggested: split to an array and then find the number of elements it contains. You don't necessarily need to assign variables; just use the array elements directly.
    @list = split /\|/, FuncThatGivesData(); print "Found ", scalar(@list), " elements\n"; $i = 0; foreach $e (@list) { print "Element $i: $list[$i]\n"; $i++; }
    Do you need to know which variables came back? Your example, "key|title|date|author|news", could be interpreted as a regular expression (i.e., "key OR title OR date...") or as a literal string, where '|' is the separator. If '|' is the separator, follow the example above. If you really are asking how to tell which pieces of data were returned, you need some kind of header to idenfity the fields, and it's a somewhat more complex problem.

    Can you be more specific with your question?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://135204]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2014-08-01 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (6 votes), past polls