in reply to Spliting a delimited string into variables

You print $line->[0] which is quite different from $line[0]. How this prints "371225..." is beyond me.

You also do lots of strange things to my eye, but that is to be expected ;-). For example to copy @arr into @line (for which you used a loop) "@arr= @line;" would have been enough. The same goes for "my @lines= ($line[0]..."

Also using lots of variables called $line, $lines, @line, @lines makes any program into an entry into the obfuscation contest. Differentiate your variables better

What you have been looking for might be this:

# starting with @arr having all the lines my @ticker; my @names=('ticket','dateadded','stime','etime','pri','SiteName','comm +ents'); my $i=0; foreach my $line (@arr) { my @items= split(/\|/, $line; print "ETime is $items[3]\n"; $ticker[$i]{$names{$_}}= $items[$_] for scalar @names; $i++; } # now $ticker[3]{SiteName} is SiteName of the fourth line

Note @ticker and $i are only neccessary if you want to operate on the data after the loop. If you just want to print or store the stuff, do that in the foreach loop above. You can also just use the split line you have in your script to fill the variables with the data inside the loop instead of using @items if it suits you better

Replies are listed 'Best First'.
Re^2: Spliting a delimited string into variables
by pissflaps (Initiate) on Apr 07, 2011 at 19:32 UTC
    This is really close to where I was going. I can't seem to initialize {$names{$_}} due to the curly braces. Is this assigning everything into a hash table? If so, I'm receiving an error about either $names or $items that it isn't initialized, which is very similar to the errors my original code pulled. Is there maybe a different notation to assign everything into a hash while still initializing the variables?

      Initializing a hash can be done with an array, each pair of values in the array form key and value of the hash. Often '=>' is used as an alias to ',' to make this obvious:

      my %hash= (0, 'ticket', 1 => 'dateadded');

      {$names{$_}} on the other hand is not a really useful expression in perl, %{$names{$_}} refers to a hash whose reference is stored as value in another hash (i.e %names). A multidimensional hash or HashOfHashes in perlspeak

      If what you wanted was a Hash of Hashes, the syntax looks like this:

      $names{$_}{key}= 'value'; #or to initialize with an array: %{$names{$_}} = ('key1' => 'value1','key2' => 'value2');

      if you want further information about HoH (Hash of Hashes), you might want to read perldsc and perllol

        I've never used hashes before so this will be some good fresh reading material. It looks relative to a really complex array. In your experience with hashes, once you have all the structure laid out, is it simple enough to reference slices in the hash like you would in an array? That is to say $names[0][1] would pull up the value in the second column of the first row of the matrix? Thank you again for your great references and starting points. I am excited to learn!

Re^2: Spliting a delimited string into variables
by Anonymous Monk on Nov 28, 2013 at 14:49 UTC

    Hi,

    One other simpler way to make it works is to use regex memory as follow.

    my $line="371540|4/07/2011|08:03|11:03|2|Company Name (MAIN SITE)|DB P +URGE|" if ($line =~ /(\d+)\|([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})\|([0-9]{1,2}\: +[0-9]{2})\|([0-9]{1,2}\:[0-9]{2})\|(\d+)\|((?:\w+)\s\w+\s(?:\()?\w+\s +\w+\))\|(\w+(?:\s)?\w+)/ ) { $item1=$1; $item2=$2; ... $item7=$7 }

    Note that the regexp has to be adapted. The parentheses captures their content.
    First parenthese will be memorised as $1, Second parenthese will be memorised as $2 etc...