in reply to Removing digits until you see | in a string

Still using split but no loop and no mess.

$str = '703555121245874|45874 Smith St|Your Town|New Hampshire'; %data_hash = map { split m{\|}, $_, 2 } $str;

If you are perhaps reading a lot of these strings from a file you could populate the hash in one fell swoop.

my %data_hash = map { split m{\|}, $_, 2 } map {chomp; $_} <$fileHandle>;

I hope this is of use.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: Removing digits until you see | in a string
by Animator (Hermit) on Jan 08, 2007 at 12:10 UTC

    That is a bad idea.

    If he is reading a lot of these strings from a file then it implies that there are a lot of records in the file.

    What your code is doing is first reading the entire file into the memory and after that starting to process it.

    Also: you can combine both maps just fine.
    That is: map { chomp; split m{\|}, $_, 2 }

      What's bad about reading the file into memory? With modern computer systems it is quite a common idiom to read the whole of a file into memory before processing it. Only if the data file was very large would this become a bad idea.

      Combining the maps is good, I should have thought of it myself.

      Cheers,

      JohnGG

        What is bad about it (IMHO):

        • file size is unknown
        • processing does not start untill you are done reading
        • using at the very least three of four times the size of the file in memory
        • most people will not realizse that you are reading it in memory first. If you really want to read it all at once then I would suggest reading it in an array first.
        • the processing will be slower then reading it with a while and immedaitly creating a hash-element for each record. Now you read it in a temporary list, then you loop over that list, while looping over it you create a new list, and then you finally assign that list to a hash. Not that you will notice the speed/memory difference but that doesn't mean it's not there.

        And with this technique you can't check (easily) for duplicate elements - but that wasn't asked