Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Update: AthanassiosAthanassios correctly pointed out that my attempt to set defaults doesn't work.
The problem appears to be that the absence of the element from the first list (ad) causes the defaults list to never be checked. A condensed exhibit:

ad = {"accept_rate" -> 75, "account_id" -> 395497, "age" -> 41, "badge_counts" -> {"bronze" -> 35, "gold" -> 0, "silver" -> 11}, "creation_date" -> 1326833982, "display_name" -> "Verbeia", "is_employee" -> False, "last_access_date" -> 1331949804, "last_modified_date" -> 1330990001, "link" -> "http"https://mathematica.stackexchange.com/users/8/verbeia", "location" -> "Sydney, Australia", "profile_image" -> "http://www.gravatar.com/avatar/3df2379fc0221bb0281c0d608542bd84?\ d=\ identicon&r=PG", "reputation" -> 3571, "reputation_change_day" -> 0, "reputation_change_month" -> 857, "reputation_change_quarter" -> 3475, "reputation_change_week" -> 605, "reputation_change_year" -> 3475, "user_id" -> 8, "user_type" -> "registered", "website_url" -> "http://www.verbeia.com/mathematica"}; 

Update: Athanassios correctly pointed out that my attempt to set defaults doesn't work.
The problem appears to be that the absence of the element from the first list (ad) causes the defaults list to never be checked. A condensed exhibit:

ad = {"accept_rate" -> 75, "account_id" -> 395497, "age" -> 41, "badge_counts" -> {"bronze" -> 35, "gold" -> 0, "silver" -> 11}, "creation_date" -> 1326833982, "display_name" -> "Verbeia", "is_employee" -> False, "last_access_date" -> 1331949804, "last_modified_date" -> 1330990001, "link" -> "http://mathematica.stackexchange.com/users/8/verbeia", "location" -> "Sydney, Australia", "profile_image" -> "http://www.gravatar.com/avatar/3df2379fc0221bb0281c0d608542bd84?\ d=\ identicon&r=PG", "reputation" -> 3571, "reputation_change_day" -> 0, "reputation_change_month" -> 857, "reputation_change_quarter" -> 3475, "reputation_change_week" -> 605, "reputation_change_year" -> 3475, "user_id" -> 8, "user_type" -> "registered", "website_url" -> "http://www.verbeia.com/mathematica"}; 

Update: Athanassios correctly pointed out that my attempt to set defaults doesn't work.
The problem appears to be that the absence of the element from the first list (ad) causes the defaults list to never be checked. A condensed exhibit:

ad = {"accept_rate" -> 75, "account_id" -> 395497, "age" -> 41, "badge_counts" -> {"bronze" -> 35, "gold" -> 0, "silver" -> 11}, "creation_date" -> 1326833982, "display_name" -> "Verbeia", "is_employee" -> False, "last_access_date" -> 1331949804, "last_modified_date" -> 1330990001, "link" -> "https://mathematica.stackexchange.com/users/8/verbeia", "location" -> "Sydney, Australia", "profile_image" -> "http://www.gravatar.com/avatar/3df2379fc0221bb0281c0d608542bd84?\ d=\ identicon&r=PG", "reputation" -> 3571, "reputation_change_day" -> 0, "reputation_change_month" -> 857, "reputation_change_quarter" -> 3475, "reputation_change_week" -> 605, "reputation_change_year" -> 3475, "user_id" -> 8, "user_type" -> "registered", "website_url" -> "http://www.verbeia.com/mathematica"}; 
deleted 2 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

One can use the modified form OptionValue[{}, {ad, defaults}, . . .] to avoid the message but the result is still incorrect. I am sorry forI missed this problem when I posted this answer.

One can use the modified form OptionValue[{}, {ad, defaults}, . . .] to avoid the message but the result is still incorrect. I am sorry for missed this problem when I posted this answer.

One can use the modified form OptionValue[{}, {ad, defaults}, . . .] to avoid the message but the result is still incorrect. I am sorry I missed this problem when I posted this answer.

added 1029 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Update: Athanassios correctly pointed out that my attempt to set defaults doesn't work.
The problem appears to be that the absence of the element from the first list (ad) causes the defaults list to never be checked. A condensed exhibit:

ad = {"accept_rate" -> 80, "badge_counts" -> {"bronze" -> 39, "silver" -> 9}}; defaults = {"age" -> Missing[], "badge_counts" -> {"bronze" -> #, "gold" -> #, "silver" -> #} &@ Missing["NotAvailable"]}; OptionValue[{ad, defaults}, {"accept_rate", "age", "badge_counts" -> "bronze", "badge_counts" -> "gold"}] 

OptionValue::optnf: "Option name "badge_counts" -> "gold" not found in defaults ..."

{80, Missing[], 39, "badge_counts" -> "gold"} 

One can use the modified form OptionValue[{}, {ad, defaults}, . . .] to avoid the message but the result is still incorrect. I am sorry for missed this problem when I posted this answer.


It seems to me that this is the correct way to extract values from a nested list of rules:

It seems to me that this is the correct way to extract values from a nested list of rules:

Update: Athanassios correctly pointed out that my attempt to set defaults doesn't work.
The problem appears to be that the absence of the element from the first list (ad) causes the defaults list to never be checked. A condensed exhibit:

ad = {"accept_rate" -> 80, "badge_counts" -> {"bronze" -> 39, "silver" -> 9}}; defaults = {"age" -> Missing[], "badge_counts" -> {"bronze" -> #, "gold" -> #, "silver" -> #} &@ Missing["NotAvailable"]}; OptionValue[{ad, defaults}, {"accept_rate", "age", "badge_counts" -> "bronze", "badge_counts" -> "gold"}] 

OptionValue::optnf: "Option name "badge_counts" -> "gold" not found in defaults ..."

{80, Missing[], 39, "badge_counts" -> "gold"} 

One can use the modified form OptionValue[{}, {ad, defaults}, . . .] to avoid the message but the result is still incorrect. I am sorry for missed this problem when I posted this answer.


It seems to me that this is the correct way to extract values from a nested list of rules:

Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading