Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Since your code doesn't produce a result and you haven't told us what you want, this is a guessing game.

On style, this: for my $i (0..$#$ref_a) is a super red flag. This sort of loop variable is a "non-Perl" way to do things and $# is deprecated anyway.

I guessed that you wanted to merge the $ref_b stuff into the $ref_a stuff and get a revised version of $ref_a reflecting a new sorted version of those updates (eg "j,k,l" should come before "x,y,z"),like this:

3 4 5 6 7 9 a b c d e f g h i j k l m n o p q r x y z

The code below shows how to do that..and what to do if you want numbers to appear after letters (sort order change)...

#!/usr/bin/perl -w use strict; use Data::Dumper; my $ref_a = [ [ qw/a b c/ ], [ qw/d e f/ ], [ qw/g h i/ ], [ qw/x y z/ ], [ qw/3 4 5/ ], [ qw/6 7 9/ ], ]; my $ref_b = [ [ qw/j k l/ ], [ qw/m n o/ ], [ qw/p q r/ ], ]; #clone (deep copy) the lists in $ref_b into $ref_a #by appending them to the List of Lists (LoL) in $ref_a... foreach my $bref (@$ref_b) { push @$ref_a,[@$bref]; } #now add 99 to each list in $ref_b (a new "column") #so that we can see that modifying b no longer #modifies a, meaning that the "deep copy" worked! @$ref_b = map{[@$_,99]}@$ref_b; print Dumper($ref_a,$ref_b); # here is a way to sort the LoL by "column" # without using [$i] indicies... @$ref_a = sort by_column @$ref_a; sub by_column { my @a_cols = @$a; my @b_cols = @$b; foreach my $item_a (@a_cols) { my $item_b = shift (@b_cols); my $compare_not_equal = $item_a cmp $item_b; return $compare_not_equal if $compare_not_equal; } return 0; } print "Sorted results in \$ref_a, reference to LOL\n"; foreach (@$ref_a) { print "@$_\n"; } # if we want the numbers to be greater than the alpha # characters, then we replace # my $compare_not_equal = $item_a cmp $item_b; # with something like: # my $compare_not_equal = myCompare($item_a,$item_b) # where myCompare returns the standard >0,<0,=0 for # what the order you want __END__ OUTPUT: $VAR1 = [ [ 'a', 'b', 'c' ], [ 'd', 'e', 'f' ], [ 'g', 'h', 'i' ], [ 'x', 'y', 'z' ], [ '3', '4', '5' ], [ '6', '7', '9' ], [ 'j', 'k', 'l' ], [ 'm', 'n', 'o' ], [ 'p', 'q', 'r' ] ]; $VAR2 = [ [ 'j', 'k', 'l', 99 ], [ 'm', 'n', 'o', 99 ], [ 'p', 'q', 'r', 99 ] ]; Sorted results in $ref_a, reference to LOL 3 4 5 6 7 9 a b c d e f g h i j k l m n o p q r x y z

In reply to Re: Undefined Array Reference by Marshall
in thread Undefined Array Reference by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-23 06:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found